Android 如何将数据从解析保存到本地数据
我是Android的初学者。 我在项目中遇到了一个小问题,我想将解析中的数据保存到本地数据,但我不知道如何做: 我在parse中将数据从BUS_STOP加载到我的BUS STOP arraylist。 我添加了Android 如何将数据从解析保存到本地数据,android,parse-platform,Android,Parse Platform,我是Android的初学者。 我在项目中遇到了一个小问题,我想将解析中的数据保存到本地数据,但我不知道如何做: 我在parse中将数据从BUS_STOP加载到我的BUS STOP arraylist。 我添加了Parse.enableLocalDatastore(getApplicationContext())到我的应用程序。 这是我的代码下载数据到巴士站: busStops = new ArrayList<>(); try { ParseQuery<P
Parse.enableLocalDatastore(getApplicationContext())代码>到我的应用程序。
这是我的代码下载数据到巴士站:
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();
}
如果我的路是坏路,请告诉我好路 从您的代码和问题中我了解到,在加载和解析数据时,您锁定了主线程(UI线程)
主线程负责处理用户输入和所有屏幕绘图。因此,如果一个操作花费的时间太长,你的应用程序将被冻结,直到它完成,对用户来说,你的应用程序似乎崩溃了
Udacity对此解释得很好
您可以在Android上阅读更多关于Thread
并向您展示如何在后台运行
编辑:异步任务的示例代码。摘自《圣经》
AsyncTask
允许您在用户界面上执行异步工作。它在工作线程中执行阻塞操作,然后在UI线程上发布结果,而不需要您自己处理线程和/或处理程序
要使用它,必须子类化AsyncTask
并实现doInBackground()
回调方法,该方法在后台线程池中运行。要更新您的UI,您应该实现onPostExecute()
,它提供来自doInBackground()
的结果,并在UI线程中运行,因此您可以安全地更新UI。然后可以通过从UI线程调用execute()
来运行任务
例如,您可以通过以下方式使用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);
}
}
public void onClick(视图v){
新建DownloadImageTask()。执行(“http://example.com/image.png");
}
私有类DownloadImageTask扩展了AsyncTask{
/**系统调用它以在工作线程中执行工作,并
*将给定给AsyncTask.execute()的参数传递给它*/
受保护位图doInBackground(字符串…URL){
从网络返回loadImageFromNetwork(URL[0]);
}
/**系统调用它来执行UI线程中的工作,并提供
*doInBackground()的结果*/
受保护的void onPostExecute(位图结果){
设置图像位图(结果);
}
}
现在UI是安全的,代码更简单,因为它将工作分为应该在工作线程上完成的部分和应该在UI线程上完成的部分
您应该阅读参考资料以全面了解如何使用该类,但以下是该类工作原理的简要概述:
- 您可以指定参数的类型、进度值和
任务的最终值,使用泛型
- 方法
doInBackground()
在工作线程上自动执行
onPreExecute()
,onPostExecute()
,以及onProgressUpdate()
都是
在UI线程上调用
doInBackground()
返回的值被发送到onPostExecute()
- 您可以随时在
doInBackground()
中调用publishProgress()
在UI线程上执行onProgressUpdate()
- 您可以随时从任何线程取消任务
注意:使用工作线程时可能遇到的另一个问题是由于运行时导致活动意外重新启动
配置更改(例如用户更改屏幕时
方向),这可能会破坏工作线程。看看你能做什么
在其中一次重新启动期间持续执行任务,以及如何正确执行任务
当活动被销毁时取消任务,请参阅源代码
对于Shelfs示例应用程序
根据我从您的代码和问题中了解到的情况,在加载和解析数据时,您锁定了主线程(UI线程)
主线程负责处理用户输入和所有屏幕绘图。因此,如果一个操作花费的时间太长,你的应用程序将被冻结,直到它完成,对用户来说,你的应用程序似乎崩溃了
Udacity对此解释得很好
您可以在Android上阅读更多关于Thread
并向您展示如何在后台运行
编辑:异步任务的示例代码。摘自《圣经》
AsyncTask
允许您在用户界面上执行异步工作。它在工作线程中执行阻塞操作,然后在UI线程上发布结果,而不需要您自己处理线程和/或处理程序
要使用它,必须子类化AsyncTask
并实现doInBackground()
回调方法,该方法在后台线程池中运行。要更新您的UI,您应该实现onPostExecute()
,它提供来自doInBackground()
的结果,并在UI线程中运行,因此您可以安全地更新UI。然后可以通过从UI线程调用execute()
来运行任务
例如,您可以通过以下方式使用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);
}
}
public void onClick(视图v){
新建DownloadImageTask()。执行(“http://example.com/image.png");
}
私有类DownloadImageTask扩展了AsyncTask{
/**系统调用它以在工作线程中执行工作,并
*将给定给AsyncTask.execute()的参数传递给它*/
受保护位图doInBackground(字符串…URL){
从网络返回loadImageFromNetwork(URL[0]);
}
/**系统调用它来执行UI线程中的工作,并提供
*doInBackground()的结果*/
受保护的void onPostExecute(位图结果){