如何在Android设备中保存来自parse.com的数据?
我正在尝试将数据保存在应用程序中的某个位置,当您没有网络时,客户仍然可以将数据保存在设备上。
我正在使用此代码制作列表并且正在运行,只是在没有互联网时忽略了保存数据的问题。
package com.example.ronysueliton.patospizzas;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
/**
* Created by Rony Sueliton on 11/04/2015.
*/
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class Pizzarias extends ActionBarActivity {
// Declare Variables
Boolean bColorStatus = true;
TextView status;
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapterPizzarias adapter;
private List<WorldPopulation> worldpopulationlist = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pizzarias);
new RemoteDataTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pizzarias, menu);
//Os metodos abaixo são para mostrar o icone do aplicativo na action bar
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
return true;
}
/*@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}*/
//RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(Pizzarias.this);
// Set progressdialog title
mProgressDialog.setTitle("Carregando Pizzarias");
// Set progressdialog message
mProgressDialog.setMessage("Aguarde...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
worldpopulationlist = new ArrayList<WorldPopulation>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery query = new ParseQuery("GerenciarPizzariasPatos");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending,
query.orderByAscending("nome");
ob = query.find();
for (ParseObject country : ob) {
WorldPopulation map = new WorldPopulation();
map.setNome((String) country.get("nome"));
map.setEndereco((String) country.get("endereco"));
map.setTelefone((String) country.get("telefone"));
map.setStatus((String) country.get("status"));
worldpopulationlist.add(map);
}
} 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.listviewpizzarias);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapterPizzarias(Pizzarias.this,
worldpopulationlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
////
public class ParseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
// Add your initialization code here
Parse.initialize(this, "xxx", "xxx");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this
// line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
答案 0 :(得分:0)
Just add <?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'root';
$db_database = 'drmahima_com';
$link = mysqli_connect($db_host,$db_user,$db_pass,$db_database) or die('Unable to establish a DB connection');
mysqli_query($link, "SET names UTF8");
// function temp() {
$patient = mysqli_fetch_assoc(mysqli_query($link, "SELECT name,dob FROM patients WHERE id='69'"));
echo $patient['name'];
// }
// temp();
?>
after DGTSession
and add to logic that will check whether connection is available and would enable pin(map);
option in case of unavailable connection before executing query
答案 1 :(得分:0)
我改变了我的代码行,我尝试使用互联网parse.com博客的教程,但是给出了错误GET,用红线加下划线。
我尝试使用网站示例:http://blog.parse.com/announcements/take-your-app-offline-with-parse-local-datastore/
@Override
protected Void doInBackground(Void... params) {
// Create the array
worldpopulationlist = new ArrayList<WorldPopulation>();
try {
// Locate the class table named "Country" in Parse.com
//ParseQuery query = new ParseQuery("GerenciarPizzariasPatos");
String objectId = "GFiBnjCE4v";
ParseObject feed = ParseQuery.get(objectId); // error GET // Online ParseQuery result
feed.fetch();
feed.put("fechado", true);
feed.saveEventually();
//ParseQuery<ParseObject> query = ParseQuery.getQuery("GerenciarPizzariasPatos");
ParseQuery<ParseObject> query = ParseQuery.get("GerenciarPizzariasPatos") // error GET
.fromLocalDatastore()
.whereEquals("fechado", true)
.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
// "feed" ParseObject will be returned in the list of results
}
});
// Locate the column named "ranknum" in Parse.com and order list
// by ascending,
query.orderByAscending("nome");
ob = query.find();
for (ParseObject country : ob) {
WorldPopulation map = new WorldPopulation();
map.setNome((String) country.get("nome"));
map.setEndereco((String) country.get("endereco"));
map.setTelefone((String) country.get("telefone"));
map.setStatus((String) country.get("status"));
worldpopulationlist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}