我想添加一个过滤ListView
的搜索功能并仅显示结果,因此我添加了EditText
并添加了TextWatcher
并编写了此代码{{1但它不起作用它说“无法解析getFilter方法”我确实得到了这样的事实:我的BaseAdapter中没有MainActivity.this.adapter.getFilter().filter(cs);
方法,但我不知道该怎么做或我是什么应该写,我看了几个教程,但我不能使用我的代码。
P.S:getFilter
项目是从互联网收集的,它们未保存在应用程序代码中
这是MainActivity:
ListView
这是我的适配器:
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private InterstitialAd interstitial;
// Navigation drawer title
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private List<Category> albumsList;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
private LinearLayout DrawerLinear;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Test", "OnCreate Called");
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
DrawerLinear = (LinearLayout) findViewById(R.id.DrawerLinear);
navDrawerItems = new ArrayList<NavDrawerItem>();
// Getting the albums from shared preferences
albumsList = AppController.getInstance().getPrefManger().getCategories();
// Insert "Recently Added" in navigation drawer first position
Category recentAlbum = new Category(null,
getString(R.string.nav_drawer_recently_added));
albumsList.add(0, recentAlbum);
// Loop through albums in add them to navigation drawer adapter
for (Category a : albumsList) {
navDrawerItems.add(new NavDrawerItem(a.getId(), a.getTitle()));
}
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// Setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// Enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(
android.R.color.transparent)));
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
final EditText inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
//You should use the adapter in NavigationDrawerFragment
MainActivity.this.adapter.getFilter().filter(cs); // Doesn't work, gives an error "Cannot resolve getFilter method"
}
@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
}
});
}
答案 0 :(得分:3)
您应该在自定义适配器中实现Filterable
接口。它不是由BaseAdapter实现的。
编辑: 这是一个更完整的例子:
public class FiltarableAdapter extends BaseAdapter implements Filterable {
// keep the full data here. This is needed to not loose it while filtering
private List<SomeObject> originalData;
// keep the data you want to show to the user here
private List<SomeObject> data;
// skip adapter methods
// we will use an instance of this to perform the filtering
private class AdapterFilter implements Filter {
FilterResults performFiltering(CharSequence constraint) {
// use the constraint to filter your data in this method and create a list containing the result
List<SomeObject> filteredData = ...
FilterResults result = new FilterResults();
result.count = filteredData.size();
result.object = filteredData;
}
void publishResults(CharSequence constraint, Filter.FilterResults results) {
// here you get the search results and you should use them to update your UI
// for brevity of the example, we just replace the current adapter data with the result object
FilterableAdapter.this.data = results.object;
FilterableAdapter.this.notifyDataSetChanged();
}
}
public Filter getFilter() {
return new AdapterFilter();
}
}
基本上,您在适配器内部定义过滤器,并将新实例返回到调用getFilter()
的客户端。在过滤器本身中,您需要在数据中进行搜索,并在获得结果时将结果数据集与当前数据集交换。您还需要保留原始的未过滤数据集,这样您就可以在清除过滤器后快速恢复列表。
希望这有帮助。