I have an app, that has a service (using alarm manager to fire every minute). So while the user browses through the activities of the app, the service fires and updates a local SQLite table ("tbl_messages"). One of the activities holds a ListView with data from that table ("tbl_messages"). When the user is viewing that activity, I need the listview to be refreshed after the service fires and adds records to the table. How can I achieve this?
I use AsyncTask to get records (messages) from an external MySQL table and to insert them into the local SQLite. Where should I put a code and what code should that be?
I use a SimpleCursorAdapter for displaying the data in listView like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notif);
ArrayList<String> listItems = new ArrayList<String>();
SQLiteDatabase db = new myDbHelper(getApplicationContext()).getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM tbl_messages", null);
String[] fromFieldNames = new String[]
{myDbHelper.DATA_NOT, myDbHelper.TITLU_NOT, myDbHelper.TITLUCONT_NOT, myDbHelper.CONT_NOT};
int[] toViewIDs = new int[]
{R.id.tv_data, R.id.tv_titlu, R.id.tv_subtitlu, R.id.tv_mesaj};
// Create adapter
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this, // Context
R.layout.notif_item, // Row layout template
c, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIDs // View IDs to put information in
);
// Set the adapter for the list view
ListView myList = (ListView) findViewById(R.id.lv_notificari);
myList.setAdapter(myCursorAdapter);
myCursorAdapter.notifyDataSetChanged();
}
So this is done onCreate of the messagesActivity
How can I notifyDataSetChanged() from my AsyncTask class? Because my Adapter is declare locally in the activity onCreate, and it's not accessible from the service/AsyncTask class
Thank you