Skip to main content
1 de 2
Osdward
  • 322
  • 2
  • 15

Como implementar Click Listener al RecyclerView?

Tengo una base de datos en Firebase donde subo texto que luego recupero en el RecyclerView como items, lo que nececesito es que al tocar un item del recyclerview pueda pasar esos datos a otra activity.

Mi Adaptador

public class MyAdaptador extends  RecyclerView.Adapter<MyAdaptador.ViewHolder>{



private Context context;
private List<Upload> uploads;



public MyAdaptador(Context context, List<Upload> uploads) {

    this.uploads = uploads;
    this.context = context;



}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cuadros_de_textos, parent, false);

    ViewHolder viewHolder = new ViewHolder(v);



    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {


    Upload upload = uploads.get(position);

    holder.titulo.setText(upload.getTitulo());

    holder.texto.setText(upload.getTexto());





}


@Override
public int getItemCount() {

    return uploads.size();
}



public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView titulo;
    public TextView texto;




    public ViewHolder(final View itemView) {
        super(itemView);
        

        titulo = (TextView) itemView.findViewById(R.id.titulo_del_cuadro);
        texto = (TextView) itemView.findViewById(R.id.texto_del_cuadro);



      

    }


   }




 }

MainActivy donde recibo los datos en el Recyclerview

public class MainActivity extends AppCompatActivity  {



//recyclerview object
private RecyclerView recyclerView;

//adapter object
private RecyclerView.Adapter adapter;

//database reference
private DatabaseReference mDatabase;

//progress dialog
private ProgressDialog progressDialog;

//list to hold all the uploaded images
private List<Upload> uploads;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);





    floatingActionButton = findViewById(R.id.floatingbotton);

    floatingActionButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent i = new Intent(MainActivity.this, CreateNote.class);
            startActivity(i);

        }
    });

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

    recyclerView.setHasFixedSize(true);

    GridLayoutManager mGridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);

    recyclerView.setLayoutManager(mGridLayoutManager);


    progressDialog = new ProgressDialog(this);


    uploads = new ArrayList<>();


    //displaying progress dialog while fetching images
    progressDialog.setMessage("Please wait...");
    progressDialog.show();


    mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);


    //adding an event listener to fetch values
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {



            //Detener the progress dialog
            progressDialog.dismiss();

            //iterating through all the values in database
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                Upload upload = postSnapshot.getValue(Upload.class);

                uploads.add(0, upload);


            }





            //creating adapter
            adapter = new MyAdaptador(getApplicationContext(), uploads);


            //adding adapter to recyclerview
            recyclerView.setAdapter(adapter);




        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });





}
}

(IGNOREN ESTE TEXTO) Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Osdward
  • 322
  • 2
  • 15