samedi 13 février 2016

Listview not getting refreshed with SimpleCursorAdapter

I have written a program to update my listview using SimpleCursorAdapter. I have an edittext to enter mobile number and when i click on add , the mobile number is getting stored inside my sqlite database and at the same time displaying it inside the listview. But the problem is the listview is not getting refreshed , i have tried using notifyDataSetChanged() but not working .

Home_Page.class : This is the activity where i am updating my listview

 try{
        dbListHelper = new DriverSqliteHelper(getBaseContext());
        dbListHelper.open(getBaseContext());
    }catch (Exception e){
        e.printStackTrace();
    }

    columns = new String[]{DriverSqliteHelper.DbListHelper.DRIVER_USER_ID};
    to = new int[]{R.id.DriverId};
    getLoaderManager().initLoader(0, null, this);
    columns = new String[]{DriverSqliteHelper.DbListHelper.DRIVER_USER_ID};
    to = new int[]{R.id.DriverId};
    driverStatusAdapter = new DriverStatusAdapter(getBaseContext(),
    R.layout.view_userid_item,null,columns,to,0);
    listDriverId.setAdapter(driverStatusAdapter);

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    return new CustomCursorLoader(getBaseContext());
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if(driverStatusAdapter!=null && cursor!=null) {
        driverStatusAdapter.swapCursor(cursor);
        driverStatusAdapter.notifyDataSetChanged();
    }else {
        Log.v("Adapter null","OnLoadFinished: mAdapter is null");
    }
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    if(driverStatusAdapter!=null) {
        driverStatusAdapter.swapCursor(null);
    } else {
        Log.v("Adapter null", "OnLoadReset: mAdapter is null");
    }
}

@Override
protected void onResume() {
    super.onResume();
    getLoaderManager().restartLoader(0x01, null, this);
}

I have used CustomCursorLoader class for quering the results.

public class CustomCursorLoader extends CursorLoader{

Context context;
DriverSqliteHelper driverSqliteHelper;
Cursor cursor;

public CustomCursorLoader(Context context) {
    super(context);
    try {
        driverSqliteHelper = new DriverSqliteHelper(context);
        driverSqliteHelper.open(context);
    }catch (Exception e){
        e.printStackTrace();
    }
}

public Cursor loadInBackground(){
    cursor = driverSqliteHelper.getDriverStatus();
    return cursor;
}

}

When i restart my app the listview is updated but not automatically when i click on add button from the same activity. I have been stuck on this for a long time and i dont know what to do now to refresh the listview whnever i click on add. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire