Android ArrayAdapter筛选器返回错误的项目
我在使用自定义对象筛选阵列适配器时遇到问题。我将自定义对象转换为字符串(),这样它将返回名称-我想要的过滤器。问题是,it文件管理器对项目计数正确,但项目计数错误。例如,我的清单上有a1、a2、b1、b2项。当我进入b时,它会过滤,我看到a1,a2。这是我的适配器代码。我想问题在于:Android ArrayAdapter筛选器返回错误的项目,android,Android,我在使用自定义对象筛选阵列适配器时遇到问题。我将自定义对象转换为字符串(),这样它将返回名称-我想要的过滤器。问题是,it文件管理器对项目计数正确,但项目计数错误。例如,我的清单上有a1、a2、b1、b2项。当我进入b时,它会过滤,我看到a1,a2。这是我的适配器代码。我想问题在于: class CustomerAdapter extends ArrayAdapter<Customer> { private LayoutInflater mInflater; private Lis
class CustomerAdapter extends ArrayAdapter<Customer> {
private LayoutInflater mInflater;
private List<Customer> customers;
public CustomerAdapter(Context context, int textViewResourceId,
List<Customer> customers) {
super(context, textViewResourceId, customers);
mInflater = LayoutInflater.from(context);
this.customers = customers;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customers_list_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.textViewCustomerName);
holder.icon = (LinearLayout) convertView
.findViewById(R.id.linearLayoutCustomersListEdit);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(customers.get(position).getCustomerName());
return convertView;
}
static class ViewHolder {
TextView text;
LinearLayout icon;
}
列出客户=新建列表();
List tempcustomers=新列表();
customers=repository.getCustomers();
etSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
//TODO自动生成的方法存根
System.out.println(“onTextChanged Key presed…”+s);
}
@凌驾
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
System.out.println(“beforeTextChanged Key presed…”+filterText.getText());
}
@凌驾
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
System.out.println(“PostTextChanged Key presed…”+filterText.getText());
如果(!etSearch.getText().toString().equalsIgnoreCase(“”)){
tempcusters=新列表();
String text=etSearch.getText().toString();
对于(int i=0;i
我认为您应该重写getFilter方法,以便能够更改筛选器行为并进行自己的筛选
customerAdapter = new CustomerAdapter(this,
R.layout.customers_list_item, repository.getCustomers());
setListAdapter(customerAdapter);
etSearch = (EditText) findViewById(R.id.editText_customers_search);
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
customerAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
getListView().setTextFilterEnabled(true);
List<Customer> customers = new List<Customer>();
List<Customer> tempcustomers = new List<Customer>();
customers = repository.getCustomers();
etSearch .addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
System.out.println("onTextChanged Key presed..."+s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
System.out.println("beforeTextChanged Key presed..."+filterText.getText());
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("afterTextChanged Key presed..."+filterText.getText());
if (!etSearch .getText().toString().equalsIgnoreCase("")){
tempcustomers = new List<Customer>();
String text = etSearch .getText().toString();
for(int i=0 ;i< customers .size();i++){
if(customers .get(i).toUpperCase().toString().contains(text.toUpperCase())){
tempcustomers .add(customers .get(i));
}
}
}
customerAdapter = new CustomerAdapter(this,
R.layout.customers_list_item,tempcustomers );
setListAdapter(customerAdapter);
}else{
customerAdapter = new CustomerAdapter(this,
R.layout.customers_list_item,customers );
setListAdapter(customerAdapter);
}
}
});
}