2012-05-22 3 views
0

사용자 지정 개체로 배열 어댑터를 필터링하는 데 문제가 있습니다. 내가 owerride 사용자 정의 개체 toString() 그래서 그것은 필터를 원하는 이름을 반환합니다. 문제는 올바른 개수의 항목이지만 잘못된 항목이있는 것입니다. 예 : 아이템 a1, a2, b1, b2가있는 목록이 있습니다. 내가 필터에 들어가면 a1, a2가 보입니다. 다음은 어댑터 코드입니다. 나는 문제가있는 것 같아요 :ArrayAdapter 필터가 잘못된 항목을 반환합니다.

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; 
} 

} 여기

필터 설정 및 호출 할 수 있습니다 :

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); 
+0

당신이 필터를 교체 할 수 있도록 getFilter 메소드를 오버라이드 (override) 할 필요가 있다고 생각 행동과 자신의 필터링을 만들 –

답변

1
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); 

       } 
      } 
     }); 
    } 
+0

잘 작동합니다. 감사합니다 – vandzi

+0

@ vandzi 환영합니다, 귀하의 웹 사이트 ... –

관련 문제