And*_*ter 5 android expandablelistview
我正在尝试访问GroupView的textview,它显示ChildView中所选复选框的计数.
例如 - North是GroupView,下面带有复选框的列表是ChildView.我想每次点击复选框更新计数(18).我在复选框上应用了OnClickListner.我有自定义ExpanadableListViewAdapter扩展BaseExpandableListAdapter.
这是我的代码片段 -
@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;
}
Run Code Online (Sandbox Code Playgroud)
小智 3
首先,以简单列表视图为例,而不是可扩展列表视图。ListView 是保存一堆 Item 的容器。
这些项目都有子视图,这些子视图由构成 ListView 中一行的各个元素组成。即不同的文本视图等。
适配器的 getView() 对数据集进行操作,然后在列表中创建项目。因此,如果您更改用于创建适配器的数据集,并调用notifyDatasetChanged(),则会更新列表视图。
现在,在您的情况下,您可能使用 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
Run Code Online (Sandbox Code Playgroud)
所以在 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();
}
}
Run Code Online (Sandbox Code Playgroud)
现在这种方法的唯一限制是 count 应该是 Object 的一部分。因此,如果您第一次从其他地方而不是从对象本身初始化计数,我建议您将计数存储在对象本身中,并从那里初始化 textView 的值。
所以在 getGroupView() 中
groupViewHolder.GroupCount.setText(""+((OutletListData) getGroup(groupPosition)).getCount());
Run Code Online (Sandbox Code Playgroud)