我有一个 EditText 和一个 ListView。ListView 中的项目包含两个 TextView。如果用户在 EditText 中输入内容,我需要过滤 ListView。我的适配器:
public class AdapterUserIsp extends ArrayAdapter{
public static ArrayList<ItemUserIsp> data = new ArrayList<ItemUserIsp>();
Context context;
int ch = 0;
public static TextView header4;
@SuppressWarnings("unchecked")
public AdapterUserIsp(Context context, int textViewResourceId, ArrayList<ItemUserIsp> arr) {
super(context, textViewResourceId, arr);
if (arr != null) {
data = arr;
}
this.context = context;
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int num) {
// TODO Auto-generated method stub
return data.get(num);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@SuppressLint("NewApi")
@Override
public View getView(int i, View someView, ViewGroup arg2) {
LayoutInflater inflater = LayoutInflater.from(context);
if (someView == null) {
someView = inflater.inflate(R.layout.item_user_isp, arg2, false);
}
TextView header1 = (TextView) someView.findViewById(R.id.tvUserNameIsp);
TextView header2 = (TextView) someView.findViewById(R.id.tvUserPositionIsp);
header1.setText(data.get(i).header1);
header2.setText(data.get(i).header2);
return someView;
}
}
我的活动:
final ListView lv = (ListView)findViewById(R.id.users);
final AdapterUserIsp adapter = new AdapterUserIsp(this,R.id.users,data);
final EditText search= (EditText)findViewById(R.id.search);
search_isp.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
adapter.getFilter().filter(s);
lv.invalidateViews();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
但它不起作用。为什么?谢谢!