我在实现expandableListView时遇到了一些问题。我实现了我的ExpandableListAdapter
public class ExpandableListAdapter扩展了BaseExpandableListAdapter {
private static final int SUM = 1;
private static final int SUB = 2;
private Context context;
private List<String> listDataHeader; //Header titles
private HashMap<String, List<Article>> listDataChild;
private HashMap<Integer, Bitmap> productImagesHashMap;
public ExpandableListAdapter (Context context, List<String> listDataHeader, HashMap<String, List<Article>> listDataChild, HashMap<Integer, Bitmap> productImagesHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listDataChild;
this.productImagesHashMap = productImagesHashMap;
}
@Override
public int getGroupCount() {
return this.listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.user_expand_listview_header, null);
}
TextView listHeaderTextView = (TextView) convertView.findViewById(R.id.list_header);
listHeaderTextView.setTypeface(null, Typeface.BOLD);
listHeaderTextView.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Article childValues = (Article) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.product_row, null);
}
Integer articleId = childValues.getId();
final TextView articleIdTextView = (TextView) convertView.findViewById(R.id.product_id);
ImageView productImageTextView = (ImageView) convertView.findViewById(R.id.product_image);
TextView productNameTextView = (TextView) convertView.findViewById(R.id.product_name);
TextView productDescriptionTextView = (TextView) convertView.findViewById(R.id.product_description);
final TextView productPriceTextView = (TextView) convertView.findViewById(R.id.product_price);
articleIdTextView.setText(articleId.toString());
productImageTextView.setImageBitmap(productImagesHashMap.get(articleId));
productNameTextView.setText(childValues.getNome());
productDescriptionTextView.setText(childValues.getDescrizione());
productPriceTextView.setText(childValues.getPrezzo());
ImageButton addButton = (ImageButton) convertView.findViewById(R.id.add_btn);
final TextView articleQty =(TextView) convertView.findViewById(R.id.product_qnty);
ImageButton removeButton = (ImageButton) convertView.findViewById(R.id.remove_btn);
addButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
if (context instanceof UserActivity) {
((UserActivity) context).updateTotal(
Integer.parseInt(articleIdTextView.getText().toString()),
productPriceTextView.getText().toString(),
SUM);
articleQty.setText(updateProductQty(articleQty, SUM));
}
}
});
removeButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
if (context instanceof UserActivity) {
((UserActivity) context).updateTotal(
Integer.parseInt(articleIdTextView.getText().toString()),
productPriceTextView.getText().toString(),
SUB);
articleQty.setText(updateProductQty(articleQty, SUB));
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
private String updateProductQty(TextView productQty, int operation) {
Integer qty = Integer.parseInt(productQty.getText().toString());
switch (operation) {
case 1: //SUM
qty += 1;
break;
case 2: //SUB
if (qty > 0)
qty -= 1;
break;
}
return qty.toString();
}
}
有些产品从数据库中读取并按类型插入ExpandableListView(实际钢笔,笔记本和纸张)。然后有两个按钮,允许选择用户想要购买的数量,数量由TextView跟踪。现在,当我使用普通的ListView实现这一切时,所有工作都有效,但是使用ExpandableListView时,孩子的数量TextView之间存在干扰。
例如,ExpandableListView的实际组成是:
笔
- 黑笔
- 蓝笔
- 红笔
笔记本
- 黑色笔记本</ p>
- Blue Notebook
- 红色笔记本</ p>
纸张
- A4纸
如果用户添加黑色笔记本,还会添加蓝色笔
如果用户添加蓝色笔记本,还会添加黑色笔
如果用户添加红色笔记本,还会添加蓝色笔
为什么会有这些干扰?
答案 0 :(得分:0)
您没有正确跟踪数量更新。
由于ExpandableListView
- 与ListView
一样 - 回收其观点,您会遇到以下行为:
这是你需要做的:
Article
类articleQty
对象中包含的数量填充TextView
Article
。Article
对象中的数量并致电notifyDataSetChanged()
。规则是:从模型中填充视图,从侦听器更新模型。不要使用监听器来更新视图,调用notifyDataSetcChanged()
将会解决这个问题。