golbahar
golbahar

Reputation: 63

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.

Upvotes: 5

Views: 15295

Answers (4)

Srikanth
Srikanth

Reputation: 1575

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) { }
    }));

Upvotes: 18

Applicant
Applicant

Reputation: 3

Try this.but this code will execute when press any place in recycler view. If you want to action when press title object only you must put event on title

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

    @Override
    public void onLongClick(View view, int position) { }
}));

Upvotes: 0

Aasik
Aasik

Reputation: 11

Check out this. It works for me.
Just Paste in Your Activity or Fragment
rvSelectedProductList = Recyclerview
selectedItemAdapter = RecyclerView Adapter

      rvSelectedProductList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                final int itemCount = selectedItemAdapter.getItemCount();

                for (int i = 0; i < itemCount; i++) {
                    TextView tvSelling = rvSelectedProductList.getChildAt(i).findViewById(R.id.tvSelling);
                    TextView textViewDrawerTitle = rvSelectedProductList.getChildAt(i).findViewById(R.id.tvCartQty);


                    String totalamount = tvSelling.getText().toString();
                    String qty = textViewDrawerTitle.getText().toString();
                    System.out.println("qty" + qty);
                    System.out.println("total" + totalamount);
                }
                rvSelectedProductList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });

Upvotes: 0

A.R.
A.R.

Reputation: 2641

You can try this.

Use below code where you want to retrieve the value from textview, so that you can iterate the value of each textview from your RecyclerView.

for(int i=0;i<itemCount;i++)
{
  View view=mRecyclerView.getChildAt(i); // This will give you entire row(child) from RecyclerView
   if(view!=null)
     {
       TextView textView= (TextView) view.findViewById(R.id.deckpicker_name);
        String text=textView.getText().toString();
        if (text.equals(di)){
             // Do your stuff, after comparison
        }
      }
}

I have not tested this, but hope it works for you somehow.

Upvotes: 3

Related Questions