我的申请表有问题。我的列表视图中填充了来自parse.com的数据,但是当用户未连接到互联网时,列表视图为空。当用户在线显示列表视图离线时,我没办法保存listview的适配器吗?
我尝试了各种方式并阅读了parse.com文档,但无济于事。这是我的代码。
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();
}
}
}
答案 0 :(得分:1)
Parse本身支持查询缓存和本地数据存储(在引擎盖下使用SQLite),它可以像以下一样简单:
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);
}
});
}
});
可以在Parse docs中找到更多信息,这一切都取决于您的应用至少在线一次。
答案 1 :(得分:0)
也许您可以使用SQLite临时存储数据。
我经常使用的针对Android的SQLite库
1. ActiveAndroid
2. GreeDAO
从服务器检索数据,然后存储在本地数据库中的第一步。下一步检查设备是否已连接到互联网。