我从我的应用程序中获取了联系人并在列表视图中显示了它。我想在listview上添加一个搜索栏。我曾尝试过以下代码。但它没有正常工作。这是我正在处理的代码:
et.addTextChangedListener(new TextWatcher() {
@SuppressLint("ShowToast")
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Toast.makeText(context, cs.toString()+"", Toast.LENGTH_SHORT).show();
new MyBaseAdapter(context, myList).getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
这是getFiletr方法:
public Filter getFilter() {
// TODO Auto-generated method stub
Filter filter=new Filter(){
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
Toast.makeText(context, "inside publish", Toast.LENGTH_SHORT).show();
Log.d("inside publish", "inside publish");
mDisplayedValues = (ArrayList<ListData>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults results = new FilterResults();
ArrayList<ListData> FilteredArrList = new ArrayList<ListData>();
Toast.makeText(context, "inside perform", Toast.LENGTH_SHORT).show();
Log.d("insde perform", "inside perform");
if (mOriginalValues == null) {
Toast.makeText(context, "original data is null", Toast.LENGTH_SHORT).show();
Log.d("original data is null", "original data is null");
mOriginalValues = new ArrayList<ListData>(mDisplayedValues); // saves the original data in mOriginalValues
}
if (constraint == null || constraint.length() == 0) {
Toast.makeText(context, "constraint is null", Toast.LENGTH_SHORT).show();
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
}
else {
Toast.makeText(context, "constraint not null", Toast.LENGTH_SHORT).show();
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i).title;
if (data.toLowerCase().startsWith(constraint.toString())) {
ListData ld=new ListData();
ld.setTitle(mOriginalValues.get(i).title);;
ld.setDescription(mOriginalValues.get(i).Description);
ld.setImg(mOriginalValues.get(i).image);
FilteredArrList.add(ld);
Toast.makeText(context, "added"+i, Toast.LENGTH_SHORT).show();
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
Toast.makeText(context, "results stored", Toast.LENGTH_SHORT).show();
}
Toast.makeText(context, "before returning results", Toast.LENGTH_SHORT).show();
Log.d("before returning results","before returning results");
return results;
}
};
Toast.makeText(context, "before returning filter", Toast.LENGTH_SHORT).show();
Log.d("before returning filter","before returning filter");
return filter;
}
请帮忙
答案 0 :(得分:1)
尝试这个我在我的应用程序中使用此代码并且它工作正常。
挑衅地帮助你。
SContactListBox.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
textlength = SContactListBox.getText().length();
array_sort.clear();
for (int i = 0; i < Contact_NameList.size(); i++)
{
if (textlength <= Contact_NameList.get(i).length())
{
if((boolean)
Contact_NameList.get(i).contains(SContactListBox.getText().toString()))
{
array_sort.add(Contact_NameList.get(i));
check=false;
}
}
}
ClistAdapter = new ContactListAdapter(ContactListActivity.this, array_sort);
listContacts.setAdapter(ClistAdapter);
}
});