How to get item from RecyclerView adapter in Android

后端 未结 5 1103
情歌与酒
情歌与酒 2021-02-01 12:37

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

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


        
5条回答
  •  醉梦人生
    2021-02-01 12:49

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

提交回复
热议问题