简体   繁体   English

在其他活动中将项目添加到RecyclerView

[英]Add item to RecyclerView in other Activity

I'm working on a simple application, but I got stuck with the Android design architecture. 我正在开发一个简单的应用程序,但是我陷入了Android设计架构的困境。 In my main Activity, I have a RecyclerView which shows a list of items. 在我的主活动中,我有一个RecyclerView,其中显示了项目列表。 When I click the floating action button in the main Activity, a new Activity (input Activity) is started where a new item to the RecyclerView must be added. 当我单击主活动中的浮动动作按钮时,将启动一个新的活动(输入活动),必须在其中添加RecyclerView的新项目。

What is the best way to do this? 做这个的最好方式是什么?

  • I could share the ViewModel of the main Activity with the input Activity. 我可以与输入Activity共享主Activity的ViewModel。 But when I add an item to this shared ViewModel, the RecyclerView is not automatically updated. 但是,当我向共享的ViewModel添加项目时,RecyclerView不会自动更新。
  • I could create a result Intent in the input Activity. 我可以在输入Activity中创建结果Intent。 But there is no possibility to include a new Item() object in this Intent. 但是,不可能在此Intent中包含新的Item()对象。

Since I'm new to Android application development, I'm not sure how to do this. 由于我是Android应用程序开发的新手,因此我不确定如何执行此操作。

  1. Save data of Input activity in sqlite. 将输入活动的数据保存在sqlite中。
  2. Save a boolean flag on shared preference indicating data has changed. 在共享首选项上保存一个布尔标志,指示数据已更改。 You might use another integer(or string, depending on you implementation) for saving sqlite primary key of the newly added input. 您可以使用另一个整数(或字符串,取决于您的实现)来保存新添加的输入的sqlite主键。
  3. Exit Input Activity 退出输入活动
  4. Inside onResume() of Main activity, check if data is added, if added, grab its id and values. 在Main活动的onResume()内部,检查是否添加了数据,如果添加了数据,则获取其ID和值。

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

相关问题 从RecyclerView项目到其他活动的值 - Values from RecyclerView item to other Activity 从另一个 Activity (Kotlin) 将项目添加到 RecyclerView - Add item to RecyclerView from another Activity (Kotlin) 如何在收藏夹活动中添加recyclerview项目 - How to add recyclerview item in favorites Activity 从recyclelerview项目到具有共享元素的其他活动的活动转换并不顺利 - Activity transition from recyclerview Item to other activity with shared elements is not smooth 从其他片段添加 RecyclerView 的新项目 - add new item of RecyclerView from other fragment 从其他已删除的RecyclerView项目将项目添加到RecyclerView - Add Items to RecyclerView from other removed RecyclerView item 如何将值从 RecyclerView 项目传递和显示到其他活动? - How to pass and show Values from RecyclerView item to other Activity? 如何在不刷新其他项目的情况下在回收站视图中添加新项目 - How to add new item in recyclerview without refreshing other item 将项目添加到主要活动中嵌入片段的RecyclerView中 - Add Item to RecyclerView embebed in Fragment from Main Activity 在活动中获取recyclerView项的内容 - Getting the content of a recyclerView item in the Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM