Ramprasad
Ramprasad

Reputation: 8071

How to update ListFragment whenever data updated in Sqlite Database

How to update ListFragment whenever data updated in Sqlite Database.My following Fragment code works properly,how to update this list fragment?

public class MyMasterFragment extends SherlockListFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*
        Runnable mRunnable=new Runnable() {
               public void run() {
                     Log.i("mylog", "refreshing...");
                     ListAdapter myListAdapter = getMyListAdapter();
                     setListAdapter(myListAdapter);
                     Log.i("mylog", "list Updated");
                     mHandler.postDelayed( this, 60*1000 );
                   }
        };

        mHandler.postDelayed( mRunnable, 0);
        */

        ListAdapter myListAdapter = getMyListAdapter();
        setListAdapter(myListAdapter);

    }

    private ListAdapter getMyListAdapter() {
        DBHelper dbHelper=new DBHelper(getActivity().getApplicationContext());
        SQLiteDatabase myDB = dbHelper.getWritableDatabase();


        ListAdapter adapter = null ;
            String TIME_SORT_ORDER=DBConstants.TIME+" DESC";
            String[] mycolumns={DBConstants.SNO,DBConstants.MYSYMBOL,DBConstants.VALUE1,DBConstants.DATE,DBConstants.TIME,DBConstants.VALUE2,DBConstants.VALUE3,DBConstants.VALUE4,DBConstants.VALUE5,DBConstants.VALUE6};
            Cursor c1 = myDB.query(DBConstants.LATEST_MY_DATA_TABLE,mycolumns, null, null, null, null, TIME_SORT_ORDER);

            if(c1.getCount()>0)
            {
                adapter=new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.live_list_item, c1, new String[] {DBConstants.MYSYMBOL,DBConstants.VALUE1,DBConstants.VALUE2,DBConstants.DATE,DBConstants.TIME}, new int[] {R.id.mysymbol, R.id.value1,R.id.value2,R.id.ldate,R.id.ltime});
            }
        myDB.close();
        dbHelper.close();
        return adapter;
    }
}

Also i have studied about Loader Manager in Android document.

  1. If i use loader manager it automatically updates my fragment list whenever data updated in my DB?
  2. Can i use Loader manager on Pre-honeycomb versionz using Support Library?

Upvotes: 0

Views: 705

Answers (1)

s.d
s.d

Reputation: 29436

If i use loader manager it automatically updates my fragment list whenever data updated in my DB?

Nope, you have to listen to some internal event, or external broadcast to re-run the load.

  • Services: When you bind with a service, you share an Interface IBinder to communicate. Using that , service can notify your Activity that it has updated the DB.

Can i use Loader manager on Pre-honeycomb versionz using Support Library?

Yes, you can.

Upvotes: 1

Related Questions