如何将数据从解析保存到本地数据
我是Android的开端。 我在我的项目中有一个小问题,我想保存我的数据解析到本地数据,但我不知道该怎么办: 我从BUS_STOP解析数据到我的BusStop arraylist。 我在我的应用程序中添加了Parse.enableLocalDatastore(getApplicationContext());
。 这是我的代码下载数据到busstops:如何将数据从解析保存到本地数据
busStops = new ArrayList<>();
try {
ParseQuery<ParseObject> query = new ParseQuery<>("BUS_STOP");
query.orderByAscending("stop_id");
query.setLimit(2000);
listA = query.find();
for (ParseObject mBusStop : listA) {
BusStop newBusStop = new BusStop();
newBusStop.setName((String) mBusStop.get("name"));
newBusStop.setStreet((String) mBusStop.get("street"));
newBusStop.setStyle((String) mBusStop.get("Type"));
newBusStop.setNext_id((int) mBusStop.get("next_id"));
newBusStop.setBus_id((int) mBusStop.get("bus_id"));
newBusStop.setStop_id((int) mBusStop.get("stop_id"));
double x, y;
x = (double) mBusStop.get("coor_x");
y = (double) mBusStop.get("coor_y");
LatLng a = new LatLng(x, y);
newBusStop.setLatLngBus(a);
busStops.add(newBusStop);
}
} catch (com.parse.ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
,这是我班巴士站就
@ParseClassName("BUS_STOP")
public class BusStop extends ParseObject{
String name;
String street;
String style;
LatLng latLngBus;
int bus_id;
int next_id;
int stop_id;
public String getPName(){
return getString("name");
}
public void setPName(String name) {
put("name", name);
}
public String getPStreet(){
return getString("street");
}
public void setPStreet(String street) {
put("street", street);
}
public String getPStyle(){
return getString("Type");
}
public void setPStyle(String type) {
put("Type", type);
}
public double getMCoor_x(){
return getDouble("coor_x");
}
public void setMCoor_x(double coor_x) {
put("coor_x", coor_x);
}
public double getMCoor_y(){
return getDouble("coor_y");
}
public void setMCoor_y(double coor_y) {
put("coor_y", coor_y);
}
public int getMBus_id(){
return getInt("bus_id");
}
public void setMCoor_y(int bus_id) {
put("bus_id", bus_id);
}
public int getMNext_id(){
return getInt("next_id");
}
public void setMNext_id(int next_id) {
put("next_id", next_id);
}
public int getMStop_id(){
return getInt("stop_id");
}
public void setMStop_id(int stop_id) {
put("stop_id", stop_id);
}
public int getStop_id() {
return stop_id;
}
public void setStop_id(int stop_id) {
this.stop_id = stop_id;
}
public int getNext_id() {
return next_id;
}
public void setNext_id(int next_id) {
this.next_id = next_id;
}
public int getBus_id() {
return bus_id;
}
public void setBus_id(int bus_id) {
this.bus_id = bus_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public LatLng getLatLngBus() {
return latLngBus;
}
public void setLatLngBus(LatLng latLngBus) {
this.latLngBus = latLngBus;
}
}
如果我的方法是不错的方法,告诉我伟大的方式,请!
从我从您的代码和问题中了解到的情况,您在加载和分析数据时锁定了主线程(UI线程)。
主线程负责处理用户输入和所有的屏幕绘图。因此,如果一项操作花费太长时间,则应用程序将被冻结直至完成,对于用户来说,它看起来像是您的应用程序崩溃了。来自Udacity的
This video解释得非常好。
你可以阅读更多关于Thread
在Android here。
和this link向您展示了如何在背景上运行事物。
编辑:为AsyncTask
示例代码。采取从Android dev site。
AsyncTask
允许您在用户界面上执行异步工作。它在工作线程中执行阻塞操作,然后将结果发布到UI线程上,而无需您自己处理线程和/或处理程序。
要使用它,您必须子类AsyncTask
并实现doInBackground()
回调方法,该方法在后台线程池中运行。要更新您的用户界面,您应该实现onPostExecute()
,该屏幕提供doInBackground()
的结果并在UI线程中运行,以便您可以安全地更新UI。然后,您可以通过从UI线程调用来运行该任务。
例如,可以实现使用AsyncTask
这样前面的例子:
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
现在UI是安全的代码更简单,因为它的工作成应该在做的部分分离工作者线程和应该在UI线程上完成的部分。
你应该阅读有关如何使用此类的充分理解AsyncTask
参考,但这里是它是如何工作的简要概述:
- 您可以指定参数,进度值的类型和 任务的终值,使用泛型
- 方法
doInBackground()
自动在工人线程执行 -
onPreExecute()
,onPostExecute()
和onProgressUpdate()
都是 调用在UI线程上 - 通过
doInBackground()
返回的值发送到onPostExecute()
- 您可以随时在
doInBackground()
调用publishProgress()
到 UI线程 - 上执行
onProgressUpdate()
您可以随时取消任务,从任何线程
注意:使用工作线程时可能会遇到的另一个问题是在您的活动由于runtim意外重启e 配置更改(例如,当用户更改屏幕 方向),这可能会破坏您的工作线程。要了解如何在这些重新启动过程中以及如何正确地取消任务时取消该任务,请参阅源代码 以了解Shelves示例应用程序。
等等,你的代码是否工作? – Mauker
是啊它工作,但只加载数据在线...当我打开我的应用程序延迟2s的加载数据 –
你将不得不加载数据在后台线程,使用'服务','AsyncTask',工人'线程'或类似的东西。 – Mauker