截至目前,我有三个标签(可滚动),这些标签是ActionBarActivity实现的ActionBar.TabListener
当我按下搜索图标时,我试图添加searchview功能,以便弹出edittext来过滤我想要搜索的应用程序。
我的目标是这样的:
目前我正在尝试实施我从中学到的东西:
List Filter Custom Adapter dont give result
Android - Actionbar Sherlock - Search Filter
Android ActionBar Customize Search View
现在我在ActionBarActivity中有这个:
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.block, menu);
final EditText editText = (EditText) menu.findItem(
R.id.action_search).getActionView();
editText.addTextChangedListener(textWatcher);
MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
editText.clearFocus();
return true; // Return true to expand action view
}
});
return super.onCreateOptionsMenu(menu);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (null != mAdapter) {
mAdapter.getFilter().filter(s);
}
}
问题是无法解析getFilter方法。在这种情况下我不确定。
我的适配器类是:
package com.ibc.android.demo.appslist.app;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.spicycurryman.getdisciplined10.app.R;
import java.util.List;
public class ApkAdapter extends BaseAdapter {
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
boolean[] itemChecked;
public ApkAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
itemChecked = new boolean[packageList.size()];
}
private class ViewHolder {
TextView apkName;
CheckBox ck1;
}
public int getCount() {
return packageList.size();
}
public Object getItem(int position) {
return packageList.get(position);
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.installed_apps, null);
holder = new ViewHolder();
holder.apkName = (TextView) convertView
.findViewById(R.id.appname);
holder.ck1 = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
//holder.ck1.setTag(packageList.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
}
// ViewHolder holder = (ViewHolder) convertView.getTag();
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 75, 75);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
holder.ck1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.ck1.isChecked())
itemChecked[position] = true;
else
itemChecked[position] = false;
}
});
return convertView;
}
}
我需要在适配器类中更改以解决getFilter方法错误以及为什么?我还想知道是否有更优化/最有效的方法在操作栏中实现搜索...?
答案 0 :(得分:3)
首先在ApkAdapter类中实现Filterable
在apkAdapter clas中添加此内容
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemsFilter();
}
return mFilter;
}
private class ItemsFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
// TODO Auto-generated method stub
ArrayList<PackageInfo> packageList_2 = packageList;
String prefixString = prefix.toString().toLowerCase();
FilterResults results = new FilterResults();
ArrayList<PackageInfo> FilteredList = new ArrayList<PackageInfo>();
if (prefix == null || prefix.length() == 0) {
results.values = packageList_2;
results.count = packageList_2.size();
return results;
}
for (int i = 0; i < packageList_2.size(); i++) {
String filterText = prefix.toString().toLowerCase();
try {
PackageInfo data = packageList_2.get(i);
if (data.applicationInfo
.loadLabel(getActivity().getPackageManager())
.toString().toLowerCase().contains(filterText)) {
FilteredList.add(data);
} else if (data.packageName.contains(filterText)) {
FilteredList.add(data);
}
} catch (Exception e) {
Toast.makeText(getActivity(),
"exception e" + e.toString(),
Toast.LENGTH_SHORT).show();
}
}
results.values = FilteredList;
results.count = FilteredList.size();
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
Toast.makeText(getActivity(),"result-0 "+results.count,
Toast.LENGTH_SHORT).show();
packageList = (List<PackageInfo>) results.values;
notifyDataSetChanged();
}
}
然后在OnCreateOptionMenu中添加它
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.block, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (!isLoading()) {
mAppAdapter.getFilter().filter(query);
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (!isLoading()) {
if (newText.equals("")) {
mAppAdapter.getFilter().filter("");
}
}
return true;
}
});
答案 1 :(得分:1)
ApkAdapter必须实现Filterable接口。然后,您需要实际编写自己的过滤逻辑来执行过滤器。请记住,所有过滤逻辑都发生在后台线程上。这意味着你的所有适配器mutate方法都需要有同步锁......除非你绝对确定地知道,你的适配器在加载后永远不会改变。
请注意,您的ApkAdapter实现并没有执行ArrayAdapter无法执行的任何操作。因此,扩展ArrayAdapter类可能会更好,因为它已经支持过滤。但是,我应该指出ArrayAdapter有filtering bugs。
或者,如果您想避免编写自己的过滤代码的所有麻烦,我建议您查看此alternative。