我已经实现了一个自定义列表视图,其中我有两个textview和复选框。这些被封装成线性布局点击我选中/取消选中复选框。我面临的问题是,当我点击线性布局时,它会检查复选框,但它也会检查固定模式上的其他复选框,即每7-8行。我读了很多关于它的问题,发现实现一个视图持有者可以解决问题,但这也不适合我。以下是我的代码:
CustomListAdapter:
public class ContactsListAdapter extends BaseAdapter{
private Context context;
private List<ContactInfo> contacts;
private List<ContactInfo> selectedContacts;
public ContactsListAdapter(Context context, List<ContactInfo> contacts) {
this.context = context;
this.contacts = contacts;
selectedContacts = new ArrayList<ContactInfo>();
}
public List<ContactInfo> getSelectedContacts() {
return selectedContacts;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return contacts.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return contacts.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return Long.parseLong(contacts.get(position).id);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.contacts_list_row_item, null);
holder = new ViewHolder();
holder.view = (LinearLayout) convertView.findViewById(R.id.llContactRow);
holder.name = (TextView) convertView.findViewById(R.id.tvContactName);
holder.number = (TextView) convertView.findViewById(R.id.tvContactNumber);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbMultipleSelect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(contacts.get(position).name);
holder.number.setText(contacts.get(position).number);
holder.view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ContactInfo contact = new ContactInfo();
contact.id = contacts.get(position).id;
contact.name = contacts.get(position).name;
contact.number = contacts.get(position).number;
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact);
} else {
selectedContacts.remove(contact);
}
}
});
return convertView;
}
private static class ViewHolder {
LinearLayout view;
CheckBox checkbox;
TextView name;
TextView number;
}
}
我做错了什么?请帮忙。
修改
在做了@vinitius要求我做的事情之后问题得到了解决,但是当我滚动列表时支票正在消失。这是我的新getView()
:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.contacts_list_row_item, null);
holder = new ViewHolder();
holder.view = (LinearLayout) convertView.findViewById(R.id.llContactRow);
holder.name = (TextView) convertView.findViewById(R.id.tvContactName);
holder.number = (TextView) convertView.findViewById(R.id.tvContactNumber);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbMultipleSelect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(contacts.get(position).name);
holder.number.setText(contacts.get(position).number);
holder.checkbox.setChecked(selectedContacts.contains(contacts.get(position)));
holder.view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ContactInfo contact = new ContactInfo();
contact.id = contacts.get(position).id;
contact.name = contacts.get(position).name;
contact.number = contacts.get(position).number;
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact);
} else {
selectedContacts.remove(contact);
}
}
});
return convertView;
}
答案 0 :(得分:1)
您的问题是您需要将holder.checkbox.setChecked()设置为该行下方的值:
holder.number.setText(contacts.get(position).number);
目前,您在检查它时会更改它的值,但是您需要在调用getview方法时设置该值,否则它将保留之前的任何值。
如果您在contactinfo中有一个“选定”值而不是管理新列表,那可能是最好的。这样你可以做类似的事情:
holder.checkbox.setChecked(contacts.get(position).isSelected);
答案 1 :(得分:1)
您未在getView()
中展示自己的内容。执行以下操作:
@Override
public void onClick(View v) {
//remove the other stuff. You don't want to create new instances here, otherwise you won't be able to remove them later
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact.get(position));
} else {
selectedContacts.remove(contact.get(position));
}
}
在为OnClickListener
设置LinearLayout
之前执行此操作:
holder.checkbox.setChecked(selectedContacts.contains(contacts.get(position)));
如果您在点击CheckBox
时想要相同的行为,只需添加到CheckBox
xml:
android:clickable="false"
所以linearLayout
可以拦截它