Hola mi problema es que estoy desarrollando una app en android donde tengo un Activity y un RecyclerView.Adapter, y quiero enviar un parametro serializable desde el adaptador hacia el activity y recibirlo. He intentado hacerlo con intent, no me funciona se cae la app. Código:
public class NotasListAdapter extends RecyclerView.Adapter{ private Context context; private List notas; private RecyclerView mRecyclerV; private NotasDB notasDB;
public NotasListAdapter(List<Notas> items, Context contexto, RecyclerView recyclerView) {
this.context = contexto;
this.notas = items;
this.mRecyclerV = recyclerView;
}
static class NotaViewHolder extends RecyclerView.ViewHolder {
// Campos respectivos de un item
protected TextView title;
protected TextView description;
protected TextView date;
public View layout;
public NotaViewHolder(View v) {
super(v);
layout = v;
this.title = (TextView) v.findViewById(R.id.txtTitulo_celda);
this.description = (TextView) v.findViewById(R.id.txtDesc_celda);
this.date = (TextView) v.findViewById(R.id.txtFecha_celda);
}
}
public void remove(int position) {
notas.remove(position);
notifyItemRemoved(position);
}
@Override
public NotaViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.notes_layout, viewGroup, false);
return new NotaViewHolder(v);
}
@Override
public void onBindViewHolder(NotaViewHolder viewHolder, final int position) {
final Notas item = notas.get(position);
viewHolder.itemView.setTag(item);
viewHolder.title.setText(item.getTitulo());
viewHolder.description.setText(item.getDescripcion());
viewHolder.date.setText(item.getFecha());
//Acción para eliminar
viewHolder.layout.setOnLongClickListener(new View.OnLongClickListener() {
@Override public boolean onLongClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Eliminar");
builder.setMessage("¿Deseas eliminar la nota?");
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
NotasDB dbHelper = new NotasDB(context); dbHelper.delete(item.getId());
notas.remove(position);
mRecyclerV.removeViewAt(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, notas.size());
notifyDataSetChanged();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
return true;
}
}); //Click para dirigirse a otro activity y actualizar viewHolder.layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //He intentado hacerlo conn intent pero al parecer no funciona con ello Notas nota = notasDB.findAll().get(position); Intent intent = new Intent(context,Actualizar.class); intent.putExtra("idSeleccionado",nota); context.startActivity(intent); } });
}
@Override
public int getItemCount() {
return notas.size();
}
}