2012-12-21 53 views
1

我在看這個問題之前先看看this thread。但是該線程包含簡單字符串數組的示例,而在我的情況下,我使用的是ArrayList<Map<String, String>>,所以我現在有點困惑。如何在列表視圖上實現過濾器

我在我的android活動中有多個選擇listview。我正在使用listview來顯示聯繫人列表,以便用戶可以一次選擇多個電話號碼。但是現在我想爲相同的目的添加搜索功能。我想要做的是在頂部放置一個EditText,並且無論用戶輸入哪個字符EditText,我都希望ListView可以根據用戶輸入填充過濾的數據。我有一個CustomAdapter其中BaseAdapter和POJO類名爲Contact我正在使用GET & SET聯繫方式。

MYCUSTOMADAPTER.JAVA

public class MyCustomAdapter extends BaseAdapter { 

Context mContext; 
private LayoutInflater mInflater; 
SparseBooleanArray mSparseBooleanArray; 
private ArrayList<Map<String,String>> mAdapData = new ArrayList<Map<String, String>>(); 

public MyCustomAdapter(Context mContext) { 
    mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mSparseBooleanArray = new SparseBooleanArray(); 
} 

public ArrayList<String> getCheckedItems() { 
    ArrayList<String> mTempArry = new ArrayList<String>(); 
    for (int i = 0; i < mAdapData.size(); i++) { 
     if (mSparseBooleanArray.get(i)) { 
      Map<String, String> map = (Map<String, String>) mAdapData.get(i); 
      final String numbr = map.get("Phone").toString(); 
      mTempArry.add(numbr); 
     } 
    } 
    return mTempArry; 
} 

public int getCount() { 
    return this.mAdapData.size(); 
} 

public void addItem(String paramString1, String paramString2) { 
    Map<String, String> NameNumber = new HashMap<String, String>(); 
    NameNumber.put("Name", paramString1); 
    NameNumber.put("Phone", paramString2); 
    this.mAdapData.add(NameNumber); 
    notifyDataSetChanged(); 
} 

@SuppressWarnings("unchecked") 
public Object getItem(int paramInt) { 
    return (ArrayList<Map<String, String>>) this.mAdapData.get(paramInt); 
} 

public long getItemId(int paramInt) { 
    return paramInt; 
} 

public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup) { 

    if (paramView == null) { 
     paramView = mInflater.inflate(R.layout.multiplecontactview, null); 
    } 

    TextView txtName = (TextView) paramView.findViewById(R.id.txtContactName); 
    TextView txtNumber = (TextView) paramView.findViewById(R.id.txtContactNumber); 
    CheckBox mCheckBox = (CheckBox) paramView.findViewById(R.id.checkBox1); 
    mCheckBox.setTag(paramInt); 
    mCheckBox.setChecked(mSparseBooleanArray.get(paramInt)); 
    mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener); 
    txtName.setTextColor(Color.BLACK); 
    txtNumber.setTextColor(Color.BLACK); 
    for (int i = 0; i < mAdapData.size(); i++) { 
     Map<String, String> map = (Map<String, String>) mAdapData.get(paramInt); 
     final String name = map.get("Name").toString(); 
     final String numbr = map.get("Phone").toString(); 
     txtName.setText(name); 
     txtNumber.setText(numbr); 
    } 
    return paramView; 
} 

OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) { 
     mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked); 
    } 
}; 
} 

Contact類

public class Contact { 

int _id; 
String _name; 
String _phone_number; 

public Contact() { 

} 

public Contact(int id, String name, String _phone_number) { 
    this._id = id; 
    this._name = name; 
    this._phone_number = _phone_number; 
} 

public Contact(int id) { 
    this._id = id; 
} 

public Contact(String name, String _phone_number) { 
    this._name = name; 
    this._phone_number = _phone_number; 
} 

public int getID() { 
    return this._id; 
} 

public void setID(int id) { 
    this._id = id; 
} 

public String getName() { 
    return this._name; 
} 

public void setName(String name) { 
    this._name = name; 
} 

public String getPhoneNumber() { 
    return this._phone_number; 
} 

public void setPhoneNumber(String phone_number) { 
    this._phone_number = phone_number; 
} 

} 

回答

0

你是能夠做到這一點,你需要實現一個Textwatcher

private TextWatcher filterTextWatcher = new TextWatcher() { 

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

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 

    } 

    @Override 
    public void afterTextChanged(Editable arg0) { 
     /*clear previous contents of list*/ 
        adapter.clear(); 
        /*add new contents according to the filter*/ 
        String filter=arg0.getText().toString(); 
        if (filter == null 
          || Pattern 
            .compile(Pattern.quote(filter), 
              Pattern.CASE_INSENSITIVE) 
            .matcher(adapter.getItem(...)).find() 
          || filter.equals("")) { 
         adapter.addItem(...); 
        } 
    } 

}; 

然後將該filterTextWatcher設置爲您的editext:

searchEditText.addTextChangedListener(filterTextWatcher); 

希望有所幫助! :)