Android Studio searchview with recyclerview

Hi im learning android studio and i cant get to work my SEARCH VIEW can u help me please my app crashs and says java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1

MAIN LIST WITH RECYCLER VIEW ITEMS

public class list_product extends AppCompatActivity implements SearchView.OnQueryTextListener {

RecyclerView recyclerView;
SearchView searchView;

MyDatabaseHelper myDB;
ArrayList<String> id_id,id_cliente, id_cantceldas;
CustomAdapter customAdapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_list_product);

    recyclerView = findViewById(R.id.recyclerView);
    searchView = findViewById(R.id.searchview);

    initListener();

    myDB = new MyDatabaseHelper(list_product.this);
    id_id = new ArrayList<>();
    id_cliente = new ArrayList<>();
    id_cantceldas = new ArrayList<>();




    storeDataInArrays();


    customAdapter = new CustomAdapter(list_product.this, id_id, id_cliente ,id_cantceldas);
    recyclerView.setAdapter(customAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(list_product.this));

}


void storeDataInArrays(){
    Cursor cursor = myDB.readAllData();
    if(cursor.getCount() == 0){
        Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
    }else{
        while(cursor.moveToNext()){
            id_id.add(cursor.getString(0));
            id_cliente.add(cursor.getString(1));
            id_cantceldas.add(cursor.getString(3));
        }
    }
}

public void initListener(){
    searchView.setOnQueryTextListener(this);
}


@Override
public boolean onQueryTextSubmit(String query) {

    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    customAdapter.filter(newText);
    return false;
}

ADAPTER

private Context context;
private ArrayList id_id;
private ArrayList id_cliente;
private ArrayList id_cantceldas;
private ArrayList<String> original_clients;
CustomAdapter(Context context, ArrayList id_id, ArrayList id_cliente, ArrayList id_cantceldas){


    this.context = context;
    this.id_id = id_id;
    this.id_cliente = id_cliente;
    this.id_cantceldas = id_cantceldas;
    this.original_clients = new ArrayList<>();
    original_clients.addAll(id_cliente);

}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.my_row, parent,false);
    return new MyViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    Calendar calendar = Calendar.getInstance();
    String currentdateday_string = "";
    int currentDateday = calendar.get(Calendar.DAY_OF_WEEK);


    String currentDatedaynum = new SimpleDateFormat("dd", Locale.getDefault()).format(new Date());

    holder.id_id_txt.setText(String.valueOf(id_id.get(position)));
    holder.id_cliente_txt.setText(String.valueOf(id_cliente.get(position)));
    holder.id_cantceldas_txt.setText(String.valueOf(id_cantceldas.get(position))+ " Celdas");
    holder.today_datemonth.setText(String.valueOf(currentDatemonth));
    holder.today_dateday.setText(String.valueOf(currentdateday_string + currentDatedaynum ));
}

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

public void filter(final String searchView) {
    if (searchView.length() == 0) {
        id_cliente.clear();
        id_cliente.addAll(original_clients);
    }
    else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            id_cliente.clear();
            List<String> collect = original_clients.stream()
                    .filter(i -> i.toLowerCase().contains(searchView))
                    .collect(Collectors.toList());

            id_cliente.addAll(collect);
        }
        else {
            id_cliente.clear();
            for (String i : original_clients) {
                if (i.toLowerCase().contains(searchView)) {
                    id_cliente.add(i);
                }
            }
        }
    }
    notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView id_id_txt,id_cliente_txt, id_cantceldas_txt,today_datemonth,today_dateday;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        int positionid = getAdapterPosition();
        Button button = (Button) itemView.findViewById(R.id.button4);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Toast.makeText(view.getContext(), "Clicked button at position: " + (getAdapterPosition() + 1), Toast.LENGTH_SHORT).show();
            }
        });
        id_id_txt = itemView.findViewById(R.id.id_id);
        id_cliente_txt = itemView.findViewById(R.id.id_cliente_show);
        id_cantceldas_txt = itemView.findViewById(R.id.id_cantceldas_show);
        today_datemonth = itemView.findViewById(R.id.id_cliente_show3);
        today_dateday = itemView.findViewById(R.id.current_day);

    }


}

}



Read more here: https://stackoverflow.com/questions/64398750/android-studio-searchview-with-recyclerview

Content Attribution

This content was originally published by TheRakaPlays 2.0 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: