Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get item from RecyclerView adapter in Android

In old code when I was using ListView with custom adapter I could get item with this code.

Message msg = (Message) adapter.getItem(poistion);

Now I am implementing RecyclerView. How can I get item from RecyclerView.adapter?

like image 371
Rafael Avatar asked Apr 16 '15 04:04

Rafael


People also ask

How can I access any component outside RecyclerView from RecyclerView in Android?

The best way to do this is via a click listener which will allow you to set up a communication between your RecyclerView items and the widgets or views outside of the recycler View. And that's all.

What is RecyclerView adapter in android?

RecyclerView. Adapter base class for presenting List data in a RecyclerView , including computing diffs between Lists on a background thread. Base class for an Adapter. Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView .

What is the use of onBindViewHolder in Android?

It is called by RecyclerView to display the data at the specified position. This method is used to update the contents of the itemView to reflect the item at the given position.


3 Answers

You have to implement it yourself.

Add a new method in your custom Adapter and you are done.

public Message getItem(int position) {
  return messageList.get(position);
}
like image 77
pdegand59 Avatar answered Oct 11 '22 15:10

pdegand59


I think the correct way to getItem() is through RecyclerView LayoutManager.

View v = recyclerView.getLayoutManager().findViewByPosition(position);
like image 25
Volodymyr Kret Avatar answered Oct 11 '22 15:10

Volodymyr Kret


Onclicklistener did the trick

public class MainActivity extends AppCompatActivity {

    Context context;
    RecyclerView recyclerView;
    RelativeLayout relativeLayout;
    RecyclerView.Adapter recyclerviewAdapter;
    RecyclerView.LayoutManager recyclerViewLayoutManager;
    String[] numbers = { "ANDROID",
            "PHP",
            "BLOGGER",
            "WORDPRESS",
            "JOOMLA",
            "ASP.NET",
            "JAVA",
            "C++",
            "MATHS",
            "HINDI",
            "ENGLISH"};

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

        context = getApplicationContext();
        relativeLayout = (RelativeLayout)findViewById(R.id.relativelayout1);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerview1);
        recyclerViewLayoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(recyclerViewLayoutManager);

        recyclerviewAdapter = new RecyclerViewAdapter(context,numbers);
        recyclerView.setAdapter(recyclerviewAdapter);



    }


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

        String[] numvalues;
        Context context;
        View view1;
        ViewHolder viewHolder1;
        TextView textView;



        public RecyclerViewAdapter(Context context1,String[] numvalues1){
            numvalues = numvalues1;
            context = context1;


        }

        public  class ViewHolder extends RecyclerView.ViewHolder{

            public TextView textView;

            public ViewHolder(View v){
                super(v);
                textView = (TextView)v.findViewById(R.id.number_textview);
            }
        }
        @Override
        public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);
       //     view1.setOnClickListener(new MyOnClickListener());
            view1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int itemPosition = recyclerView.indexOfChild(v);
                    //    Toast.makeText(MainActivity.this,"Selected item position is---"+ itemPosition,Toast.LENGTH_SHORT).show();
                    textView = (TextView)v.findViewById(R.id.number_textview);
                    Toast.makeText(MainActivity.this,"Selected val of clicked position is---"+ textView.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });

            viewHolder1 = new ViewHolder(view1);



            return viewHolder1;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.textView.setText(numvalues[position]);
        }

        @Override
        public int getItemCount() {
            return numvalues.length;
        }
    }
}
like image 40
saurabh mishra Avatar answered Oct 11 '22 14:10

saurabh mishra