我有带有文本和复选框的列表视图。滚动列表视图时复选框的行为很奇怪。这似乎是一个回收问题。无论如何,我尝试了一些类似帖子中建议的各种选项,但没有一个对我有用。我尝试按照其中一篇文章的建议设置并获取标签位置,但运气不好。我在列表视图的活动文件中使用自定义适配器。以下是一些代码片段;活动类:
lv = (ListView) findViewById(R.id.itemdisplay_listview);
lv.setAdapter(new CustomAdapter(this, itemNames, tableNo));
自定义适配器:
private class CustomAdapter extends BaseAdapter {
private List<String> mListItems;
private LayoutInflater mLayoutInflater;
ViewHolder holder;
int a = 1;
public CustomAdapter(Context context, List<String> arrayList,
String table) {
tableNumber = table;
mListItems = arrayList;
// get the layout inflater
mLayoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListItems.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView itemName, quantity;
CheckBox checkbox;
ImageButton addBtn, minusBtn;
}
@Override
public View getView(int position, View convertview, ViewGroup viewGroup) {
// create a ViewHolder reference
// check to see if the reused view is null or not, if is not null
// then
// reuse it
if (convertview == null) {
holder = new ViewHolder();
convertview = mLayoutInflater.inflate(
R.layout.itemdisplay_list, null);
holder.itemName = (TextView) convertview
.findViewById(R.id.itemdisplaylist_name);
holder.addBtn = (ImageButton) convertview
.findViewById(R.id.itemdisplaylist_add);
holder.minusBtn = (ImageButton) convertview
.findViewById(R.id.itemdisplaylist_minus);
holder.quantity = (TextView) convertview
.findViewById(R.id.itemdisplaylist_quantity);
holder.checkbox = (CheckBox) convertview
.findViewById(R.id.itemdisplaylist_cbox);
// holder.checkbox.setTag(position);
// the setTag is used to store the data within this view
convertview.setTag(holder);
} else {
// the getTag returns the viewHolder object set as a tag to the
// view
holder = (ViewHolder) convertview.getTag();
}
holder.checkbox.setOnClickListener(checkListener);
doneBtn.setEnabled(true);
holder.minusBtn.setImageResource(R.drawable.minus);
holder.quantity.setText(String.valueOf(a));
holder.addBtn.setImageResource(R.drawable.add);
holder.addBtn.setOnClickListener(addBtnClick);
holder.minusBtn.setOnClickListener(minusBtnClick);
String stringItem = mListItems.get(position);
if (stringItem != null) {
if (holder.itemName != null) {
// set the item name on the TextView
holder.itemName.setText(stringItem);
}
}
// this method must return the view corresponding to the data at the
// specified position.
return convertview;
}
private OnClickListener checkListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*doneBtn.setOnClickListener(doneBtnClick);*/
View view = (View) v.getParent();
CheckBox cb = (CheckBox) view
.findViewById(R.id.itemdisplaylist_cbox);
TextView tv = (TextView) view
.findViewById(R.id.itemdisplaylist_name);
TextView tv1 = (TextView) view
.findViewById(R.id.itemdisplaylist_quantity);
if (cb.isChecked()) {
map.put(tv.getText().toString(), tv1.getText().toString());
} else {
for (Iterator<Map.Entry<String, String>> it = map
.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, String> entry = it.next();
String item = entry.getKey();
if ((tv.getText().toString()).equals(item)) {
it.remove();
}
}
}
}
};
我已经挣扎了很长时间。请指教。谢谢。