Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing groupview's TextView from getChildView() of ExpandableListView

I am trying to access a textview of GroupView which shows count of the selected checkbox in ChildView. enter image description here

For eg - North is the GroupView , and list below with checkbox is ChildView. I want to update the count(18) with each click on checkbox. I have applied OnClickListner on checkbox. I have custom ExpanadableListViewAdapter extends BaseExpandableListAdapter.

Here is my code snippet -

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_expandable_list_group,
                parent, false);

        groupViewHolder = new GroupViewHolder();

        groupViewHolder.GroupName = (TextView) convertView.findViewById(R.id.group_name);
        groupViewHolder.GroupCount = (TextView) convertView.findViewById(R.id.group_count);
        groupViewHolder.rightArrow = (ImageView) convertView.findViewById(R.id.right_arrow);

        convertView.setTag(groupViewHolder);
    }else{
        groupViewHolder = (GroupViewHolder) convertView.getTag();
    }
    groupViewHolder.GroupName.setText(((OutletListData) getGroup(groupPosition)).getName());
    groupViewHolder.GroupCount.setText(""+((OutletListData) getGroup(groupPosition)).getOutletDatas().size());

    return convertView;
}

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.filter_expandable_list_child,
                parent, false);

        childViewHolder = new ChildViewHolder();

        childViewHolder.childTextView = (TextView) convertView.findViewById(R.id.text1);
        childViewHolder.childCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);

        convertView.setTag(childViewHolder);
    }else{
        childViewHolder = (ChildViewHolder) convertView.getTag();
    }

    childViewHolder.childTextView.setText(((OutletData) getChild(groupPosition, childPosition)).getDealerName());
    childViewHolder.childCheckBox.setChecked(((OutletData) getChild(groupPosition, childPosition)).getSelected() == "1");
    childViewHolder.childCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isChecked = ((CheckBox) v).isChecked();
            ApplicationController.getEventBus().post(((OutletData) getChild(groupPosition, childPosition)).getOutletID());
            if (isChecked) {
                ((OutletData) getChild(groupPosition, childPosition)).setSelected("1");
            } else {
                ((OutletData) getChild(groupPosition, childPosition)).setSelected("0");
            }
        }
    });
    return convertView;
}
like image 747
BinaryGuy Avatar asked Oct 20 '22 00:10

BinaryGuy


1 Answers

First, take example of simple list view, not the expandable list view. ListView is the container that holds a bunch of Items.
Those Items each have child views that consist of the individual elements that make up a row in the ListView. i.e. different textViews etc.
The getView() of the adapter, operates on the data set and then creates items in the list. So if you change the data set that is used to create the adapter, and call the notifyDatasetChanged(), then it updates the list view.

Now in your case, You may be using an ArrayList to represent the objects like "NORTH". So if you store the value of count in this object then updation of count will be easy. Just access the data in the list using groupPosition and update it. And call the notifyDatasetChanged.

Let say you used mList which is ArrayList of type Object, to create the adapter

// ArrayList<Object> mList;
// You created mAdapter from this mList
// now mList is data set for this adapter

So in getChildView() you write :

@Override
    public void onClick(View v) {
        boolean isChecked = ((CheckBox) v).isChecked();
        ApplicationController.getEventBus().post(((OutletData) getChild(groupPosition, childPosition)).getOutletID());
        if (isChecked) {
            // your code 
            int count = ((OutletListData) getGroup(groupPosition)).getCount();
            count++;
            ((OutletListData) getGroup(groupPosition)).setCount(count);
            notifyDataSetChanged();
        } else {
            // your code;
            int count = ((OutletListData) getGroup(groupPosition)).getCount();
            count--;
            ((OutletListData) getGroup(groupPosition)).setCount(count);
            notifyDataSetChanged();

        }
    }

Now the only limitation to this approach is that count should be a part of Object. So if you are initialising count first time from some where else but not from Object itself, I will recommend that you store count in the Object itself, and initialise textView's value from there.
So in getGroupView()

groupViewHolder.GroupCount.setText(""+((OutletListData) getGroup(groupPosition)).getCount());
like image 190
Navratan Soni Avatar answered Oct 22 '22 00:10

Navratan Soni