Pass data from Adapter to an Android Studio Activity

0

Hello my problem is that I am developing an android app where I have an Activity and a RecyclerView.Adapter, and I want to send a serializable parameter from the adapter to the activity and receive it. I tried to do it with intent, it does not work for me the app is dropped. Code:

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();
}
}
    
asked by jeanD 01.04.2018 в 04:23
source

2 answers

0

I found the solution, here's the code:

Adapter:

 Intent intent = new Intent(context, tu_clase.class);
                intent.putExtra(Intent.EXTRA_TEXT, item);
                context.startActivity(intent);
Activity:

Intent intent = getIntent();
        if(intent.hasExtra(Intent.EXTRA_TEXT)) {
        // Notas es mi entity
notas = (Notas)intent.getSerializableExtra(Intent.EXTRA_TEXT);
    
answered by 03.04.2018 / 08:27
source
0

The Dialog must call it from the activity, try to add a listener in the adapter and then implement it in the activity, here is an excellent tutorial that can help you Tutorial of RecyclerView greetings

    
answered by 02.04.2018 в 01:35