A few days ago I asked a question asking for help and guidance on how to create a RecyclerView
with a CursorAdapter
to be able to create a list where% data RecyclerView
were extracted from the internal DB (in SQLite) from the app itself. Here the link of the question.
Today, I wanted to implement the onClickListener
method so that, when the user clicks on an item in the list, a new Activity
will open where all the data of that item will appear.
By logic and by several failed Internet examples, I have to implement the onClickListener
within the adapter of RecyclerView
and, from there, take the data _id
of the item selected, consult the data of that record and pass it to the new Activity
for a Intent
, but I can not get this _id
of the item.
How do I get the _id
of the selected item? Am I forgetting something? I'm doing it wrong? Is the onClickListener
method not implemented in RecyclerAdapter
or yes? Can you really implement onClickListener
or you have to use OnItemTouchListener
if it is in RecyclerView
according to the Google Documentation ?
This is the adapter code of RecyclerView
extending 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 your project and can answer me and explain the questions I have asked.
Thanks in advance to everyone.