从ArrayList Android返回值

时间:2014-11-25 09:07:19

标签: android arraylist

我正在尝试返回一组值。我这样做并设法检索JSONService中的值。如何在OneMap.Java中的方法 updateUI 中检索值数组?我在updateUI()

的第一行遇到了一个nullexception错误

OneMap.java

public class OneMap extends Activity implements OnSingleTapListener
{
    private Map map=null;
    private ArrayList<Map> list = null; 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
      loadData();
    }
    private void loadData() {
       new GetCoordinationsDetailsTask().execute();
    }
   private class GetCoordinationsDetailsTask extends AsyncTask<String, String, Void> {
    @Override
    protected void onPreExecute() {

    };

    @Override
    protected Void doInBackground(String... params) {
        list=JSONService.getLocationList();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        updateUI();
    }
}

private void updateUI() {
    String name=map.getName();
    String region=map.getRegion();
    String iconurl=map.getIconURL();
    String address=map.getAddress();
    String contactno=map.getContactNo();
    String operatinghrs=map.getOperatingHrs();
    String coordinates=map.getCoordinates();

JSONService.java

public static ArrayList<Map> getLocationList() {
    String url = second_URL + "storelocator.php";
    String method = GET;
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    JSONObject result = makeHttpRequest(url, method, params);

    try {
        Log.d("result",result.toString());
        if (result.getInt("success") == 1) {
            JSONArray jLocationList = result.getJSONArray("store_list");
            ArrayList<Map> list = new ArrayList<Map>();

            int count = jLocationList.length();
            for (int i = 0; i < count; i++) {
                JSONObject jlocation = jLocationList.getJSONObject(i);
                String LocationName=jlocation.getString("mall");
                String region=jlocation.getString("region");
                String iconURL=jlocation.getString("image");
                String address=jlocation.getString("address");
                String telephone=jlocation.getString("contact");
                String operatinghrs=jlocation.getString("Operating");
                String coordinates=jlocation.getString("coordinates");
                Map location= new Map(LocationName,region,iconURL,address,telephone,operatinghrs,coordinates);
                //list.add(location);
            }
            return list;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

Map.java

public class Map {

private String name=null;
private String region=null;
private String iconURL=null;
private String address=null;
private String contactNo=null;
private String operatinghrs=null;
private String coordinates=null;


public Map(String name,String region,String iconURL,String address, String contactNo, String operatinghrs,String coordinates)
{
    this.name=name;
    this.region=region;
    this.iconURL=iconURL;
    this.address=address;
    this.contactNo=contactNo;
    this.operatinghrs=operatinghrs;
    this.coordinates=coordinates;
}

public String getName()
{
    return name;
}
public String getRegion()
{
    return region;
}
public String getIconURL()
{
    return iconURL;
}
public String getAddress()
{
    return address;
}
public String getContactNo()
{
    return contactNo;
}
public String getOperatingHrs()
{
    return operatinghrs;
}
public String getCoordinates()
{
    return coordinates;
}
}

2 个答案:

答案 0 :(得分:1)

您忘记在OneMap中创建Map实例

答案 1 :(得分:0)

onPostExecute调用updateUI,它使用未初始化的地图对象。另一方面,您的JSONService.getLocationList会返回ArrayList<Map>。您可以从此ArrayList获取元素以填充UI。