Reyjohn
Reyjohn

Reputation: 2714

How to get notified when data is changed on a database in android?

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?

Upvotes: 2

Views: 8670

Answers (2)

Kevin Adesara
Kevin Adesara

Reputation: 3830

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

<receiver
    android:name=".DatabaseChangedReceiver"
    android:enabled="true" >
       <intent-filter>
           <action android:name="com.youapppackage.DATABASE_CHANGED"></action>
       </intent-filter>
</receiver>

DatabaseChangedReceiver.java

public class DatabaseChangedReceiver extends BroadcastReceiver {
    public static String ACTION_DATABASE_CHANGED = "com.youapppackage.DATABASE_CHANGED";

    @Override
    public void onReceive(Context context, Intent intent) {

    }
}

Activity which register this receiver,

private DatabaseChangedReceiver mReceiver = new DatabaseChangedReceiver() {
   public void onReceive(Context context, Intent intent) {
       // update your list
   }

}

inside database insert/delete method, NOTE: you need context reference inside database code.

....
// code to insert/delete
....

context.sendBroadcast(new Intent(DatabaseChangedReceiver.ACTION_DATABASE_CHANGED));

so this will execute code where you've registered DatabaseChangedReceiver in activity.

Upvotes: 10

Narendra Pal
Narendra Pal

Reputation: 6604

@Reyjohn. You can use the boolean flag variable for this.Initially it is set to false Whenever insertion or any updation is done then change the flag to true. And after that you can simply check if flag is true then you have to notifyDataSetChanged() and again set the flag to false.

Upvotes: -3

Related Questions