我已經在customadapter類中實現了可篩選,並且我能夠通過listview進行搜索,但是當我清空搜索框時,我的列表仍顯示最後一個搜索應用程序,而不是整個應用程序列表。在列表視圖中獲取搜索數據的問題
mainactivity.java
public class MainActivity extends ListActivity {
List<ApplicationInfo> appList;
ListAdapter listAdapter;
PackageManager packageManager;
Process suProcess;
DataOutputStream os;
ApplicationInfo info;
EditText search;
ArrayList<ApplicationInfo> applist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager = getPackageManager();
new LoadApplication().execute();
search = (EditText) findViewById(R.id.searchView);
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
我已經加入我的過濾器類。
private class ListFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
results.values = applist;
results.count = applist.size();
} else {
ArrayList<ApplicationInfo> info = new ArrayList<ApplicationInfo>();
for (ApplicationInfo c : applist) {
if (c.loadLabel(packageManager).toString().toUpperCase().contains(constraint.toString().toUpperCase())) {
info.add(c);
}
}
results.values = info;
results.count = info.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
notifyDataSetInvalidated();
} else {
applist = (List<ApplicationInfo>) results.values;
notifyDataSetChanged();
}
}
}
並覆蓋了getFilter()方法。
public Filter getFilter() {
if (filter == null)
{
filter=new ListFilter();
}
return filter;
}
,因爲我是新來的這...可以請你給我看看一些啓動代碼 – Rahul