'Search view resulting incorrectly from the database array

The searchview that is implemented works as in it can search but the results will always be the same no matter what is inputted by the user. If the user searches for CC the result from the database will be AA or BB still will be AA. Is there something I am missing in my adapter code.

EDIT** Added The Indexed Position that was overlooked, Runs As Intended Now.

ListViewAdapter class

package com.example.project;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View; 
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Locale;

public class ListViewAdapter extends 
RecyclerView.Adapter<ListViewAdapter.MyItemViewHolder> implements 
Filterable {

private Context context1;
private Activity activity1;
private ArrayList item_id, item_title,item_desc, item_category, item_quantity;
private ArrayList<Integer> index = new ArrayList<>();

ListViewAdapter(Activity activity1, Context context1, ArrayList item_id, ArrayList item_title, 
ArrayList item_desc, ArrayList item_category,
          ArrayList item_quantity){
this.activity1 = activity1;
this.context1 = context1;
this.item_id = item_id;
this.item_title = item_title;
this.item_desc = item_desc;
this.item_category = item_category;
this.item_quantity = item_quantity;
initArrayIndex();

}

private void initArrayIndex(){
index.clear();
for(int i = 0; i < item_title.size(); i++){
    index.add(i);
}
}

@NonNull
@Override
public MyItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context1);
View view = inflater.inflate(R.layout.item_row, parent, false);
return new MyItemViewHolder(view);
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onBindViewHolder(@NonNull final MyItemViewHolder holder, final int position) {
    //Be sure to Add Indexed Position - thanks to i_A_mok
    final int indexedPosition = index.get(position);
    holder.item_id_txt.setText(String.valueOf(item_id.get(indexedPosition)));
    holder.item_title_txt.setText(String.valueOf(item_title.get(indexedPosition)));
    holder.item_desc_txt.setText(String.valueOf(item_desc.get(indexedPosition)));
    holder.item_category_txt.setText(String.valueOf(item_category.get(indexedPosition)));
    holder.item_quantity_txt.setText(String.valueOf(item_quantity.get(indexedPosition)));
    holder.mainLayout1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context1, UpdateItemActivity.class);
            intent.putExtra("id", String.valueOf(item_id.get(indexedPosition)));
            intent.putExtra("title", String.valueOf(item_title.get(indexedPosition)));
            intent.putExtra("desc", String.valueOf(item_desc.get(indexedPosition)));
            intent.putExtra("category", String.valueOf(item_category.get(indexedPosition)));
            intent.putExtra("quantity", String.valueOf(item_quantity.get(indexedPosition)));
            activity1.startActivityForResult(intent, 1);
        }
    });


}

@Override
public int getItemCount() {
 return index.size();
}

 class MyItemViewHolder extends RecyclerView.ViewHolder {

TextView item_id_txt, item_title_txt, item_desc_txt, item_category_txt, item_quantity_txt;
LinearLayout mainLayout1;

MyItemViewHolder(@NonNull View itemView) {
    super(itemView);
    item_id_txt = itemView.findViewById(R.id.item_id_txt);
    item_title_txt = itemView.findViewById(R.id.item_title_txt);
    item_desc_txt = itemView.findViewById(R.id.item_desc_txt);
    item_category_txt = itemView.findViewById(R.id.item_category_txt);
    item_quantity_txt = itemView.findViewById(R.id.item_quantity_txt);
    mainLayout1 = itemView.findViewById(R.id.mainLayout1);
    Animation translate_anim = AnimationUtils.loadAnimation(context1, R.anim.translate_anim);
    mainLayout1.setAnimation(translate_anim);
}

}

public Filter getFilter() {
    return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
    String searchString = constraint.toString().toLowerCase();

    if (constraint.length() == 0) {
        initArrayIndex();

    } else {
        index.clear();
        for(int i=0; i<item_title.size(); i++){
            if(item_title.get(i).toString().toLowerCase().contains(searchString)){
                index.add(i);
            }
        }
    }
    FilterResults results = new FilterResults();
    results.values = index;
    return results;
}
@Override
public void publishResults(CharSequence constraint, FilterResults results) {
    notifyDataSetChanged();
}
};
}

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source