簡體   English   中英

setListAdapter無法解析

[英]setListAdapter unable to be resolved

一個真正的業余開發人員迫切需要一些幫助。 我一直在嘗試解析JSON數據並將其轉換為項目列表,但是盡管將其導入到類中,但在嘗試使用setListAdapter方法時遇到了困難。 任何幫助將不勝感激。

這是我的主要活動,錯誤在onPostExecute方法中

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;

import android.app.Activity;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;


public class JSON extends Activity {

    // Coordinates used for centering the Map

    private final static String UNAME = "aporter";
    private final static String URL = "http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username="
            + UNAME;

    public static final String TAG = "MapsEarthquakeMapActivity";

    // Set up UI and get earthquake data
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        new HttpGetTask().execute(URL);

    }

    private class HttpGetTask extends
            AsyncTask<String, Void, List<GeonameRec>> {

        AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

        @Override
        protected List<GeonameRec> doInBackground(String... params) {

            HttpGet request = new HttpGet(params[0]);
            JSONResponseHandler responseHandler = new JSONResponseHandler();

            try {

                // Get Earthquake data in JSON format
                // Parse data into a list of EarthQuakeRecs

                return mClient.execute(request, responseHandler);

            } catch (ClientProtocolException e) {
                Log.i(TAG, "ClientProtocolException");
            } catch (IOException e) {
                Log.i(TAG, "IOException");
            }

            return null;

        }

        @Override
        protected void onPostExecute(List<GeonameRec> result) {

            if (null != mClient)
                mClient.close();
            setListAdapter(new ArrayAdapter<String>(
                 JSON.this,
                 R.layout.listitem, result));



        }
    }

}
}

這是我的類,在其中格式化JSON響應。

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class JSONResponseHandler implements
    ResponseHandler<List<GeonameRec>> {
@Override
public List<GeonameRec> handleResponse(HttpResponse response)
        throws ClientProtocolException, IOException {
    List<GeonameRec> result = new ArrayList<GeonameRec>();
    String JSONResponse = new BasicResponseHandler()
            .handleResponse(response);
    try {
        JSONObject object = (JSONObject) new JSONTokener(JSONResponse)
                .nextValue();
        JSONArray earthquakes = object.getJSONArray("earthquakes");
        for (int i = 0; i < earthquakes.length(); i++) {
            JSONObject tmp = (JSONObject) earthquakes.get(i);
            result.add(new GeonameRec(
                    tmp.getDouble("lat"),
                    tmp.getDouble("lng")));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}

}

setListAdapter方法在ListActivity中可用,而在Activity中不可用。 為此,必須在Activity子類中定義ListView並調用listView.setAdapter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM