【问题标题】:Android ListView Updated by AsyncTaskLoaderAndroid ListView 由 AsyncTaskLoader 更新
【发布时间】:2017-11-30 07:00:36
【问题描述】:

我正在从数据库中填充 ListView。来自数据库的记录集包含邮政编码,然后使用 google map api 将其转换为城市和州,然后设置为 Listview 项目。

我需要能够设置从 Listview 中的后台类返回的值。任何指导将不胜感激。提前致谢。

for (int i = 0; i < zipcodes.getLength(); i++) {
GetCityStateInfoFromPostalCode getCityStateInfoFromPostalCode = new GetCityStateInfoFromPostalCode(getActivity(), "110001", "ta");
String mCityState = getCityStateInfoFromPostalCode.getCityState();

}

这是从 Google 地图 api 获取信息的背景类

public class GetCityStateInfoFromPostalCode extends AsyncTaskLoader<String> {

private String URL;
private String mState = "";
private String mCity = "";
private Context mContext;

public String getCityState() {
    return mCityState;
}

private String mCityState = "";

public GetCityStateInfoFromPostalCode(Context context, String postalCode, String language) {

    super(context);
    this.mContext = context;
    URL = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:" + postalCode + "&language=" + language;

    // Kick start the load process
    forceLoad();
}

public String loadInBackground() {
    JSONObject jsonObject;
    JSONArray jsonRootArray;
    JSONArray jsonAdressArray;

    JSONObject addressComponentCityObject;
    JSONObject addressComponentStateObject;

    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    httpGet.addHeader("content-type", "application/json");

    try {

        HttpResponse resp = client.execute(httpGet);
        String json = EntityUtils.toString(resp.getEntity(), "UTF-8");
        jsonObject = new JSONObject(json);

        addressComponentCityObject = new JSONObject();
        addressComponentStateObject = new JSONObject();

        jsonRootArray = jsonObject.getJSONArray("results");

        //This points to "0"
        JSONObject rootJson = jsonRootArray.getJSONObject(0);

        //This points to address components
        jsonAdressArray = rootJson.getJSONArray("address_components");

        //This points to Object 1 (Second object of the jsonAddressArray)
        addressComponentCityObject = jsonAdressArray.getJSONObject(1);
        mCity = addressComponentCityObject.getString("long_name");

        addressComponentStateObject = jsonAdressArray.getJSONObject(3);
        mState = addressComponentStateObject.getString("long_name");



    } catch (Throwable t) {
        // Handle error here
        t.printStackTrace();
    }

    this.mCityState = mCity + ", " + mState;
    return mCityState;
}
}

【问题讨论】:

    标签: android listview android-fragments android-asynctask


    【解决方案1】:

    使用 AsyncTask,您可以生成和覆盖以下方法:

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    
    }
    
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
    
    }
    

    希望对你有帮助:)

    【讨论】:

    • 我正在使用“AsyncTaskLoader” - onPreExecute() 和 onPostExecute(Boolean aBoolean) 似乎不可用
    • 尝试将AsyncTaskLoader改为AsyncTask
    • 不高兴。无法得到想要的结果。卡住了。
    • 您使用 AsyncTaskLoader 或 AsyncTask 的方式是什么?
    • 如果是 AyncTaskLoader,一旦生成 loadInBackgroung 方法,在其上方添加 @Override。并使用方法 deliverResult(Object data) ,还有一件事,我还看到这些方法创建为返回一个对象,因此您还需要更改它以使其与您想要的任何内容保持一致。
    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多