【问题标题】:Save data received from the site parse.com on the device在设备上保存从站点 parse.com 收到的数据
【发布时间】:2015-05-09 10:35:33
【问题描述】:

如何将来自 parse.com 的数据保存在 android 设备中?

我正在尝试将数据保存在应用程序中的某个位置,以便在您没有网络时,客户仍然可以获取保存在设备上的数据。

我正在使用此代码制作列表并且正在工作,只是错过了在您没有互联网时保存数据的问题。

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);
    }

}

【问题讨论】:

  • @Vivart。我已经完成但没有工作,我想知道在我的代码中实现它的正确方法是什么,因为我尝试了很多方法,但没有管理。
  • 也许你可以在你的应用程序中有一个数据库来保存数据并在互联网关闭时为你服务。只需将数据库中的数据刷新一段时间即可获得更新的数据。
  • @RonySueliton 你是否使用 Parse.enableLocalDatastore(getApplicationContext()) 启用本地数据库;在应用程序类中?
  • @Vivart。你引用的这个(getApplicationContext ());我没有,只是把Parse.enableLocalDatastore(this);放在教程里他说的地方,本来要铺的,但是已经铺好了,还没用。

标签: android parse-platform


【解决方案1】:

只需在worldpopulationlist.add(map); 之后添加pin(map); 并添加到将检查连接是否可用并在执行查询之前在连接不可用的情况下启用query.fromLocalDatastore() 选项的逻辑

【讨论】:

    【解决方案2】:

    我将我的代码行更改为这个,我尝试使用互联网 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;
        }
    

    【讨论】:

    • @Oleg Osipenko。我对代码进行了更改,尝试使用教程,但在获取时收到错误,用红线下划线和编译时出现此错误:Error:(100, 46) error: non-static method get(String) cannot be referenced from a static context where T is a type-variable: T extends ParseObject declared in class ParseQuery
    • @async。现在看看我正在尝试实现什么。
    • @Yarh。你可以看看我的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多