我有一个自定义适配器,它由复选框和textview组成。我已经通过EditText实现了过滤,它的工作正常。但当我过滤位置得到改变。例如说我有10个复选框。然后我筛选第9个复选框,并选中复选框,然后它的位置变为0,当我清除EditText时,第一个复选框被选中,而不是第9个复选框。我怎么能保持这个?我的自定义适配器类:
public class CustomAdapter extends ArrayAdapter<Custom> implements Filterable
{
private ArrayList<Custom> entries;
private Activity activity;
private Context context;
private qrusers qrusers;
Custom data;
CustomFilter customFilter;
private String[] udis;
ArrayList<String> userid= new ArrayList<String>();
boolean[] checkBoxState;
private ArrayList<Custom> orignalvalues;
LayoutInflater vi;
public CustomAdapter(Context context, ArrayList<Custom> entries,String []udis,ArrayList<Custom> orignalvalue) {
super(context,0,entries);
this.entries = entries;
this.context= context;
orignalvalues = new ArrayList<Custom>(entries);
this.udis=udis;
checkBoxState=new boolean[entries.size()];
this.qrusers =(qrusers) context;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
public CheckBox chek;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return entries.size();
}
@Override
public Custom getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
final Custom i = entries.get(position);
final Custom t = orignalvalues.get(position);
if (v == null) {
v = vi.inflate(R.layout.checkboxlist, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.contactname);
holder.item2 = (TextView) v.findViewById(R.id.companyname);
holder.chek=(CheckBox)v.findViewById(R.id.checboxlist);
data = getItem(position);
holder.chek.setOnCheckedChangeListener(null); //important
holder.chek.setChecked(data.isChecked());
v.setTag(holder);
}
else{
holder=(ViewHolder)v.getTag();
}
holder.chek.setChecked(checkBoxState[position]);
holder.chek.setTag(udis[position]);
holder.chek.setOnClickListener(new OnClickListener() {
private String s;
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()){
checkBoxState[position]=true;
data.setChecked(true);
// isChecked=true;
s=(String)v.getTag();
Log.e("IDDDDDDDD", s);
userid.add(s);
Log.e("ADDED ID", userid.toString());
}
else{
checkBoxState[position]=false;
s=(String)v.getTag();
userid.remove(s);
Log.e("Removed ID", userid.toString());
}
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("userid", TextUtils.join(",", userid));
editor.commit();
}
}
);
Custom ei = (Custom)i;
if (ei != null) {
holder.item1.setText(ei.getcustomBig());
holder.item2.setText(ei.getcustomSmall());
}
return v;
}
public Filter getFilter() {
if (customFilter == null) {
customFilter = new CustomFilter();
}
return customFilter;
}
public ArrayList<String> getUserid() {
return userid;
}
public void setUserid(ArrayList<String> userid) {
this.userid = userid;
}
private class CustomFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint == null || constraint.length() == 0) {
entries = new ArrayList<Custom>(orignalvalues);
results.values=entries;
results.count = entries.size();
}
else{
ArrayList<Custom> newValues = new ArrayList<Custom>();
for (Custom i : entries){
if (i.toString().toUpperCase().startsWith(constraint.toString().toUpperCase()))
newValues.add(i);
}
results.count = newValues.size();
results.values = newValues;
Log.e("NEW VALUES", newValues.toString());
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results.count == 0){
notifyDataSetInvalidated();
} else {
Log.e("publish result", results.toString());
@SuppressWarnings("unchecked")
ArrayList<Custom> listi = (ArrayList<Custom>)results.values;
ArrayList<Custom> itemsList = new ArrayList<Custom>(listi);
entries =itemsList;
notifyDataSetChanged();
}
}
}
}
答案 0 :(得分:0)
不要将checkBoxState用作独立数组 - 尤其是在视图发生变化时。将checkBoxState保留在持有者中。