Home » Android » onClickListener in RecyclerView Implementation

onClickListener in RecyclerView Implementation

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am new to Android, I have made a recyclerview, which prints the list and works, I want to implement a onClickListener on the list elements, when click I am trying to send the text to a new Intent but in vain. Could someone help me with their expertise, This might be simple, but I am trying to learn and get better at Android, if any one plans to down vote this, kindly comment and let me know first, so that I can at least try to fix the question with your suggestions.

Below is Code & Screenshot.

Here is my view, when I click on a list item, I want to get the text on that List Item screenshot

MainActivity.java

package com.fayazahmed.ifscdb;

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.ProgressBar;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<GetDataAdapter> GetDataAdapter1;
    RecyclerView recyclerView;
    RecyclerView.LayoutManager recyclerViewlayoutManager;
    RecyclerView.Adapter recyclerViewadapter;
    ProgressBar progressBar;
    String GET_JSON_DATA_HTTP_URL = "http://myTestapi.php";
    String JSON_BANK_NAME = "BANK";
    JsonArrayRequest jsonArrayRequest ;
    RequestQueue requestQueue ;
    View ChildView ;


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


        GetDataAdapter1 = new ArrayList<>();

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

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        recyclerView.setHasFixedSize(true);

        recyclerViewlayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(recyclerViewlayoutManager);

        JSON_DATA_WEB_CALL();

    }

    public void JSON_DATA_WEB_CALL(){

        progressBar.setVisibility(View.VISIBLE);

        jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        progressBar.setVisibility(View.GONE);

                        JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }

    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

        for(int i = 0; i<array.length(); i++) {

            GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                GetDataAdapter2.setBankName(json.getString(JSON_BANK_NAME));

            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }

        recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);

        recyclerView.setAdapter(recyclerViewadapter);
    }

}

RecyclerViewAdapter.java

package com.fayazahmed.ifscdb;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;



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

    Context context;
    List<GetDataAdapter> getDataAdapter;

    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){
        super();
        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

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

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

        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

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

        GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

        holder.IdTextView.setText(String.valueOf(getDataAdapter1.getBankName()));

    }

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView IdTextView;



        public ViewHolder(View itemView) {

            super(itemView);

            IdTextView = (TextView) itemView.findViewById(R.id.TextViewCard) ;

        }
    }

}
Answers:

here you go

just implement the click listener on your viewholder and perform a callback to your main activity.

callback

public interface OnClickViewItemListener {

        void onClickViewItem(View view, int position);

    }

adapter

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

    Context context;
    List<GetDataAdapter> getDataAdapter;
    private OnClickViewItemListener mItemListener;

    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context,OnClickViewItemListener mItemListener){
        super();
        this.getDataAdapter = getDataAdapter;
        this.context = context;
        this.mItemListener = mItemListener;
    }

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

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

        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

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

        GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

        holder.IdTextView.setText(String.valueOf(getDataAdapter1.getBankName()));

    }

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

     public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
            public TextView IdTextView;
            public ViewHolder(View itemView) {
                super(itemView);
                IdTextView = (TextView) itemView.findViewById(R.id.TextViewCard) ;
                itemView.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                //perform your action here or get a callback on calling view (activity/fragment) and perform action there.
                mItemListener.onClickViewItem(v,getAdapterPosition());
            }
}

if you choose to get the callback on mainactivity follow this

mainactivity should implement the click listener

public class MainActivity extends AppCompatActivity implements  OnClickViewItemListener {
        List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
ProgressBar progressBar;
String GET_JSON_DATA_HTTP_URL = "http://myTestapi.php";
String JSON_BANK_NAME = "BANK";
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
View ChildView ;


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


    GetDataAdapter1 = new ArrayList<>();

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

    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    recyclerView.setHasFixedSize(true);

    recyclerViewlayoutManager = new LinearLayoutManager(this);

    recyclerView.setLayoutManager(recyclerViewlayoutManager);

    JSON_DATA_WEB_CALL();

}

public void JSON_DATA_WEB_CALL(){

    progressBar.setVisibility(View.VISIBLE);

    jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    progressBar.setVisibility(View.GONE);

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(jsonArrayRequest);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

    for(int i = 0; i<array.length(); i++) {

        GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

        JSONObject json = null;
        try {
            json = array.getJSONObject(i);

            GetDataAdapter2.setBankName(json.getString(JSON_BANK_NAME));

        } catch (JSONException e) {

            e.printStackTrace();
        }
        GetDataAdapter1.add(GetDataAdapter2);
    }

    recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this,this);

    recyclerView.setAdapter(recyclerViewadapter);
}


        @Override
        public void onClickViewItem(View view, int position) {
            //here handle the item click listener

        }
    }