简体   繁体   English

如何将数据从recyclerview中的单击项传递到ViewModel并打开新活动?

[英]How to pass data from clicked item in recyclerview to viewmodel and open new activity?

As I wrote, I'm looking the solution to that problem. 如我所写,我正在寻找解决该问题的方法。 How to correctly in MVVM in Android pass texts and id clicked an item to ViewModel and open new activity? 如何正确地在android中的MVVM中传递文本和id单击一个项目到ViewModel并打开新活动?

The new activity is a detail of item. 新活动是项目的明细。 So when I click on the item I want to display new activity with data from the clicked item and I need item id to edit the object in the item. 因此,当我单击该项目时,我想显示新活动以及所单击项目的数据,并且需要项目ID来编辑该项目中的对象。

Using the RecyclerView.Adapter to populate your data, implement OnClickListener to viewHolder. 使用RecyclerView.Adapter填充数据,实现OnClickListener到viewHolder。

At onClick Method (overridden) you can type your code to start new activity, lets say you have model class called Test, and array list called testList. 在onClick方法(重写)上,您可以键入代码以开始新的活动,假设您有一个名为Test的模型类,以及一个名为testList的数组列表。 then: 然后:

Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra(KEY, testList.get(getAdapterPosition()));
mContext.startActivity(intent);

Note that getAdapterPosition() will return the position where you clicked, mContext is context passed to adapter. 请注意,getAdapterPosition()将返回您单击的位置,mContext是上下文传递给适配器的。

You will need your model class to implement Parcelable to allow model to be transferred between activities. 您将需要模型类来实现Parcelable,以允许在活动之间传递模型。 https://developer.android.com/guide/components/activities/parcelables-and-bundles https://developer.android.com/guide/components/activities/parcelables-and-bundles

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在传递单击的自定义列表项的数据时单击带有RecyclerView的片段项时打开新活动? - How to open a new activity when an item of a fragment with RecyclerView is clicked while passing the data of the clicked custom list item? 如何根据单击的recyclerview项目打开新活动? - How to open a new activity depending on the recyclerview item clicked? 如何根据RecyclerView中的单击项在新活动中打开特定片段 - How to open specific Fragment in new Activity depending on clicked item in RecyclerView 在Firebase RecyclerView中单击项目时如何打开新活动 - How to open a new activity when an item is clicked in Firebase RecyclerView 如何将数据从Recyclerview传递到新活动 - How to pass data from recyclerview to new activity 在 RecyclerView 的新活动中打开项目(来自 json 的数据) - Open item in new activity from RecyclerView (data from json) 如何从房间数据库中获取与 recyclerView 中单击项目相关的数据到新活动? - How to fetch data related to a clicked item in a recyclerView from room database to a new activity? 如何在RecyclerView中使用不同的数据和照片在项目单击上打开新的活动 - How to open new Activity on item click with different data and photo in RecyclerView 如何通过单击来自recyclerview的项目来打开新活动 - How to open new activity from clicking an item from recyclerview 在Firebase RecyclerView Cardview中单击项目时如何打开详细信息活动 - How to open a details activity when an item is clicked in Firebase RecyclerView Cardview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM