我已实施recyclerView
,其中填充JSON
方法的REST
数据。我还在工具栏上添加了searchView
,根据recyclerview
中输入的数据过滤searchview
。现在我遇到一个问题,在输入搜索参数后点击recyclerview
中的项目时,它总是返回列表中从rest方法填充的第一个元素。
Adapter.java
private Context context;
private LayoutInflater inflater;
private List<RetailerOfferPOJO> data;
private static ClickListener clickListener;
public RetailerOfferAdapter(Context context, List<RetailerOfferPOJO> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.retailer_offer_adapter, parent, false);
MyHolder holder = new MyHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyHolder myHolder = (MyHolder)holder;
RetailerOfferPOJO current = data.get(position);
//myHolder.lblID.setText(current.getsID());
myHolder.lblOfferName.setText(current.getsOfferName());
myHolder.lblDescription.setText(current.getsDescription());
myHolder.lblPrice.setText(current.getsPrice());
}
@Override
public int getItemCount() {
return data.size();
}
/**
* Search Filter method for RecyclerView
* @param searchModels
*/
public void setFilter(List<RetailerOfferPOJO> searchModels) {
data = new ArrayList<>();
data.addAll(searchModels);
notifyDataSetChanged();
}
public static class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
TextView lblID;
TextView lblOfferName;
TextView lblDescription;
TextView lblPrice;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
// lblID = (TextView) itemView.findViewById(R.id.lblID);
lblOfferName = (TextView) itemView.findViewById(R.id.lblOfferName);
lblDescription = (TextView) itemView.findViewById(R.id.lblDescription);
lblPrice = (TextView) itemView.findViewById(R.id.lblPrice);
}
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
final List<RetailerOfferPOJO> filteredModelList = filter(mRetailerOffers, newText);
if (!filteredModelList.isEmpty()) {
mAdapter.setFilter(filteredModelList);
}
return true;
}
});
/**
* Filter method for RecyclerView
*
* @param models
* @param query -> data entered by the user as search parameter
* @return
*/
private List<RetailerOfferPOJO> filter(List<RetailerOfferPOJO> models, String query) {
//@param query -> the data entered by user is taken and converted to lowe case
query = query.toLowerCase();
final List<RetailerOfferPOJO> filteredModelList = new ArrayList<>();
for (RetailerOfferPOJO model : models) {
// the data that is populated in the RecyclerView is converted to lower case
//full name and designation is attached to model class so that when the user searches
//on either of them the recycler view displays data accordingly
final String text = model.getsOfferName().toLowerCase();
final String text1 = model.getsDescription().toLowerCase();
if (text.contains(query)) {
//check if the text in the recycler view contains the data entered by the user
// and then update the list
filteredModelList.add(model);
mAdapter.notifyDataSetChanged();
} else if (text1.contains(query)) {
filteredModelList.add(model);
}
}
// mAdapter.notifyDataSetChanged();
return filteredModelList;
}