我正在开发一个应用程序,它使用View Pager和视图分页器片段,然后是网格视图。 我有主要活动,有搜索栏,现在我想从网格视图页面搜索。我已经实现了片段搜索,但是当我搜索时,它会用更多的数据填充网格 在我的适配器中,我有一种搜索数据的方法是,
public void filterData(String query) {
query = query.toLowerCase();
// Toast.makeText(activity, String.valueOf(this.tempMenulist.size()),
// Toast.LENGTH_LONG).show();
this.mArrayList.clear();
if (query.isEmpty()) {
this.mArrayList.addAll(this.mArrayList_temp);
} else {
for (int i = 0; i < this.mArrayList_temp.size(); i++) {
GridItem next = this.mArrayList_temp.get(i);
String strFirstName = next.getItem_name().toLowerCase();
//String strLastName = next.get("LastName").toLowerCase();
//String strFinalName = strFirstName;
//+" "+strLastName;
if (next.getItem_name().toLowerCase().contains(query)
|| next.getItem_price().toLowerCase().contains(query)
//|| strFinalName.contains(query)
/*|| next.get("attendant").toLowerCase().contains(query)
|| next.get("order_total").toLowerCase().contains(query)
|| next.get("appointment_date").toLowerCase().contains(query)*/
) {
this.mArrayList.add(next);
//Log.d("System Out", "mArrayList in search : "+mArrayList.toString());
} else {
}
}
for (int j = 0; j < mArrayList.size(); j++) {
GridItem next1 = this.mArrayList_temp.get(j);
}
}
// Toast.makeText(activity, String.valueOf(this.tempMenulist.size()),
// Toast.LENGTH_LONG).show();
notifyDataSetChanged();
}
在Fragment中我在editext的textchange上调用了上面的方法,edittext在Activity中,
EditText etno_searchbar_products = (EditText)getActivity().findViewById(R.id.etno_searchbar_products);
etno_searchbar_products.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
gridImageAdapter.filterData(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});