我用一组行做了listview。每行都有一个复选框。如果我单击一个复选框,则还会选中另一个复选框。我会怎么做才能避免这种情况?任何人都知道帮助我..
答案 0 :(得分:2)
为了避免ListView中的CheckBoxes出现问题,您可以在开头使用一个初始化为false的布尔数组,然后在ListView中选中复选框的数组中的相应位置为true。当您在应用程序中向前和向后移动或滚动ListView时,这不会导致复选框的选中状态出现问题。
以下是设置checkboxstate是布尔数组的方法:
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
checkBoxState[pos] = true;
else
checkBoxState[pos] = false;
}
});
然后这会在滚动时检查复选框,状态不会自动更改:
holder.checkbox.setChecked(checkBoxState[pos]);
答案 1 :(得分:1)
这是我的代码:
public class Favourites extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listfavourites);
initcomponents();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
for (int i = 1; i < 20; i++) {
HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put("itemname", "ItemName" + i);
hmap.put("price", "Price");
alist.add(hmap);
}
final CustomListAdapter adapter = new CustomListAdapter(this,
R.layout.listitemfavourites, alist);
list.setAdapter(adapter);
}
private void initcomponents() {
list = (ListView) findViewById(R.id.listfavourites_lst_list);
}
class CustomListAdapter extends ArrayAdapter<HashMap<String, String>> {
Context context;
boolean[] checkBoxState;
int textViewResourceId;
ArrayList<HashMap<String, String>> alist;
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, String>> alist) {
super(context, textViewResourceId);
this.context = context;
this.alist = alist;
this.textViewResourceId = textViewResourceId;
checkBoxState = new boolean[alist.size()];
}
public int getCount() {
return alist.size();
}
public View getView(final int pos, View convertView, ViewGroup parent) {
Holder holder = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.listitemfavourites, parent,
false);
holder = new Holder();
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.listitemfavourites_chk_checkbox);
holder.itemname = (TextView) convertView
.findViewById(R.id.listitemfavourites_txt_itemname);
holder.price = (TextView) convertView
.findViewById(R.id.listitemfavourites_txt_price);
holder.lin_background = (LinearLayout) convertView
.findViewById(R.id.favourites_lin_top);
convertView.setTag(holder);
holder = (Holder) convertView.getTag();
holder.itemname.setText(alist.get(pos).get("itemname"));
holder.price.setText(alist.get(pos).get("price"));
holder.checkbox.setChecked(checkBoxState[pos]);
if (pos == 0) {
holder.lin_background
.setBackgroundResource(R.drawable.bg_celltop);
} else if (pos == (alist.size() - 1) && alist.size() != 1) {
holder.lin_background
.setBackgroundResource(R.drawable.bg_cellbottom);
}
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked())
checkBoxState[pos] = true;
else
checkBoxState[pos] = false;
}
});
return convertView;
}
class Holder {
TextView itemname, price;
CheckBox checkbox;
LinearLayout lin_background;
}
}
}
答案 2 :(得分:0)
这是因为你通过条件回收视图if(view == null)删除它。