Access Group View TextView from getChildView () ExpandableListView

I am trying to access a text view of a GroupView that shows the number of selected checkbox in ChildView. enter image description here

For example, North is a GroupView, and below is a ChildView flag. I want to update the score (18) with every click on the checkbox. I applied OnClickListner on the checkbox. I have a 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; } 
+5
source share
1 answer

First, take an example of simply viewing a list, not a drop-down list. ListView is a container containing a bunch of items.
These items have child views, which are made up of individual items that make up a row in a ListView. those. various text elements, etc.
The adapter's GetView () works with the data set and then creates the items in the list. Therefore, if you change the dataset that is used to create the adapter and call notifyDatasetChanged (), it will update the list view.

Now, in your case, you can use ArrayList to represent objects of type "NORTH". Therefore, if you store the count value in this object, then updating the counter will be simple. Just access the data in the list using groupPosition and update it. And call notifyDatasetChanged.

Say you used mList, which is an ArrayList of type Object, to create an 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 of this approach is that the account must be part of the object. Therefore, if you first initialize the counter from some other, but not from the object itself, I recommend that you store the count in the object itself and initialize the value of textView.
So in getGroupView ()

 groupViewHolder.GroupCount.setText(""+((OutletListData) getGroup(groupPosition)).getCount()); 
+3
source

Source: https://habr.com/ru/post/1233722/


All Articles