Populating listview with parse.com when user offline

I have a problem in my application. My listview is filled with data coming from parse.com, but when the user is not connected to the internet, the listview is empty. I would not have a way to save the adapter of listview while the user is online to display the listview offline?

I tried in various ways and read the parse.com documentation, but to no avail. Here is my code.

public class Eventos extends ActionBarActivity {

    ListView listview;
    List<ParseObject> ob;
    ProgressDialog mProgressDialog;
    ListViewEventos adapter;
    private List<GetEventos> eventos_lista = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventos);

        new RemoteDataTask().execute();
    }

    // RemoteDataTask AsyncTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(Eventos.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Carregando Dados...");
            // Set progressdialog message
            mProgressDialog.setMessage("Aguarde...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            eventos_lista = new ArrayList<GetEventos>();
            try {
                // Localizando a classe noticias no parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        "Eventos");
                //colocando por ordem de data
                query.orderByAscending("Number");
                ob = query.find();
                for (ParseObject titulo : ob) {
                    // Localizando as imagens na coluna foto do parse
                    ParseFile image = (ParseFile) titulo.get("Foto");
                    ParseFile img = (ParseFile) titulo.get("FotoEvento");

                    GetEventos eventos = new GetEventos();
                    eventos.setTitulo((String) titulo.get("Titulo"));
                    eventos.setDescricao((String) titulo.get("Descricao"));
                    eventos.setTextoEvento((String) titulo.get("TextoEvento"));
                    eventos.setFoto(image.getUrl());
                    eventos.setFotoEvento(img.getUrl());
                    eventos_lista.add(eventos);
                }

            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listViewEventos);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewEventos(Eventos.this,
                    eventos_lista);
            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }    
}

Upvotes: 0

Views: 200

Answers (2)

luffynas
luffynas

Reputation: 99

maybe you can use SQLite to store data temporarily.

SQLite library for Android that I often use
1. ActiveAndroid
2. GreeDAO

The first step to retrieve data from the server, and then stored in the local database. The next check, whether the device is connected to the internet or not.

Upvotes: 0

David Rothera
David Rothera

Reputation: 427

Parse natively supports both query caching and a local datastore (which uses SQLite under the hood), it can be as easy as:

final String TOP_SCORES_LABEL = "topScores";

// Query for the latest objects from Parse.
query.findInBackground(new FindCallback<ParseObject>() {
  public void done(final List<ParseObject> scoreList, ParseException e) {
    if (e != null) {
      // There was an error or the network wasn't available.
      return;
    }

    // Release any objects previously pinned for this query.
    ParseObject.unpinAllInBackground(TOP_SCORES_LABEL, scoreList, new DeleteCallback() {
      public void done(ParseException e) {
        if (e != null) {
          // There was some error.
          return;
        }

        // Add the latest results for this query to the cache.
        ParseObject.pinAllInBackground(TOP_SCORES_LABEL, scoreList);
      }
    });
  }
});

Some more information can be found in the Parse docs, this all ofcourse relies on your app being online at least once.

Upvotes: 1

Related Questions