ben*_*ill 0 java android parcelable android-asynctask
[我有一个自定义的parcelable对象车]
我看过AsyncTask但是关于这个主题还不太清楚:
我想将一个String(它是Vehicle的Vehicle ID)传递给AsyncTask,然后在doInBackground()中我有一个方法
mRemoteInterface.getTrackHistory();
Run Code Online (Sandbox Code Playgroud)
这给了我一个ArrayList.我想在onPostExecute()中启动一个活动,其中Vehicle ID和ArrayList都是额外的.
这是我无法做到的概述.问题是我不明白将对象INTO到asynctask,然后从doInBackground()到onPostExecute()和从onPostExecute()返回到原始的执行调用.
getTrackHistory.execute(这里应该去哪里?);
private class getTrackHistory extends
AsyncTask<String, Integer, Boolean **WHAT SHOULD GO HERE?**> {
@Override
protected Boolean doInBackground(
String... params) {
try {
String vehicleID = params[0];
listVehicleHistory = (ArrayList<Vehicle>) mRemoteInterface.getVehicleHistory(vehicleID);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(Boolean worked) {
super.onPostExecute(worked);
Intent intent = new Intent(MainActivity.this,VehicleInfo.class);
intent.putExtra("Vehicle ID", toReturn);
intent.putParcelableArrayListExtra("VehicleHistory", listVehicleHistory);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
}
您可以将字符串传递给asynctask或doInbackground的构造函数
new getTrackHistory(mystringvalue).execute();
Run Code Online (Sandbox Code Playgroud)
然后在构造函数中
private class getTrackHistory extends AsyncTask<String, Integer, Boolean> {
String value;
public getTrackHistory(String mystring) {
value = mystring;
}
Run Code Online (Sandbox Code Playgroud)
AsyncTask {
new TheTask().execute("mystring");
class TheTask extends AsyncTask <String,Void,Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
@Override
protected Void doInBackground(String... params) {
String value = params[0];
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
您还可以将值传递给 doInbackground()
new getTrackHistory(mystringvalue,ActivityName.this).execute();
Run Code Online (Sandbox Code Playgroud)
关于评论中的问题
String value;
TheInterface listener;
public getTrackHistory(String mystring,Context context) {
value= mystring;
listener = (TheInterface) context;
}
Run Code Online (Sandbox Code Playgroud)
在构造函数中
public interface TheInterface {
public void theMethod(ArrayList<String> result); // your result type
}
Run Code Online (Sandbox Code Playgroud)
接口
if (listener != null) {
listener.theMethod(result); // result is the ArrayList<String>
// result returned in doInbackground
// result of doInbackground computation is a parameter to onPostExecute
}
Run Code Online (Sandbox Code Playgroud)
然后
在doInbackground中返回结果.我假设它的StringList类型为String.将arraylist更改为适合您的要求.
在你的onPostExecute中
public class ActivityName implements getTrackHistory.TheInterface
Run Code Online (Sandbox Code Playgroud)
在您的活动类中实现接口
@Override
public void theMethod(ArrayList<String> result) { // change the type of result according yo your requirement
// use the arraylist here
}
Run Code Online (Sandbox Code Playgroud)
然后
new getTrackHistory(mystringvalue).execute();
Run Code Online (Sandbox Code Playgroud)
类似帖子使用界面
| 归档时间: |
|
| 查看次数: |
3941 次 |
| 最近记录: |