androidandroid-recyclerviewonclicklistenerlistitem

RecyclerView - Getting Objects from my ViewHolder


I'm creating a RecyclerView that accepts a list of Match objects. I have set an onClickListener for the listItem. I want to set the Match to active when I click on this listItem. How do I get access to the Match object that's being stored in a particular listItem when I click on it.

Here is my Adapter/ViewHolder class for the RecyclerView:

package com.checkinsystems.ez_score.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.checkinsystems.ez_score.EditMatchActivity;
import com.checkinsystems.ez_score.MainActivity;
import com.checkinsystems.ez_score.MatchActivity;
import com.checkinsystems.ez_score.R;
import com.checkinsystems.ez_score.model.Match;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;



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

    private List<Match> mMatches;
    private final Context mContext;

    public MatchItemAdapter(Context context, List<Match> items) {
        this.mContext = context;
        this.mMatches = items;
    }

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

        LayoutInflater inflater = LayoutInflater.from(mContext);
        View itemView = inflater.inflate(R.layout.list_item_match, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MatchItemAdapter.ViewHolder holder, int position) {
        Match match = mMatches.get(position);

        try {
            holder.tvName.setText(match.getMatchName());
            holder.tvDate.setText(match.getMatchDate());
            holder.imageView.findViewById(R.id.imageView);
            holder.hiddenMatchId.setText(match.getMatchId());
            holder.container.findViewById(R.id.list_item_container);

            Typeface bold = Typeface.create("san-serif", Typeface.BOLD);

            SharedPreferences sharedPreferences = mContext.getSharedPreferences(
                    MY_GLOBAL_PREFS, mContext.MODE_PRIVATE);
            String currentMatchName = sharedPreferences.getString(CURRENT_MATCH_NAME, "current_match_name");


            String itemName = holder.tvName.getText().toString();

            if (itemName.equals(currentMatchName)) {
                holder.tvName.setTypeface(bold);
                holder.container.setBackgroundColor(Color.parseColor("#a4a4a4"));
                holder.tvName.setTextSize(24);
                holder.matchIsActive = true;


                SharedPreferences.Editor editor = mContext.getSharedPreferences(MY_GLOBAL_PREFS, mContext.MODE_PRIVATE).edit();
                editor.putString(CURRENT_MATCH_ID, match.getMatchId());
                editor.putString(CURRENT_MATCH_NAME, match.getMatchName());
                editor.putString(CURRENT_MATCH_CLUB_ID, match.getClubId());
                editor.putString(CURRENT_MATCH_DATE, match.getMatchDate());
                editor.putString(CURRENT_MATCH_LEVEL, match.getMatchLevel());
                int spinnerPos;
                switch (match.getMatchLevel()){
                    case "I":
                        spinnerPos = 0;
                        break;
                    case "II":
                        spinnerPos = 1;
                        break;
                    case "III":
                        spinnerPos = 2;
                        break;
                    case "IV":
                        spinnerPos = 3;
                        break;
                    default:
                        spinnerPos = 0;
                }
                editor.putInt(POSITION_OF_SPINNER, spinnerPos);
                editor.apply();

            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public int getItemCount() {
        return mMatches.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvName;
        public TextView tvDate;
        public ImageView imageView;
        public TextView hiddenMatchId;
        public View container;

        boolean matchIsActive = false;


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

            tvName = (TextView) itemView.findViewById(R.id.match_name);
            tvDate = (TextView) itemView.findViewById(R.id.match_date);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            hiddenMatchId = (TextView) itemView.findViewById(R.id.match_id_hidden);
            container = (View) itemView.findViewById(R.id.list_item_container);

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

                    final PopupMenu popup = new PopupMenu(view.getContext(), imageView);
                    if (!matchIsActive) {
                        popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                switch (item.getItemId()) {
                                    case R.id.edit_match:

                                        /////////////////////////////////////////////////////////
                                        // Make this match the current one before sending the intent
                                        String matchId = hiddenMatchId.getText().toString();

                                        Intent intent = new Intent(itemView.getContext(), EditMatchActivity.class);
                                        intent.putExtra(EditMatchActivity.EXTRA_MATCH_ID, matchId);
                                        itemView.getContext().startActivity(intent);
                                        break;
                                    case R.id.delete_match:


                                        // to delete a match here
                                        // also remove from recyclerview

                                        break;
                                }
                                return false;
                            }
                        });
                    } else {

                        // If the current item is selected and you click on the side menu -> don't show delete
                        // delete isn't an option for active Matches
                        popup.getMenuInflater().inflate(R.menu.popup_menu_no_delete, popup.getMenu());
                        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                switch (item.getItemId()) {
                                    case R.id.edit_match:
                                        // Make this match the current one before sending the intent


                                        Intent intent = new Intent(itemView.getContext(), EditMatchActivity.class);
                                        itemView.getContext().startActivity(intent);
                                        break;

                                }
                                return false;
                            }
                        });
                    }

                    //displaying the popup
                    popup.show();
                }
            });

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

                    matchIsActive = true;   // maybe this is redundant

                    String matchName = tvName.getText().toString();
                    Gson gson = new Gson();

                    SharedPreferences.Editor editor = itemView.getContext().getSharedPreferences(
                            MY_GLOBAL_PREFS, itemView.getContext().MODE_PRIVATE).edit();
                    //editor.putString(CURRENT_MATCH_NAME, matchName);
                    editor.apply();


                    Intent intent = new Intent(itemView.getContext(), MainActivity.class);
                    itemView.getContext().startActivity(intent);

                }
            });

        }

    }
}

Solution

  • as mentioned by pavel, inside of your onBindViewHolder() function is where you should set the listeners for your items.

    holder.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) { .... } });
    

    and

    holder.container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) { ... } });