简体   繁体   中英

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. 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.

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.

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.

Call addItem Activity using

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM