Reputation: 20123
I have an Android activity that, onCreate(), will query a database and display the query results in textviews on the UI.
The problem that I am having is at some point, users can open another activity and update the database. When they go back to the original activity, I would like for the text views to update with the new database information (so, I will re query the database and display the info).
Another thing that I am willing to do is to take the new input from the user (in the new activity) and directly update the textview in the existing activity (without having the existing activity re-query the database). But I'm not sure how to do something like that either.
Can someone please inform me on the best way to go about doing this? The only solution that I have found so far has to do with broadcast receivers, but I'm not sure if that is best suited for what I have to do.
I'd appreciate any advice. Thanks!
Upvotes: 0
Views: 4778
Reputation: 624
if you are using setText in your onResume, you will still need to refresh/redraw via a handler. The easiest way I found was to redraw the base layout view.
LinearLayout myBaseLayout = (LinearLayout)findViewById(R.id.linearlayout01);
myBaseLayout.invalidate();
Just make sure it's in your handler method.
Upvotes: 0
Reputation: 121
You can do:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db.beginTransaction();
Cursor cur = null;
try {
cur = db.query("table",
null, null, null, null, null, null);
cur.moveToPosition(0);
String titleString = cur.getString(cur.getColumnIndex("title"));
db.setTransactionSuccessful();
} catch (Exception e) {
Log.e("Error in transaction", e.toString());
} finally {
db.endTransaction();
cur.close();
}
}
Transactions are really fast. You can use onResume() metod to fill the Textview.
Upvotes: 2
Reputation: 43088
if the amount of data to show in list view is not big the simpliest solution is just to requery in database in onResume() method. This method always get called when the activity shows up to the user again.
BroadcastReceiver is a good thing, but the effort doesn't worth for such simple task.
Upvotes: 3