dimanche 29 novembre 2015

How to find state of dynamic checkbox using ExpandableListView using parent and child from HashMap

I've been reading all around the web for days, and tried different methods on end to get the state of the checkboxes, but I'm finding it tough to apply it to my android application.

I need help on how to find the state of a checkbox dynamically filled with 'child' values in an ExpandableListView. Unfortunately, I have found myself at a dead end, and quite disheartened that I haven't been able to apply it correctly to my application.

In my program, I would like to see which CheckBox is ticked when a 'Submit' button is clicked, and use the text value of the checked box to retrieve important results.

Does anybody have an easy method to keep track of which checkboxes are clicked in my expandable listview?

The activity:

public class meal extends Activity {
    HashMap<String, List<String>> Meal_Category;
    List<String> Meal_list;
    ExpandableListView Exp_list;
    MealAdapter adapter;
    dataGenerator dg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        Meal_Category = new HashMap<>();
        setContentView(R.layout.activity_meal);
        Exp_list = (ExpandableListView) findViewById(R.id.exp_list);

        Log.d("myTag", "This is my message");
        dg = new dataGenerator();
        try {
            //TODO -- chuk mods
            dg.context = this;
            Meal_Category = dg.getInfo();
            //TODO -- end
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("mealCat", Meal_Category.toString());

        Meal_list = new ArrayList<String>(Meal_Category.keySet());
        adapter = new MealAdapter(this, Meal_Category, Meal_list);
        Exp_list.setAdapter(adapter);
    }

}

The adapter for the expandablelistview:

public class MealAdapter extends BaseExpandableListAdapter {
        private Context ctx;
        private HashMap<String, List<String>> Meal_Category;
        private List<String> Meal_List;

        public MealAdapter(Context ctx, HashMap<String, List<String>> Meal_Category, List<String> Meal_List )
        {
            this.ctx = ctx;
            this.Meal_Category = Meal_Category;
            this.Meal_List = Meal_List;
        }
    //Hashmap for keeping track of checkbox check states


        @Override
        public Object getChild(int parent, int child) {

            return Meal_Category.get(Meal_List.get(parent)).get(child);
        }

        @Override
        public long getChildId(int parent, int child) {
            // TODO Auto-generated method stub
            return child;
        }

        @Override
        public View getChildView(int parent, int child, boolean lastChild, View convertview,
                                 ViewGroup parentview)
        {
            String child_title =  (String) getChild(parent, child);
            if(convertview == null)
            {
                LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertview = inflator.inflate(R.layout.child_layout, parentview,false);
            }
            CheckBox child_cb = (CheckBox) convertview.findViewById(R.id.child_txt);
            child_cb.setText(child_title);

            return convertview;
        }

        @Override
        public int getChildrenCount(int arg0) {

            return Meal_Category.get(Meal_List.get(arg0)).size();
        }

        @Override
        public Object getGroup(int arg0) {
            // TODO Auto-generated method stub
            return Meal_List.get(arg0);
        }

        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return Meal_List.size();
        }

        @Override
        public long getGroupId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getGroupView(int parent, boolean isExpanded, View convertview, ViewGroup parentview) {
            // TODO Auto-generated method stub
            String group_title = (String) getGroup(parent);
            if(convertview == null)
            {
                LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertview = inflator.inflate(R.layout.parent_layout, parentview,false);
            }
            TextView parent_textview = (TextView) convertview.findViewById(R.id.parent_txt);
            parent_textview.setTypeface(null, Typeface.BOLD);
            parent_textview.setText(group_title);
            return convertview;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            // TODO Auto-generated method stub
            return false;
        }


    }

The activity_main xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp">

    <ExpandableListView
        android:id="@+id/exp_list"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
        android:divider="#A4C739"
        android:dividerHeight="0.5dp"
        ></ExpandableListView>

    <Button
        android:id="@+id/bSubmit"
        android:text="Submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="105dp"
        android:onClick="finalSelection"
        />


</RelativeLayout>

The child_layout activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <CheckBox
        android:id="@+id/child_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:onClick="selectItem"
        />

</LinearLayout>




Aucun commentaire:

Enregistrer un commentaire