0

This question is similar to Accessing items in CardView after it has been created and put into a RecyclerView. But I didn't understand the answer.

I have a CardViewAdapter ViewHolder -

public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView card_text;
    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        card_text = (TextView) itemLayoutView.findViewById(R.id.info_text);
    }
}

And I access it using OnBindViewHolder which is in the same, CardViewAdapter -

     public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.card_text.setText(mColorData[position]);
        viewHolder.card_text.setBackgroundColor(Color.parseColor("#616161"));
}

But I also have a RecyclerView and now I wish to access the TextView from this CardViewAdapter and use it in my RecyclerView Activity so that I can set an action when it is (long) clicked.

My RecyclerView -

public class RecyclerViewMainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

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

        String[] colorData = {
        //a simple string array
        }

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new CardViewDataMainAdapter(colorData);
    mRecyclerView.setAdapter(mAdapter);

    mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                //do stuff here
       }

      @Override
                public void onItemLongClick(View view, int position) {
                //I want to do stuff here using TextView from CardViewAdapter.
       }
   }

P.S. I've implemented OnClickListener and OnLongClickListener using https://stackoverflow.com/a/26826692/4458075

Community
  • 1
  • 1
Chinmay
  • 119
  • 2
  • 11
  • Where is your recyclerview code?? – Pankaj Dec 04 '15 at 18:00
  • In my latest edit :) – Chinmay Dec 04 '15 at 18:12
  • Your RecyclerView is different activity and the textview you are asking about having multiple instances in cardview adapter – Pankaj Dec 04 '15 at 18:50
  • Exactly. Is there any way to achieve it? – Chinmay Dec 04 '15 at 19:04
  • 1
    There are mutiple textview, how would you know which textview you have to use??? And why do you exactly needed the textview??? What you can do is make a json array and stored it in file and use that json array file in recycler view – Pankaj Dec 04 '15 at 19:07
  • I'm really new to Recyclerview and Adapters and android programming overall. Apparently I have no idea how to use json files (but i'll definitely look for it now). And about textview, no, there's just a single textview but multiple "positions". – Chinmay Dec 04 '15 at 19:20
  • Same textview at multiple position means its obvious that it will have different value at different position. – Pankaj Dec 05 '15 at 06:38

0 Answers0