我正在开发一个使用服务的Android应用程序。我从该服务获取信息并将json数据保存到arraylist中它没关系,但我需要来自另一个类的这些数据。我试图访问此数组列表,但每次尝试时它都为null。这是我的代码:
1 2 3 4
1 1 0 0
0 1 2 1
1 0 1 0
我要将此列表称为“
”的类public class AllVehicles extends android.app.Fragment {
public ArrayList<Vehicle> vehicleList= new ArrayList<Vehicle>();
public AllVehicles() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetAllVehicles getAllVehicles=new GetAllVehicles();
getAllVehicles.execute("tr101", member.getAuthToken().toString(), String.valueOf(member.getMemberId()), String.valueOf(member.getFirmId()));
}
//onPostExecute of async task
protected void onPostExecute(Long result) {
try {
JSONObject jsonObj = new JSONObject(response_vehicles);
JSONArray jsonArray = jsonObj.getJSONArray("VehicleOnlineList");
for (int i = 0; i < jsonArray.length(); i++) {
vehicle = new Vehicle();
vehicle_report=new Vehicle();
JSONArray jsonArr = jsonArray.getJSONArray(i);
vehicle.setVehicleId(jsonArr.isNull(0) ? 0 : jsonArr.getInt(0));
vehicle.setDeviceId(jsonArr.isNull(1) ? 0 : jsonArr.getInt(1));
vehicle.setPlate(jsonArr.isNull(2) ? "" : jsonArr.getString(2));
vehicle.setVehicleName(jsonArr.isNull(3) ? "" : jsonArr.getString(3));
vehicleList.add(vehicle);
}
//The method for calling vehicleList:
public ArrayList<Vehicle> getVehicleReportList(){
return vehicleList;
}
答案 0 :(得分:1)
public static ArrayList<Vehicle> vehicleList= new ArrayList<Vehicle>();
并在您想要使用的地方使用AllVehicles.vehicleList
。
答案 1 :(得分:0)
使用回调的标准方式: 创建一个iterface,在片段中实现并将其上下文发送到AsyncTask类
以下是示例:
import android.content.Context;
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by Ramesh Kumar on 2/16/2016.
*/
public class AsycTaskTest extends
AsyncTask<String, String, ArrayList<Object>> {
Context activity;
InterfaceResult operations;
public AsycTaskTest(Context activity, InterfaceResult operations) {
this.operations = operations;
this.activity = activity;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
// new ProgressBar(Appli)
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onPostExecute(ArrayList<Object> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (operations != null)
operations.onSuccess(result);
}
@Override
protected ArrayList<Object> doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<Vehical> vehicleList =new ArrayList<>();
try {
//your code to access response_vehicles
JSONObject jsonObj = new JSONObject(response_vehicles);
JSONArray jsonArray = jsonObj.getJSONArray("VehicleOnlineList");
for (int i = 0; i < jsonArray.length(); i++) {
Vehicle vehicle = new Vehicle();
JSONArray jsonArr = jsonArray.getJSONArray(i);
vehicle.setVehicleId(jsonArr.isNull(0) ? 0 : jsonArr.getInt(0));
vehicle.setDeviceId(jsonArr.isNull(1) ? 0 : jsonArr.getInt(1));
vehicle.setPlate(jsonArr.isNull(2) ? "" : jsonArr.getString(2));
vehicle.setVehicleName(jsonArr.isNull(3) ? "" : jsonArr.getString(3));
vehicleList.add(vehicle);
}
// return list;
} catch (JSONException e) {
e.printStackTrace();
}
return vehicleList;
}
接口:
public interface InterfaceResult {
void onSuccess(ArrayList<Object> result);
}
当你在片段中实现它时,必须覆盖InterfaceResult的方法,数据将传递给Fragment的onSuccess并执行你想要的。
如果你不能得到任何东西,请随时提出。