我正在尝试访问 GroupView 的 TextView ,它显示了 ChildView 中所选复选框的计数。
例如 - North 是 GroupView ,下面带有复选框的列表是 ChildView。我想在每次单击复选框时更新计数(18)。我在复选框上应用了 OnClickListner。我有自定义的 ExpanadableListViewAdapter extends BaseExpandableListAdapter。
这是我的代码 fragment -
@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;
}
最佳答案
首先,以简单 ListView 为例,而不是可扩展 ListView 。 ListView 是容纳一堆 Items 的容器。
这些项目每个都有 subview ,这些 subview 由构成 ListView 中一行的各个元素组成。即不同的 TextView 等。
适配器的 getView() 对数据集进行操作,然后在列表中创建项目。因此,如果您更改用于创建适配器的数据集,并调用 notifyDatasetChanged(),那么它会更新 ListView 。
现在在你的情况下, 您可能正在使用 ArrayList 来表示像“NORTH”这样的对象。因此,如果您将 count 的值存储在此对象中,那么 count 的更新将很容易。 只需使用 groupPosition 访问列表中的数据并更新它。并调用 notifyDatasetChanged。
假设您使用 mList 来创建适配器,它是对象类型的 ArrayList
// ArrayList<Object> mList;
// You created mAdapter from this mList
// now mList is data set for this adapter
所以在 getChildView() 中你写:
@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();
}
}
现在这种方法的唯一限制是计数应该是对象的一部分。因此,如果您是第一次从其他地方而不是从对象本身初始化计数,我建议您将计数存储在对象本身中,并从那里初始化 textView 的值。
所以在 getGroupView()
groupViewHolder.GroupCount.setText(""+((OutletListData) getGroup(groupPosition)).getCount());
关于android - 从 ExpandableListView 的 getChildView() 访问 groupview 的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33130804/