2011-11-04 62 views
2

正如標題所解釋的,我在過濾/搜索列表視圖時遇到了問題。當我嘗試搜索讓我們說「A」時,listView顯示A-31兩次,A-32兩次等等。有人有一個想法,爲什麼發生這種情況?Android,ListView搜索/過濾 - >重複條目

package hpliu.grupp2; 

import android.content.Intent; 
import android.os.Bundle; 
import android.os.Debug; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.widget.TableRow; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import android.app.ListActivity; 
import android.app.Activity; 

import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

public class SearchRoomActivity extends ListActivity { 
    private EditText filterText = null; 
    SimpleAdapter menu = null; 
    ListView lv = null; 

    static final String[] AHalls = new String[] { "A-31", "A-32", "A-33", 
      "A-34", "A-35", "A-36", "A-37" }; 

    String[] from = new String[] { "roomName", "buildingName" }; 

    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.search_room); 

     createCampusList("Campus Valla, Linköping", 
       createHashMap(AHalls, "A Huset")); 

     // filterText = (EditText) findViewById(R.id.search_box); 
     // filterText.addTextChangedListener(filterTextWatcher); 
    } 

    private TextWatcher filterTextWatcher = new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
     } 

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

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

    }; 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     try { 
      @SuppressWarnings("unchecked") 
      HashMap<String, String> toHash = (HashMap<String, String>) this 
        .getListAdapter().getItem(position); // {text1 = "A-31", 
                  // text2 = "Herp"}; 

      Toast.makeText(
        this, 
        "Searching for... " + " " 
          + toHash.get("roomName").toString(), 
        Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public List<HashMap<String, String>> createHashMap(String[] Halls, 
      String Building) { 
     List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 
     for (int i = 0; i < Halls.length; i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put(from[0], Halls[i]); 
      map.put(from[1], Building); 
      fillMaps.add(map); 
     } 
     return fillMaps; 
    } 

    public List<HashMap<String, String>> createHashMap(String[] Halls, 
      String[] Buildings) { 
     List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 
     for (int i = 0; i < Halls.length; i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put(from[0], Halls[i]); 
      map.put(from[1], Buildings[i]); 
      fillMaps.add(map); 
     } 
     return fillMaps; 
    } 

    public void createCampusList(String campusHeader, 
      List<HashMap<String, String>> fillMaps) { 

     int[] to = new int[] { R.id.roomName, R.id.buildingName }; 

     menu = new SimpleAdapter(this, fillMaps, R.layout.list_item, from, to); 
     setListAdapter(menu); 
     lv = getListView(); 
     lv.setTextFilterEnabled(true); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     filterText.removeTextChangedListener(filterTextWatcher); 
    } 
} 

回答

2

如果過濾每個項目中有兩個字符串的適配器,過濾器將通過適配器兩次。這就是爲什麼你有多個。如果你有三個字符串,你可能會得到三個重複的項目。

我在網上找到的一個解決方案是創建一個自定義SimpleAdapter並在填充ListView之前檢查重複項。

0

我的自定義列表視圖面臨同樣的問題。

listview的超類不知道你已經在過濾後改變了大小。這意味着getCount()將返回數組的原始大小,這比您的過濾數組大得多。所以你必須重寫getCount()函數來返回已過濾數組的大小,而不是原始數組。之後,重複的項目消失了。

@Override  
public int getCount() { 
    return filter_array.size(); 
    } 
@Override 
    public Object getItem(int position) { 
    return filter_array.get(position); 
    }