简体   繁体   English

Android-如何更新其他活动的数据。 在您正在从事的工作之外

[英]Android- How to update data on another activity. Outside the one you're working on

I've got a simple android app that organises a list of items. 我有一个简单的android应用,可组织项目列表。 I'm still a beginner so forgive any ambiguity in the question. 我仍然是初学者,因此请原谅任何疑问。

The main activity consists of a list of existing items & two buttons, add item, and retrieve items. 主要活动包括现有项目列表和两个按钮,添加项目和检索项目。 Retrieving item pulls all current items from an SQLite database and displays them. 检索项目会从SQLite数据库中提取所有当前项目并显示它们。

When I add an item, it works fine and adds a new item to the database and when I close the activity that handles adding an item, the new item doesn't appear automatically, I have to retrieve the data again. 当我添加一个项目时,它可以正常工作并向数据库中添加一个新项目,并且当我关闭处理添加项目的活动时,该新项目不会自动出现,我必须再次检索数据。 I want to be able to see a new item on the screen as soon as I close the add item activity. 我希望能够在关闭添加项目活动后立即在屏幕上看到新项目。 The code for retrieving items in a database in the main activity is: 在主要活动中用于检索数据库中项目的代码是:

MovieDBAdapter dbadapter = new MovieDBAdapter(this);
ArrayList<HashMap<String, String>> movieList =  dbadapter.getMovieList();
ListAdapter adapter = new SimpleAdapter(MainActivity.this, movieList, R.layout.view_movie_entry, new String[] {"id", "title"}, new int[] {R.id.movie_Id, R.id.title});
setListAdapter(adapter);

I tried adding this code in the other activity but it threw an error because the setListAdapter method only applies to classes which extend listActivity and the class which adds a new item doesn't. 我尝试在其他活动中添加此代码,但引发了错误,因为setListAdapter方法仅适用于扩展listActivity的类,而添加新项的类则不适用。

Any suggestions on how to do this would be great. 关于如何执行此操作的任何建议都很好。

Summary: I want to update activity straight away without having to call a separate method in the mainActivity class. 摘要:我想立即更新活动,而不必在mainActivity类中调用单独的方法。

Call addItem Activity using 使用调用addItem Activity

 startActivityForResult(Intent intent,  int requestCode);

once the item in added call 一旦添加通话中的项目

 setResult(resultCode)

So then implement the following in Other activity 因此,请在其他活动中实现以下内容

 @Override
 protected void onActivityResult(int requestCode, int resultCode, 
  Intent data) {
// Check which request we're responding to
// here update the list
}

So setReult calls OnActivityResult once the item is added. 因此,一旦添加项目,setReult就会调用OnActivityResult。

暂无
暂无

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

相关问题 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 如何从一个活动转移到活动之外的另一个活动 - how to move from an activity to another one from outside an activity android RecyclerView Item Count On TextView 仅在重新进入活动时更新。 如何让它实时更新 - RecyclerView Item Count On TextView Only Updates When Re-entering the activity. How To Make It Update In RealTime Android-如何将数据从活动发送到服务? - Android- How to send data from activity to service? Android-从列表活动返回数据 - Android- Returning data from list activity 从一种方法调用时,Android AlertDialog 不会显示,但会从同一活动中的另一种方法显示。 为什么? - Android AlertDialog doesn't get displayed while calling from one method but it gets displayed from another method in the same activity. Why? 如何将数据/参数传递给 Android 中的另一个活动 - How do you pass data/parameters to another activity in Android 如何在不重新创建(重新启动)活动的情况下更新 RecyclerView。 我使用 SQLite 数据库将数据存储和检索到 Recycler 视图 - How to update RecyclerView without recreating (restarting) the activity. I use SQLite Database to store and retrieve data to the Recycler view 我正在尝试将一个活动中的用户输入存储到另一个活动中的列表视图中。 它不会工作 - I am attempting to store user input in one activity to a listview in another activity. It wont work Android-从另一个活动onButtonClick向ListView添加项目 - Android- Adding item to ListView from another activity onButtonClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM