我一直在尝试使用 mSearch EditText过滤/搜索适配器适配器。我正在寻求实施 getFilter()等方面的帮助。
此活动目前正在显示内容列表。 mSearch 位于列表上方,我希望按照某人的类型过滤/更新列表。
如果您需要特定的代码或更多信息,请与我们联系。提前谢谢!
public class MainActivity extends FragmentActivity {
private SearchView mSearch;
private List<Model> mMain = new ArrayList<Model>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class MainFragment extends Fragment implements SearchView.OnQueryTextListener {
private ListView mListView;
private Adapter mAdapter;
public boolean onQueryTextChange(String query) {
if (mAdapter != null) {
mAdapter.getFilter().filter(query.toString());
}
return false;
}
public boolean onQueryTextSubmit(String query) {
onQueryTextChange(query);
return false;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_user_list_row, container, false);
mListView = (ListView) findViewById(R.id.listView);
mAdapter = new Adapter();
mListView.setAdapter(mAdapter);
mListView.setAdapter.setTextFilterEnabled(true);
mSearch.setOnQueryTextListener((SearchView.OnQueryTextListener) mContext);
return rootView;
}
public class Adapter extends BaseAdapter implements Filterable {
@Override
public int getCount() {
return mMain.size();
}
@Override
public Object getItem(int position) {
return 1;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(mContext, R.layout.main_user_list_row, null);
if (mContext != null && mMain.get(position) != null) {
TextView mPosition = (TextView) view.findViewById(R.id.position);
mPosition.setText("...");
}
return view;
}
@Override
public Filter getFilter() {
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mMain = (List<Model>) results.values;
Adapter.this.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Contest> filteredResults = getFilteredResults(constraint);
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
};
}
}
}
我在 getFilteredResults
收到此错误Cannot resolve method 'getFilteredResults(java.lang.CharSequence)'
我也在 mSearch.setOnQueryTextListener((SearchView.OnQueryTextListener)mContext)中收到此错误;
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MainActivity}: java.lang.ClassCastException: ...InDraftActivity cannot be cast to android.widget.SearchView$OnQueryTextListener
java.lang.ClassCastException: ...MainActivity cannot be cast to android.widget.SearchView$OnQueryTextListener
解
我通过在活动中实现OnQueryTextListener而不是片段来修复SearchView错误。
performFiltering看起来像
List<Contest> filteredResults = new ArrayList<Contest>();
FilterResults results = new FilterResults();
results.values = filteredResults;
results.count = filteredResults.size();
见标记答案。
答案 0 :(得分:2)
在您返回FilterResults
之前,您必须指定其值和计数。目前你只是像这样指定值......
results.values = filteredResults;
在该行下,在results
方法中返回performFiltering(...)
之前,请添加此行...
results.count = filteredResults.size();
编辑:一个完全正常工作的适配器,带有我已经完成的项目的过滤器
class ContactsAdapter extends BaseAdapter implements Filterable {
private List<Contact> mData;
private List<Contact> database;
private LayoutInflater mInflater;
private Context context;
static boolean inMarkMode = false;
ContactsAdapter(List<Contact> mData, List<Contact> database, Context context) {
this.mData = mData;
this.context = context;
this.database = database;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
/**
* Modifies the way a contact is shown in its adapter depending on
* displayChoice shared preference
*
* @param position
* of the contact in its listview
* @return <code>String</code> to display
*/
@Override
public String getItem(int position) {
switch (context.getSharedPreferences("settings",
HomeScreenActivity.MODE_PRIVATE).getInt("displayChoice", -1)) {
case 1:
return mData.get(position).getLastName() + " "
+ mData.get(position).getFirstName();
case 2:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getMobileNumber();
case 3:
return mData.get(position).getLastName() + " "
+ mData.get(position).getMobileNumber();
default:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getLastName();
}
}
@Override
public long getItemId(int position) {
return position;
}
/**
* Modifies the view shown in HomeScreenActivity depending on whether the
* user has entered mark mode or not
*
* @return <code>View</code> to display
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView result = null;
TextView normalResult = null;
final String contactRow = getItem(position);
if (inMarkMode) {// if user is in markMode, use the mark layout
if (convertView == null) {
result = (CheckedTextView) mInflater.inflate(
R.layout.home_screen_contacts_mark_view, parent, false);
} else {
result = (CheckedTextView) convertView;
}
result.setText(contactRow);
result.setBackgroundResource(R.drawable.list_selector);
} else { // if user NOT in markmode, use normal contacts view layout
if (convertView == null) {
normalResult = (TextView) mInflater.inflate(
R.layout.home_screen_contacts_view, parent, false);
} else {
normalResult = (TextView) convertView;
}
normalResult.setText(contactRow);
normalResult.setBackgroundResource(R.drawable.list_selector);
}
if (inMarkMode) {
return result;
} else {
return normalResult;
}
}
int getItemIdAtPosition(int position) {
return mData.get(position).getID();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
// If there's nothing to filter on, return the original data for
// your list
if (charSequence == null || charSequence.length() == 0) {
results.values = database;
results.count = database.size();
} else {
List<Contact> filterResultsData = new ArrayList<Contact>();
// if search details is 0, search fullName, else, search
// all details
if (HomeScreenActivity.searchAllDetails == 0) {
for (Contact c : database) {
if (c.getFullName().toLowerCase(Locale.ENGLISH)
.contains(charSequence)) {
filterResultsData.add(c);
}
}
} else {
for (Contact c : database) {
if (c.getAllDetailsForSearch()
.toLowerCase(Locale.ENGLISH)
.contains(charSequence)) {
filterResultsData.add(c);
}
}
}
results.values = filterResultsData;
results.count = filterResultsData.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence charSequence,
FilterResults filterResults) {
// set the data to the filter results and notifyDataSetChanged()
mData = (List<Contact>) filterResults.values;
notifyDataSetChanged();
}
};
}
}