嗨,我是Android的新手,并使用复选框进行自定义列表视图,因为我在滚动时面临随机选择的另一个列表项复选框。我已经完成了一些线程,但没有解决我的问题。请帮我解决这个问题。这是我的自定义适配器
CustomAdapter.java
public class CustomAdapter extends BaseAdapter{
private boolean[] checkBoxState;
private final Activity context;
public final ArrayList<SelectedListModel>list;
private SelectedListModel element;
private ArrayList<SelectedListModel> positions = new ArrayList<SelectedListModel>();
public CustomAdapter(Activity applicationContext,
ArrayList<SelectedListModel> contacts) {
// TODO Auto-generated constructor stub
this.context = applicationContext;
this.list = contacts;
this.checkBoxState=new boolean[contacts.size()];
}
static class ViewHolder {
protected TextView name,number;
protected CheckBox checkbox;
}
public int getCount() {
// TODO Auto-generated method stub
System.out.println("list size:"+list.size());
return list.size();
}
public ArrayList<SelectedListModel> getSelectedItemList() {
// TODO Auto-generated method stub
return positions;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row_listview, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.number = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
viewHolder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
element = (SelectedListModel)viewHolder.checkbox.getTag();
if(buttonView.isChecked()==true){
checkBoxState[position] = buttonView.isChecked();
element.setSelected(checkBoxState[position]);
positions.add(element);
}else{
checkBoxState[position] = buttonView.isChecked();
element.setSelected(checkBoxState[position]);
positions.remove(element);
}
}
});
viewHolder.checkbox.setTag(list.get(position));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) convertView.getTag();
setNameAndNumber(list.get(position),holder,position);
return convertView;
}
private void setNameAndNumber(SelectedListModel selectedListModel,ViewHolder holder, int position)
{
// TODO Auto-generated method stub
holder.name.setText(selectedListModel.getName());
holder.number.setText(selectedListModel.getNumber());
holder.checkbox.setSelected(selectedListModel.isSelected());
}
}
答案 0 :(得分:2)
您正在设置&#34;已选择&#34;状态但不是&#34;检查&#34;州:
holder.checkbox.setSelected(selectedListModel.isSelected());
holder.checkbox.setChecked(selectedListModel.isSelected());
答案 1 :(得分:0)
mViewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1);
mViewHolder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (cb.isChecked()) {
checkBoxArrayList.set(position, true);
} else if (!cb.isChecked()) {
checkBoxArrayList.set(position, false);
}
}
});
您好,以下链接可能会对您有所帮助http://mylearnandroid.blogspot.in/2014/06/listview-problem-while-scrolling.html