在我的主要活动中,我正在调用onQueryTextChange
http://localhost/#!/somepage/
这是我的列表适配器
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmain, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
adapter.filter("");
listview.clearTextFilter();
} else {
adapter.filter(newText);
}
return true;
}
});
在listview中搜索包含arraylist hashmap的项目的逻辑是什么。
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView rank;
ImageView flag;
TextView hq;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(R.id.rank);
hq = (TextView) itemView.findViewById(R.id.hq);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
rank.setText(resultp.get(SplashScreen.COMAPANY_NAME));
hq.setText(resultp.get(SplashScreen.HQ));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(SplashScreen.COMPANY_LOGO), flag);
return itemView;
}
// Filter method
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
//
//
}
}
notifyDataSetChanged();
}
}
我看到很多关于arraylist字符串的答案,但这些问题无法解决我的问题,请帮我解决这个问题。
答案 0 :(得分:1)
试试这个:
1.在适配器内声明全局BackupList,例如
ArrayList<HashMap<String, String>> mBackupData = new ArrayList<>();
2.在适配器构造函数中,将ALL DATA复制到BackupList中,例如
mBackupData.addAll(arraylist);
3.创建一个公共方法:clear DataList;循环遍历BackupList并将符合条件的项添加到DataList中;通知适配器已更改,例如
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
data.clear();
for (HashMap hm : mBackupData) {
if (((String)hm.get("MapKey")).contains(charText)) {
data.add(hm);
}
}
notifyDataSetChanged();
}
注意:DataList指的是getView()中获取数据的位置。如果DataList将被Fragment / Activity更改,那么在Adapter代码中,还需要创建另一个将项更新为BackupList的公共方法。
希望有所帮助!
答案 1 :(得分:0)
选择一个tempData
以过滤您的数据
HashMap<String, String> resultp = new HashMap<String, String>();
HashMap<String, String> tempData = new HashMap<String, String>();
将resultp
的数据复制到构造函数中的tempData
。
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
tempData = new HashMap<>(mData);
}
用于过滤数据的用户过滤方法
public void filter(String charText) {
data.clear();
if (charText.length() > 0) {
for (String hm : tempData) {
if (hm.contains(charText)) {
data.put("key", hm);
}
}
} else {
for (String hm : tempData) {
data.put("key", hm);
}
}
notifyDataSetChanged(); // notifying the adapter that datSet has been changed...
}