您好,当代码命中 notifyDataSetChanged(); 时,我试图覆盖自定义适配器类中的 GetFilter 函数;它产生 IndexOutOfBoundException 这是适配器的代码
public class SelectUserAdapter extends ArrayAdapter<UserWithCheckbox> implements
Filterable {
private ArrayList<UserWithCheckbox> UsersLits;
private Context c;
LayoutInflater vi;
public SelectUserAdapter(Context context, int textViewResourceId,
ArrayList<UserWithCheckbox> countryList) {
super(context, textViewResourceId, countryList);
this.UsersLits = new ArrayList<UserWithCheckbox>();
this.UsersLits.addAll(countryList);
c = this.getContext();
}
private class ViewHolder {
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("Users View", String.valueOf(position));
if (convertView == null) {
vi = (LayoutInflater) this.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.users_select_list_item, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.Name);
holder.name = (CheckBox) convertView.findViewById(R.id.IsSelected);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
UserWithCheckbox User = (UserWithCheckbox) cb.getTag();
Toast.makeText(
c,
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG).show();
User.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
UserWithCheckbox User = UsersLits.get(position);
holder.code.setText(User.getCode());
// holder.name.setText(User.getName());
holder.name.setChecked(User.isSelected());
holder.name.setTag(User);
return convertView;
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<UserWithCheckbox> FilteredResults = new ArrayList<UserWithCheckbox>();
if (constraint != null && constraint.toString().length() > 0) {
for (int index = 0; index < UsersLits.size(); index++) {
UserWithCheckbox User = UsersLits.get(index);
if (User.code.contains(constraint.toString())) {
FilteredResults.add(User);
}
}
results.values = FilteredResults;
results.count = FilteredResults.size();
} else {
synchronized (UsersLits) {
results.values = UsersLits;
results.count = UsersLits.size();
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
UsersLits.clear();
UsersLits = (ArrayList<UserWithCheckbox>) results.values;
notifyDataSetChanged();
}
};
}
}
这是活动代码:
public class SelectUsers extends Activity {
public static SelectUserAdapter dataAdapter = null;
float Long, Lat;
String UID, Radius;
String provider;
ListView listView;
Criteria criteria;
LocationManager locationmanager;
File sdcard = Environment.getExternalStorageDirectory();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_users);
displayListView();
//dataAdapter.checkButtonClick();
EditText FilterText = (EditText)findViewById(R.id.FilterSelectUsers);
FilterText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
dataAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
}
});
}