ArrayAdapter过滤器返回错误的项目 [英] ArrayAdapter filter return wrong items
本文介绍了ArrayAdapter过滤器返回错误的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义对象过滤阵列适配器问题。我owerride自定义对象的toString(),所以它返回的名字 - 这是我想要的过滤器。问题是,它编报项目,但错误的物品计数正确。例如。我有项目A1,A2,B1,B2列表。当我输入B检查过滤和我看到的A1,A2。这里是我的适配器code。我猜问题是有:
类CustomerAdapter扩展ArrayAdapter<客户> {私人LayoutInflater mInflater;
私人列表<客户>顾客;公共CustomerAdapter(上下文的背景下,INT textViewResourceId,
清单<客户>顾客) {
超(背景下,textViewResourceId,客户);
mInflater = LayoutInflater.from(上下文);
this.customers =客户;
}@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
ViewHolder持有人; 如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.customers_list_item,NULL); 持有人=新ViewHolder();
holder.text =(TextView中)convertView
.findViewById(R.id.textViewCustomerName);
holder.icon =(LinearLayout中)convertView
.findViewById(R.id.linearLayoutCustomersListEdit); convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
} //与支架有效地结合的数据。
holder.text.setText(customers.get(位置).getCustomerName()); 返回convertView;
}静态类ViewHolder {
TextView的文本;
的LinearLayout图标;
}
}
下面是过滤器的设置和调用:
customerAdapter =新CustomerAdapter(这一点,
R.layout.customers_list_item,repository.getCustomers());
setListAdapter(customerAdapter);etSearch =(EditText上)findViewById(R.id.editText_customers_search);
etSearch.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence中,INT ARG1,ARG2 INT,
INT ARG3){
。customerAdapter.getFilter()过滤(s.toString());
} @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,
INT ARG2,诠释ARG3){
} @覆盖
公共无效afterTextChanged(编辑为arg0){
}
}); getListView()setTextFilterEnabled(真)。
解决方案
列表<客户>客户=新的List<客户>();
清单<客户> tempcustomers =新的List<客户>();
客户= repository.getCustomers();
etSearch .addTextChangedListener(新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
// TODO自动生成方法存根
的System.out.println(onTextChanged重点presed ......+ S);
} @覆盖
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
INT后){
// TODO自动生成方法存根
的System.out.println(beforeTextChanged重点presed ......+ filterText.getText()); } @覆盖
公共无效afterTextChanged(编辑S){
// TODO自动生成方法存根
的System.out.println(afterTextChanged重点presed ......+ filterText.getText());
如果(!etSearch .getText()。的toString()。equalsIgnoreCase()){
tempcustomers =新的List<客户>();
字符串文本= etSearch .getText()的toString()。 的for(int i = 0; I<客户.size();我++){ 如果(客户获得(一).toUpperCase()。toString()方法。包括(text.toUpperCase())){
tempcustomers。新增(客户获得(一)); }
} }
customerAdapter =新CustomerAdapter(这一点,
R.layout.customers_list_item,tempcustomers);
setListAdapter(customerAdapter); }其他{
customerAdapter =新CustomerAdapter(这一点,
R.layout.customers_list_item,客户);
setListAdapter(customerAdapter); }
}
});
}
I have problem with filtering array adapter with custom object. I owerride custom object toString() so it returns name- which I want filter. Problem is that it filer correct count of items but wrong items. E.g. I have list with items a1,a2, b1,b2. When I enter b it filter and I see a1,a2. Here is my adapter code. I guess problem is there:
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;
}
}
Here is filter setup and call:
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);
}
}
});
}
这篇关于ArrayAdapter过滤器返回错误的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文