我正面临这个问题,就是当我检查一个项目并在此之后搜索名称时,该复选框变为未选中状态。
我查了希瑟
我搜索后未经检查
我如何确保搜索后仍然检查项目?
CustomAdapter.java
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter<Contact> implements Filterable {
private final Activity context;
private ArrayList<Contact> contacts;
ArrayList<Contact> stringFilterList;
ValueFilter valueFilter;
private static class ViewHolder {
TextView name;
TextView phoneno;
CheckBox checkBox;
ImageView photo;
}
public CustomAdapter(Activity context, ArrayList<Contact> contacts) {
super(context, R.layout.custom, contacts);
//super(context, R.layout.custom, contacts);
this.context = context;
this.contacts = contacts;
this.stringFilterList = contacts;
}
@Override
public int getCount() {
return contacts.size();
}
/*@Override
public String getItem(int position) {
return contacts.get(position).getName();
}
@Override
public long getItemId(int position) {
return contacts.indexOf(getItem(position));
}*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.custom, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.phoneno = (TextView) convertView.findViewById(R.id.phoneno);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
viewHolder.photo = (ImageView) convertView.findViewById(R.id.photo);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
contacts.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
//stringFilterList.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.name, viewHolder.name);
convertView.setTag(R.id.phoneno, viewHolder.phoneno);
convertView.setTag(R.id.photo, viewHolder.photo);
convertView.setTag(R.id.checkBox, viewHolder.checkBox);
}
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.checkBox.setTag(position); // This line is important.
viewHolder.name.setText(contacts.get(position).getName());
viewHolder.phoneno.setText(contacts.get(position).getPhoneNo());
viewHolder.photo.setImageResource(R.drawable.photo);
viewHolder.checkBox.setChecked(contacts.get(position).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<Contact> filterList = new ArrayList<Contact>();
for (int i = 0; i < stringFilterList.size(); i++) {
if ((stringFilterList.get(i).getName().toUpperCase()).contains(constraint.toString().toUpperCase())) {
Contact contact = new Contact(stringFilterList.get(i).getName(), stringFilterList.get(i).getPhoneNo());
filterList.add(contact);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = stringFilterList.size();
results.values = stringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
contacts = (ArrayList<Contact>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:1)
更新您的ValueFilter,如下所示
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Contact> filterList = new ArrayList<Contact>();
for (int i = 0; i < stringFilterList.size(); i++) {
if ((stringFilterList.get(i).getName().toUpperCase()).contains(constraint.toString().toUpperCase())) {
Contact contact = new Contact(stringFilterList.get(i).getName(), stringFilterList.get(i).getPhoneNo(),**stringFilterList.get(i).isSelected**);
filterList.add(contact);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = stringFilterList.size();
results.values = stringFilterList;
}
return results;
}
将isSelected值添加到新的联系人列表中。
答案 1 :(得分:0)
搜索后,当前视图将等于位置0.让我们看看
contacts.get(position).isSelected()
返回true或false。