如何在android中的自定义listview中应用过滤器?
在我的android应用程序中,我想在listview上应用一个过滤器。列表的每一行显示联系人姓名、联系人号码类型、联系人号码等。 如何在列表上应用过滤器,以便在编辑文本中输入人员姓名时,必须在文本更改时刷新列表视图?我正在使用另一个类填充列表 这是我的代码。 AllContactsActivity类从手机中检索联系人如何在android中的自定义listview中应用过滤器?,android,listview,filter,custom-lists,Android,Listview,Filter,Custom Lists,在我的android应用程序中,我想在listview上应用一个过滤器。列表的每一行显示联系人姓名、联系人号码类型、联系人号码等。 如何在列表上应用过滤器,以便在编辑文本中输入人员姓名时,必须在文本更改时刷新列表视图?我正在使用另一个类填充列表 这是我的代码。 AllContactsActivity类从手机中检索联系人 public class AllContactsActivity extends ListActivity implements android.view
public class AllContactsActivity extends ListActivity implements
android.view.View.OnClickListener, OnItemClickListener {
EditText sc;
String name, phonetype;
ImageButton favourites, contacts, keypad, recent, about;
int arr, key;
Cursor tcur;
int[] typecount, count, id;
ListView lv;
ListViewAdapterContacts lva;
String[] names, numbers, typeinfo, contactinfo, types;
Integer[] sortlist;
TreeMap<Integer, String> sorted_set;
LinkedHashMap<Integer, String> sortedMap;
ContentResolver tcr;
HashMap<Integer, String> numbhashmap;
ArrayAdapter<String> adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater layoutInflater = getLayoutInflater();
mainLayout.addView(layoutInflater.inflate(R.layout.allcontacts, null));
mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
this.addContentView(mainLayout, params);
//configureBottomMenu();
getphones();//get the cantact number
getContacts();//get contact name,type,id
//names,types,number,id are populated by above two methods.
lv = new ListView(getApplicationContext());
lv = (ListView) findViewById(android.R.id.list);
//ListViewAdapterContacts class is specified later in the code which populate the list view
lva = new ListViewAdapterContacts(this, names, types, numbers, id);
lv.setAdapter(lva);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(this);
sc=(EditText)findViewById(R.id.searchcontact);
sc.addTextChangedListener(textwatcher);
}// on create
@Override
protected void onDestroy() {
super.onDestroy();
sc.removeTextChangedListener(textwatcher);
}
private TextWatcher textwatcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
AllContactsActivity.this.lva.getFilter().filter(s.toString());
lva.notifyDataSetChanged();
}
};
}
重写getFilter()并将其放入:
public Filter getFilter() {
return new MyCustomFilter();
}
当然,您必须创建自定义过滤器
private class MyCustomFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
// perform your filtering. you'll need to create a new List to get the correct filtered results. then
results.values = (your new List);
results.count = (your new List).size();
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List filtered = (ArrayList)results.values;
// set the data of your adapter here
if(results.count>0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
重写getFilter()并将其放入:
public Filter getFilter() {
return new MyCustomFilter();
}
当然,您必须创建自定义过滤器
private class MyCustomFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
// perform your filtering. you'll need to create a new List to get the correct filtered results. then
results.values = (your new List);
results.count = (your new List).size();
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List filtered = (ArrayList)results.values;
// set the data of your adapter here
if(results.count>0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
我无法理解你所说的新列表的含义。我在这门课上没有任何列表,我只有数组。我需要把数组放到列表中吗?不,数组很好。但是我不确定是否可以将FilterResults返回数组。可能需要两步转换(FilterResults到arraylist,arraylist到arraylist),我应该在->results.values=(您的新列表)行中放置什么;results.count=(您的新列表).size();我没有得到你。请详细说明。我发现非常有用。我无法理解你所说的新列表的含义。我在这门课上没有任何列表,我只有数组。我需要把数组放到列表中吗?不,数组很好。但是我不确定是否可以将FilterResults返回数组。可能需要两步转换(FilterResults到arraylist,arraylist到arraylist),我应该在->results.values=(您的新列表)行中放置什么;results.count=(您的新列表).size();我没有得到你。请详细说明。我发现非常有用。