Reputation: 67
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.
Upvotes: 1
Views: 740
Reputation: 440
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.
Upvotes: 2