gpt4 book ai didi

java - 修复操作栏中搜索 View 的 getfilter

转载 作者:行者123 更新时间:2023-11-29 03:18:46 25 4
gpt4 key购买 nike

截至目前,我有三个选项卡(可滚动),它们是带有 ActionBarActivity 实现 ActionBar.TabListener 的 fragment

enter image description here

我试图在按下搜索图标时添加搜索 View 功能,以便弹出一个编辑文本来过滤我想要搜索的应用程序。

我的目标是这样的: enter image description here

目前我正在努力实现我从中学到的东西:

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 方法错误,为什么?我也想知道是否有更优化/更有效的方式来实现操作栏中的搜索...?

最佳答案

首先在ApkAdapter类中实现Filterable

在 apkAdapter 类中添加这个

    @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;
}
});

关于java - 修复操作栏中搜索 View 的 getfilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com