2012-05-22 110 views
3

我創建一個ListView像這樣一個ArrayAdapter:過濾通過它多線ListView的ArrayAdapter

final ListView lv = (ListView) findViewById(R.id.listView1); 

final EntryAdapter adapter = new EntryAdapter(this, items); 
     lv.setAdapter(adapter); 

和適配器看起來應該是:

this.items = items; 
     vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 



    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder holder; 

     final Item i = items.get(position); 
     if (i != null) { 
      if(i.isSection()){ 
       SectionItem si = (SectionItem)i; 
       v = vi.inflate(R.layout.list_item_section, null); 


       v.setOnClickListener(null); 
       v.setOnLongClickListener(null); 
       v.setLongClickable(false); 

       final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text); 
       sectionView.setText(si.getTitle()); 
      }else{ 
       EntryItem ei = (EntryItem)i; 
       v = vi.inflate(R.layout.carlistdetails, null); 
       holder = new ViewHolder(); 
       final TextView name = (TextView)v.findViewById(R.id.name); 
       final TextView location = (TextView)v.findViewById(R.id.location); 
       final TextView speed = (TextView)v.findViewById(R.id.speed); 
       final ImageView image = (ImageView) v.findViewById(R.id.imageView1); 

       if (name != null) 
        name.setText(ei.name); 
       if(location != null) 
        location.setText(ei.location); 
       if(speedandcontact != null) 
        speed.setText(ei.speed); 
        image.setImageResource(R.drawable.car); 


      } 
     } 
     return v; 
    } 

我有一個每個項目由3個TextViews和1個ImageView組成。 現在我想通過

TextWatcher filterTextWatcher = new TextWatcher() { 

    public void afterTextChanged(Editable s) { 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     adapter.getFilter().filter(s); 
    } 

}; 


EditText filterText = (EditText) findViewById(R.id.sb); 
filterText.addTextChangedListener(filterTextWatcher); 

這是有點兒工作,但給奇怪的結果(大部分的所有項目從列表中消失的時間)來過濾適配器。我只想通過適配器/項目的名稱字段進行過濾。

關於如何做到這一點的任何想法?

+0

http://samir-mangroliya.blogspot.in/2012/05/android-sectioned-listview-with-search_6865.html –

回答

1
this issue as like as from that: http://stackoverflow.com/a/13156885/1752867 
you can try use your custom adapter and filter. 

在代碼:

a: for (int i = 0; i < allObjectItemsArray.size(); i++) { 
       T m = allObjectItemsArray.get(i); 
         // cast m to your class 
       String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName(); 

       if (fistnameAndLastName.toLowerCase().contains(constraint)) { 
        filteredItems.add(m); 
        continue a; 
       } 

      } 

you can modify this line String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName(); 

to 

like : String name = ((your object class) m).getName();