[英]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.