Actually i'm trying to add a SearchView in my RecyclerView that will permise to search different kind of items on it,
But i'm getting a problem when i'm searching for one result in SearchView and it has been found when i click on it the selected item is wrong or better it's ever the item from main ArrayList and not searched result as you can see in the gif below i'm searching for Bayles when i press on it snackbar saying that i've selected Bayles but when i add it as child item to another recyclerView it has added ABBONDANTE and not Bayles.
Here is my ViewHolder from the Adapter
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
Snackbar.make(v,variantiConstructors.get(position).getDeskT(),Snackbar.LENGTH_SHORT).show();
}
}
}
});
While here is my onClickListener from the Activity where i add the item to other RecyclerView
adapterVarianti.setOnItemClickListener(new AdapterVarianti.OnItemClickListener() {
@Override
public void onItemClick(int position) {
search.clearFocus();
if (dummyDataItems.size() != 0) {
if (tipo.toString().equals("CON ") || tipo.toString().equals("PIU' ") || tipo.toString().equals("MENO ") || tipo.toString().equals("SENZA ")) {
// Here i'm adding the selected item to a String builder but i think it's here where i'm doing something wrong
tipo.append(filteredVariable.get(position).getDeskT());
// Adding item from the recyclerView where there is searchView to last item from the recyclerView on background.
ItemPTERM lastAdded = dummyDataItems.get(dummyDataItems.size() - 1);
lastAdded.setVariant(new Variant(tipo.toString()));
if (exampleAdapter.getItemCount() > 0) {
mRecyclerViewTOP.scrollToPosition(exampleAdapter.getItemCount() - 1);
}
exampleAdapter.notifyDataSetChanged();
tipo = new StringBuilder();
if(segmentedButtonGroup.getPosition() == 0){
tipo = new StringBuilder();
tipo.append("CON ");
}
if(segmentedButtonGroup.getPosition() == 1){
tipo = new StringBuilder();
tipo.append("PIU' ");
}
if(segmentedButtonGroup.getPosition() == 2){
tipo = new StringBuilder();
tipo.append("MENO ");
}
if(segmentedButtonGroup.getPosition() == 3){
tipo = new StringBuilder();
tipo.append("SENZA ");
}
}
}
}
});
Here is my Adapter code with filter
public class AdapterVarianti extends RecyclerView.Adapter<AdapterVarianti.ExampleViewHolder> implements Filterable {
private ArrayList<VariantiConstructor> variantiConstructors;
private ArrayList<VariantiConstructor> mFilteredList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
@NonNull
@Override
public AdapterVarianti.ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.varianti_recycler,parent,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
return new AdapterVarianti.ExampleViewHolder(v, mListener);
}
AdapterVarianti(ArrayList<VariantiConstructor> exampleList){
variantiConstructors = exampleList;
mFilteredList = variantiConstructors;
}
@Override
public void onBindViewHolder(@NonNull final AdapterVarianti.ExampleViewHolder holder, final int position) {
final VariantiConstructor item = variantiConstructors.get(position);
holder.desc.setText(item.getDeskT());
holder.prepiu.setText(String.valueOf(item.getPrice()));
holder.premeno.setText(String.valueOf(item.getPacq()));
if(position % 2 == 0 ){
holder.itemView.setBackgroundColor(Color.parseColor("#17e1a0"));
}else if(position % 2 == 1){
holder.itemView.setBackgroundColor(Color.parseColor("#7EC0EE"));
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<VariantiConstructor> results = new ArrayList<>();
if (mFilteredList == null)
mFilteredList = new ArrayList<>(variantiConstructors);
if (constraint != null && constraint.length() > 0) {
if (mFilteredList != null && mFilteredList.size() > 0) {
for (final VariantiConstructor cd : mFilteredList) {
if (cd.getDeskT().toLowerCase()
.contains(constraint.toString().toLowerCase()))
results.add(cd);
}
}
oReturn.values = results;
oReturn.count = results.size(); //newly Aded by ZA
} else {
oReturn.values = mFilteredList;
oReturn.count = mFilteredList.size(); //newly added by ZA
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(final CharSequence constraint,
FilterResults results) {
variantiConstructors = new ArrayList<>((ArrayList<VariantiConstructor>) results.values);
// FIXME: 8/16/2017 implement Comparable with sort below
///Collections.sort(itemList);
notifyDataSetChanged();
}
};
}
@Override
public int getItemCount() {
return variantiConstructors.size();
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView desc;
public TextView prepiu;
public TextView premeno;
ExampleViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
desc = itemView.findViewById(R.id.Desc);
prepiu = itemView.findViewById(R.id.PrePiu);
premeno = itemView.findViewById(R.id.PreMeno);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
Snackbar.make(v,variantiConstructors.get(position).getDeskT(),Snackbar.LENGTH_SHORT).show();
}
}
}
});
}
}
}
Solve by passing again the list to Activity by the following code:
public ArrayList<VariantiConstructor> getList(){
return variantiConstructors;
}
And by this in activity code :
tipo.append(adapterVarianti.getList().get(position).getDeskT());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.