Shubhojit Singh
Shubhojit Singh

Reputation: 45

How to add OnclickListener for button inside a cardView populated using a recyclerView?

As one can see from the code, I want to put the Intent part for the next activity on ButtonClick. Right now, its inside setItemClickListener due to which on clicking anywhere on the whole card view its moving to next activity. I want to restrict it wrt. applybtn (as mentioned in my code). Kindly, anyone guide me based on my code. I don't know the actual syntax.

package com.example.shubhojit.jobs;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.example.shubhojit.jobs.Interface.ItemClickListener;
import com.example.shubhojit.jobs.Model.Jobs;
import com.example.shubhojit.jobs.ViewHolder.JobsViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import me.anwarshahriar.calligrapher.Calligrapher;

public class ExactJobList_recycler extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference joblist;
    RecyclerView recycler_joblist;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<Jobs,JobsViewHolder> adapter;
    Button applybtn;

    String categoryId="";

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

        Calligrapher calligrapher = new Calligrapher(this);
        calligrapher.setFont(this, "Fonts/Futura Book font.ttf", true);

        database = FirebaseDatabase.getInstance();
        joblist = database.getReference("Job_Details");

        recycler_joblist = (RecyclerView)findViewById(R.id.recycler_jobList);
        applybtn = (Button)findViewById(R.id.applybutton) ;
        recycler_joblist.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recycler_joblist.setLayoutManager(layoutManager);

        if(getIntent() != null)
            categoryId = getIntent().getStringExtra("CategoryId");
        if (!categoryId.isEmpty() && categoryId !=null)
        {
            loadListJobs(categoryId);
        }
    }

    private void loadListJobs(String categoryId) {

        adapter = new FirebaseRecyclerAdapter<Jobs,JobsViewHolder>(Jobs.class,R.layout.exactjoblist__item,JobsViewHolder.class,
                joblist.orderByChild("MenuId").equalTo(categoryId)
        ) {

            @Override
            protected void populateViewHolder(JobsViewHolder viewHolder, Jobs model, int position) {
                viewHolder.jobs_Name.setText(model.getJobName());
                viewHolder.jobs_Description.setText(model.getJobDescription());
                viewHolder.jobs_Openingdate.setText(model.getJobOpeningDate());
                viewHolder.jobs_Post.setText(model.getJobPost());
                viewHolder.jobs_NoofPost.setText(model.getJobNoofpost());
                viewHolder.jobs_Salary.setText(model.getJobSalary());
                viewHolder.jobs_Qualification.setText(model.getJobQualification());
                viewHolder.jobs_Agelimit.setText(model.getJobAgelimit());
                viewHolder.jobs_Lastdate.setText(model.getJoblastdate());
                final Jobs local = model;
                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {

                        Intent JobDetail = new Intent(ExactJobList_recycler.this,Job_Details.class);
                        JobDetail.putExtra("JobId",adapter.getRef(position).getKey());
                        startActivity(JobDetail);
                    }
                });

            }
        };
        recycler_joblist.setAdapter(adapter);
    }
}

Upvotes: 1

Views: 895

Answers (3)

AIMIN PAN
AIMIN PAN

Reputation: 1675

in populateViewHolder or onBindViewHolder(... position ... ):

viewholder.applybtn.setTag(position);
viewholder.applybtn.setOnClickListener(new View.OnClickListener() {
  @Override
  void onClick(View view) {
    // here, view = viewholder.applybtn
    int positionFromTag = (Int)view.getTag();
    Intent JobDetail = new Intent(ExactJobList_recycler.this,Job_Details.class);
    JobDetail.putExtra("JobId",adapter.getRef(positionFromTag).getKey());
    startActivity(JobDetail);
  }
}

actually setOnClickListener can be called in onCreateViewHolder() so it is created only once, only Tag changes when binding to rows.

Upvotes: 1

Gayathri
Gayathri

Reputation: 249

Try this,

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


        holder.dashboardTestHistorySubjectNameTv.setText(Html.fromHtml(testName));

        holder.dashboardTestHistoryTestDate.setText(Html.fromHtml(endDate));

        holder.testHistoryPercentScore.setText(Html.fromHtml(percentScore));

        holder.testHistoryAttemptCountTv.setText(Html.fromHtml(attemptCount));

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

         // your code

            }
        });
    }

Upvotes: 0

Khalid Saeed
Khalid Saeed

Reputation: 156

Just use the name of button you want to click something like this: viewHolder.Post.setOnClickListner After view holder use the name of button and then call the set on clicklistner and it will work

Upvotes: 0

Related Questions