我正在尝试使用列表视图的每个项目和联系人姓名中的复选框创建联系人列表。我写过一个按联系人姓名搜索的过滤器。 问题是,当我过滤一些名称,然后检查该项目,然后删除或删除过滤器中的一些文本,然后禁用我之前检查的项目的复选框,基本上创建一个新的视图。
public class ContactAdapter extends BaseAdapter implements Filterable{
Context context;
ArrayList<ContactItem> contactlist;
ArrayList<ContactItem> mStringFilterList;
ValueFilter valueFilter;
public ContactAdapter(Context context , ArrayList<ContactItem> contactlist) {
this.context = context;
this.contactlist = contactlist;
mStringFilterList = contactlist;
}
@Override
public int getCount() {
return contactlist.size();
}
@Override
public Object getItem(int position) {
return contactlist.get(position);
}
@Override
public long getItemId(int position) {
return contactlist.indexOf(getItem(position));
}
private class ViewHolder {
TextView name;
CheckBox selection;
TextView phoneNumber;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adapter_item_contact,null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.phoneNumber = (TextView) convertView.findViewById(R.id.tv_number);
holder.selection = (CheckBox) convertView.findViewById(R.id.cb_contact);
convertView.setTag(holder);
holder.selection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
ContactItem item = (ContactItem) cb.getTag();
item.setSelected(cb.isChecked());
}
});
}else{
holder = (ViewHolder) convertView.getTag();
}
ContactItem contact = contactlist.get(position);
holder.name.setText(contact.getName());
holder.phoneNumber.setText(contact.getPhoneNumber());
holder.selection.setTag(contact);
holder.selection.setChecked(contact.isSelected());
return convertView;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<ContactItem> filterList = new ArrayList<ContactItem>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ( (mStringFilterList.get(i).getName().toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
ContactItem contact = new ContactItem(mStringFilterList.get(i)
.getName() , mStringFilterList.get(i)
.getPhoneNumber() , mStringFilterList.get(i)
.isSelected());
filterList.add(contact);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
synchronized(this){
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
contactlist = (ArrayList<ContactItem>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:2)
Sandesh每次调用视图时我都会存储项目状态,您可以通过在复选框上使用setTag和getTag来查看。 我现在解决了这个问题。 问题是每次调用过滤器时我都在创建新的联系人项目,因此之前的联系人项目已消失。
问题代码在
之下private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<ContactItem> filterList = new ArrayList<ContactItem>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ( (mStringFilterList.get(i).getName().toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
ContactItem contact = new ContactItem(mStringFilterList.get(i)
.getName() , mStringFilterList.get(i)
.getPhoneNumber() , mStringFilterList.get(i)
.isSelected());
filterList.add(contact);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
synchronized(this){
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
}
return results;
}
解决了代码。我必须添加与filterlist中的mStringFilterList相同的过滤器对象。
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<ContactItem> filterList = new ArrayList<ContactItem>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ( (mStringFilterList.get(i).getName().toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
// ContactItem contact = new ContactItem(mStringFilterList.get(i)
// .getName() , mStringFilterList.get(i)
// .getPhoneNumber() , mStringFilterList.get(i)
// .isSelected());
filterList.add(mStringFilterList.get(i));
}
}
results.count = filterList.size();
results.values = filterList;
} else {
synchronized(this){
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
contactlist = (ArrayList<ContactItem>) results.values;
notifyDataSetChanged();
}
}
答案 1 :(得分:0)
每次在listView中发生一些更改,例如滚动列表时,每次都会调用getView方法。因此,您可以在列表中检查列表中的哪些项目,并在getView方法中检查列表中存在的项目。
或者您可以这样做:检查14.2。教程:下面链接中的域模型和行交互