Search code examples
androidsqliterefreshsimplecursoradapter

how to Refresh Database and then put the new data in a simplecursoradapter in android?


Mainactivity where I check the clicking of the refresh button

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Will close the drawer if the home button is pressed
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {
        case R.id.search:
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                return false;
            } else {
                // Legacy search mode for Eclair
                onSearchRequested();
                return true;
            }
        case R.id.refresh:
            startDownloadSchedule();
            // TODO: resolve this
            Toast.makeText(getApplication(), "Updating database", Toast.LENGTH_SHORT).show();
            return true;
    }
    return false;
}

private void startDownloadSchedule() {

    progressBar.clearAnimation();
    progressBar.setIndeterminate(true);
    progressBar.setProgress(100);
    progressBar.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));
    progressBar.setMax(100);
    progressBar.setVisibility(View.VISIBLE);
//JsonToDatabase gets data from google sheets in json format and then saves it in a database
    JsonToDatabase dataDownload = new JsonToDatabase(getApplicationContext());
    dataDownload.setOnJsonToDatabaseCallback(new JsonToDatabase.JsonToDatabaseCallback() {

        @Override
        public void onDataLoaded() {
            Toast.makeText(getApplication(), "done updating", Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
            TracksListFragment tracksListFragment1 = new TracksListFragment();
            tracksListFragment1.onCreate(new Bundle());
        }


    });
    dataDownload.startDataDownload();
  }

TracklistFragment.class ( this is the activity whose data I want to refresh)

public class TracksListFragment extends SmoothListFragment {
//public String[] columns;
//public int[] to;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    displaydata();


}

public void displaydata() {
   String[] columns = new String[]{DatabaseHelper.TABLE_COLUMN_NAME, DatabaseHelper.TABLE_COLOUMN_INFORMATION};
    // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
    int[] to = new int[]{R.id.textView_track_title, R.id.textView_track_information};
    DatabaseManager db = DatabaseManager.getInstance();
    if (getActivity() != null) {
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(
                getActivity(), R.layout.list_tracks, db.getTracks(), columns, to, CursorAdapter.NO_SELECTION);
        setListAdapter(mAdapter);
        Toast.makeText(getActivity(),"display data",Toast.LENGTH_SHORT).show();

    }

}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String text = ((TextView) v.findViewById(R.id.textView_track_title)).getText().toString();
    Intent intent = new Intent(getActivity(), TrackActivity.class);
    intent.putExtra("TRACK", text);
    startActivity(intent);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setEmptyText("No data present");
}

}

I am just stuck at how to display the new refreshed data. I have checked that the data gets downloaded. Any help would be appreciated. Thanks in advance.


Solution

  • Version the database. Add SharedPreferences for the database and increment it each time new data is added, then Call updateDb from Open helper.