在旧的代码中,当我使用自定义适配器的ListView
时,我可以用这段代码获取项目。
Message msg = (Message) adapter.getItem(poistion);
现在我正在实现RecyclerView
。如何从RecyclerView.adapter
中获取项目?
在旧的代码中,当我使用自定义适配器的ListView
时,我可以用这段代码获取项目。
Message msg = (Message) adapter.getItem(poistion);
现在我正在实现RecyclerView
。如何从RecyclerView.adapter
中获取项目?
你需要自己实现它。
在你的自定义适配器中添加一个新方法,就完成了。
public Message getItem(int position) {
return messageList.get(position);
}
我认为正确的方法是通过RecyclerView LayoutManager
来调用getItem()
。
View v = recyclerView.getLayoutManager().findViewByPosition(position);
Onclicklistener起了作用。
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;
}
}
}
RecyclerView rv_list_frames;
public frames getItem(int position) {
return rv_list_frames.get(position);
}
我希望以上内容可以帮助您获取物品。
@Override public void onClick(View view, int position) {
System.out.println("cardetail_activity"+position);
Intent i = new Intent(getActivity(), CarDetail.class);
ListingDataprovider data_provider=arrayList.get(position); i.putExtra("ID",data_provider.getId()); getActivity().startActivity(i);
}
Message
对象作为返回值,你需要在适配器中将项目列表设置为List<Message> mItems;
。 - Davideas