我有文本和複選框的列表視圖。滾動列表視圖時,複選框的行爲很奇怪。這似乎是一個回收問題。無論如何,我嘗試了一些類似的帖子中提出的各種選項,但他們都沒有爲我工作。我嘗試設置並獲得其中一個帖子中建議的標籤位置,但運氣不佳。我在列表視圖的活動文件中使用自定義適配器。以下是一些代碼片段; Activity類:ListView滾動複選框問題
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();
}
}
}
}
};
我一直在掙扎了很長一段時間。請指教。謝謝。
看看我的答案。 – Harshid