A few days ago I asked a question asking for help and guidance on how to create a RecyclerView
with a CursorAdapter
in order to create a list where the data RecyclerView
was extracted from the internal DB (in SQLite) of the app itself. Here is the link to the question.
Today, I wanted to implement the method onClickListener
so that, when the user clicks on an item in the list, a new one is opened Activity
where all the data of that item appears.
By logic and by several failed examples on the Internet, I know that I have to implement the onClickListener
within the adapter of the RecyclerView
and, from there, take the data _id
of the item that has been selected, consult the data of that record and pass it to the new Activity
one by a Intent
, but I can't get this _id
from the item.
How do I get the _id
selected item? Am I forgetting something? I'm doing it wrong? The method onClickListener
is not implemented in the RecyclerAdapter
or if? Can it really be implemented onClickListener
or does it have to be used OnItemTouchListener
if it is in it RecyclerView
according to the Google documentation ?
This is the adapter code RecyclerView
that extends from CursorRecyclerViewAdapter<ListsAdapter.ViewHolder>
:
public class ListsAdapter extends CursorRecyclerViewAdapter<ListsAdapter.ViewHolder>{
Cursor c;
Context context;
Cursor cursor;
//Constructor
public ListsAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.context = context;
this.cursor = cursor;
c = cursor;
}
// Para enlazar el diseño del ítem a la lista
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.design_item_list, parent, false);
return new ViewHolder(itemView, context);
}
// Para declarar las variables del layout seleccionado y poder llenarlas después
// Aquí he implementado el "OnClickListener"
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title, delivery, aux;
Context context;
Cursor cursor;
public ViewHolder(View itemView, Context context) {
super(itemView);
this.context = context;
itemView.setOnClickListener(this);
title = (TextView) itemView.findViewById(R.id.title_item);
delivery = (TextView) itemView.findViewById(R.id.delivery);
aux = (TextView) itemView.findViewById(R.id.date);
}
// Aquí es donde se supone tengo que coger el _id del registro del cursor, pero no se como. También tengo que abrir la nueva activity, pasandole los datos del registro del cursor por un un intent.
@Override
public void onClick(View v) {
// Aquí necesito de alguna forma coger el _id y hacer
// aparecer la nueva activity
ArrayList<String> data = selectData();
Intent intent = new Intent(context, ItemDetailsHomeworkActivity.class);
intent.putExtra("datos", data);
context.startActivity(intent);
}
// Desde aquí queria obtener los datos del cursor, pero al no saber
// como coger el _id no puedo obtener esos datos.
// Se supone que tendría que pasar como dato sobrecargado el _id.
private ArrayList<String> selectData(){
ArrayList<String> data = new ArrayList<>();
data.add(cursor.getString(0));// _ID - Integer
data.add(cursor.getString(1));// Nombre del deber
data.add(cursor.getString(2));// Nombre del tipo de deber
data.add(cursor.getString(3));// tieneNota - Booleano
data.add(cursor.getString(4));// Descripcion
data.add(cursor.getString(5));// Fecha entrega
data.add(cursor.getString(6));// Hora de entrega
data.add(cursor.getString(7));// Ubicacion
data.add(cursor.getString(8));// Grado de tipo de prioridad
data.add(cursor.getString(9));// Nombre de la asignatura
data.add(cursor.getString(10));// Calificación
data.add(cursor.getString(11));// Terminado - Booleano
return data;
}
}
@Override
public void onBindViewHolder(ListsAdapter.ViewHolder holder, Cursor cursor) {
holder.title.setText(cursor.getString(1));
holder.delivery.setText(cursor.getString(9));
if (cursor.getString(5).equals("null")) {
holder.aux.setText("");
} else {
holder.aux.setText(cursor.getString(5));
}
holder.cursor = cursor;
}
@Override
public int getItemCount() {
if (c != null) {
return c.getCount();
}
return 0;
}
}
I hope you can help me or that someone has created something similar in their project and can answer and explain the questions I have asked.
Thanks in advance to everyone.
As I have put in my code, a can be implemented
onClickListener
without any problem.The only problem I had was that I needed to get the DB record corresponding to the selected item that was stored in a Cursor and I got an answer.
This has been the solution:
This is how I take the data in
Activity
which it collects theIntent
*:Where I had confused myself was that I needed the one
_id
from the record to be able to access it, but no, because if theSELECT
one that takes the records from me to be able to fill out the list has 3 records, in the list it will have 3 records and no more, that's why if I get the position of the selected item I will have the position of the record, all you have to do is move through the cursor and that's it.Sorry for the inconvenience for the question, I'm going against the clock with this app and I'm nervous. In the end it was silly, but I don't want to leave the question for people who want to implement this or create one
onClickListener
in theirRecyclerView
. This is how we enrich this community.* I had also had a little problem with having to pass a
ArrayList
through aIntent
, but thanks to x4mp73r I fixed it.Thank you very much to all.