2012-11-28 42 views
0

我從Database獲取的數據填充ListView。我正在使用CustomAdapter,它擴展了BaseAdapter以填充數據的ListViewListView的每一行包含2個TextView和1個CheckBox。現在我想要做的是在Activity的頂部放置一個EditText,當用戶鍵入例如J的文本時,ListView應該顯示所有以J開頭的聯繫人。請引導我。從列表視圖實施搜索過濾器從數據庫填充

FOR MYCUSTOMADAPTER

public class MyCustomAdapter extends BaseAdapter { 

Context mContext; 
private LayoutInflater mInflater; 
SparseBooleanArray mSparseBooleanArray; 
private ArrayList<String> mData = new ArrayList<String>(); 
private ArrayList<String> mNumber = new ArrayList<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 < mData.size(); i++) { 
     if (mSparseBooleanArray.get(i)) { 
      //mTempArry.add(mData.get(i)); 
      mTempArry.add(mNumber.get(i)); 
     } 
    } 
    return mTempArry; 
} 

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

public void addItem(String paramString1, String paramString2) { 
    this.mData.add(paramString1); 
    this.mNumber.add(paramString2); 
    notifyDataSetChanged(); 
} 

public Object getItem(int paramInt) { 
    return (String) this.mData.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 < mData.size(); i++) { 
     txtName.setText(mData.get(paramInt).toString()); 
     txtNumber.setText(mNumber.get(paramInt).toString()); 
    } 
    return paramView; 
} 

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

public static class ViewHolder { 
    public CheckBox cb; 
    public TextView nameView; 
    public TextView numberView; 
} 

} 

代碼代碼填充LISTVIEW隨着數據的OnCreate()

ArrayList<Contact> contacts = db.getAllContacts(); 
     for (Contact cn : contacts) { 
      mAdapter.addItem(cn.getName(), cn.getPhoneNumber()); 
     } 
     mAdapter.notifyDataSetChanged(); 
     mListView.setAdapter(mAdapter); 
+0

http://stackoverflow.com/questions/13090046/how-to-implement-search-in-customlistview-based-on-class-item-of-pojo-class-in-a。此鏈接可能對您有所幫助。 – Raghunandan

+0

您建議的鏈接由customadapter擴展arrayadapter的示例組成,而我正在擴展baeseadapter。 –

+0

鏈接就像參考而不是實際的代碼。你有你自己的方式實施。至少你有一個想法開始。對於答案你可以看到下面的答案。我只是建議,所以我把它放在評論中。 – Raghunandan

回答