Search code examples
javaandroidandroid-recyclerviewtextview

how can i get the value of text view in recyclerview item?


This is my adapter for RecyclerView. How can I get the value of deckName in another activity?

public class ViewHolder extends RecyclerView.ViewHolder {
    public RelativeLayout deckLayout;
    public LinearLayout countsLayout;
    public ImageButton deckExpander;
    public ImageButton indentView;
    public  TextView deckName;
    public TextView deckNew, deckLearn, deckRev;

    public ViewHolder(View v) {
        super(v);
        deckLayout = (RelativeLayout) v.findViewById(R.id.DeckPickerHoriz);
        countsLayout = (LinearLayout) v.findViewById(R.id.counts_layout);
        deckExpander = (ImageButton) v.findViewById(R.id.deckpicker_expander);
        indentView = (ImageButton) v.findViewById(R.id.deckpicker_indent);
        deckName = (TextView) v.findViewById(R.id.deckpicker_name);
        deckNew = (TextView) v.findViewById(R.id.deckpicker_new);
        deckLearn = (TextView) v.findViewById(R.id.deckpicker_lrn);
        deckRev = (TextView) v.findViewById(R.id.deckpicker_rev);


    }
}

I want get this value and compare it with a string here:

final int itemCount = mDeckListAdapter.getItemCount();
DeckAdapter a = new DeckAdapter(getLayoutInflater(), getApplicationContext());
for(int i=0;i<itemCount;i++){
    //  final CharSequence text = DeckAdapter.ViewHolder.deckName.getText();
    if (text.equals(di)){
        mRecyclerView.findViewHolderForAdapterPosition(i).itemView.performClick();
    }
}

but this code doesn't really work.


Solution

  • Try this

    String title = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.title)).getText().toString();
    

    I used like

     recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, int position) {
                String title = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.title)).getText().toString();
                Toast.makeText(getApplicationContext(), title, Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onLongClick(View view, int position) { }
        }));