I have this code by how I can get data from a sqllite database and set it to a list view:
Cursor cursor = dbAdapter.getAllIncomingData();
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
Log.d("ADD/EDIT", cursor.getCount() + "");
int size = cursor.getCount();
incomingDataList = new ArrayList<IncomingData>(size);
incomingStringList = new ArrayList<String>(size);
for (int i = 0; i < size; i++) {
String serial = cursor.getString(cursor
.getColumnIndex("serial"));
String mapurl = cursor.getString(cursor
.getColumnIndex("mapurl"));
String datetime = cursor.getString(cursor
.getColumnIndex("datetime"));
// Device device=new Device(id, label, serial,
// contact);
Device dev = dbAdapter.getDeviceBySerial(serial);
IncomingData inData = new IncomingData(serial, mapurl,
datetime);
inData.setLabel(dev.getLabel());
inData.setContact(dev.getContact());
incomingDataList.add(inData);
cursor.moveToNext();
}
inAdapter = new IncomingDataArrayAdapter(
MatchpointHistoryActivity.this, R.layout.history_item,
incomingDataList);
historyList.setAdapter(inAdapter);
}
the data of the database is being updated by receiving sms, and I have written the insertion query in the receiver class, now I want that if the new data is added or deleted then the cursor will get notified and reset the adapter to the listview, I have to relaunch the activity to view the changes, but I need to notify the changes instant. I dont want to use timer to do this stuff all the time, is there any other way?
Now I got your question. This also can be done by BroadcastReceiver, now this time it will be your own custom broadcast receiver for e.g.,
"com.yourapppackage.DATABASE_CHANGED". Whenever you insert/delete data into database at that time send this broadcast.You have to register your custom receiver inside your list activity and in
onReceive()method update your list.e.g.,
inside AndroidManifest.xml
DatabaseChangedReceiver.java
Activity which register this receiver,
inside database insert/delete method,
NOTE: you need context reference inside database code.
so this will execute code where you’ve registered DatabaseChangedReceiver in activity.