Having trouble with async and ListAdapter can you help out?

旧城冷巷雨未停 提交于 2019-12-13 08:25:34

问题



    package com.example.simpleasync;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ProgressTask extends AsyncTask<String, Void, Boolean> {
 private ProgressDialog dialog;
 private ListActivity activity;
 private Context context;

 ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
 ListView lv ;

 private static String url = "http://api.cartperk.com/v1/supportedoperator";

 private static final String OPCODE="code";
 private static final String OPNAME="name";

 public ProgressTask(ListActivity activity) {
     this.activity = activity;
     context = activity;
     dialog = new ProgressDialog(context);
 }

 protected void onPreExecute() {
     this.dialog.setMessage("Progress start");
     this.dialog.show();
 }

@Override
protected Boolean doInBackground(final String... args) {
    JSONParser jParser = new JSONParser();
    JSONArray json = jParser.getJSONFromUrl(url);

    for (int i = 0; i < json.length(); i++)
    {
        try {
                JSONObject c = json.getJSONObject(i);
                String opcode = c.getString(OPCODE);
                String opname = c.getString(OPNAME);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(OPCODE,opcode);
                map.put(OPNAME,opname);
                jsonlist.add(map);
        }
        catch(JSONException e)
        {
            e.printStackTrace();
        }
    return null;
    }

}
@Override
protected void onPostExecute(final Boolean success) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    ListAdapter adapter = new SimpleAdapter(context, jsonlist,
            R.layout.list_item, new String[] { OPCODE, OPNAME}, new int[] {
                    R.id.opcode, R.id.opname});

    setListAdapter(adapter);

    // select single ListView item
     lv = getListView(); 
}

im having problem in setLIstViewAdapter and GetlistView its showing some error and telling me to create a method of it.. and doInBackground is asking to return a boolean. Can help me out in clearning those errors Thank you guys


回答1:


Maybe you can try this:

package com.example.simpleasync;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ProgressTask extends AsyncTask<String, Void, Void> {
 private ProgressDialog dialog;
 private ListActivity activity;
 private Context context;

 ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
 ListView lv ;

 private static String url = "http://api.cartperk.com/v1/supportedoperator";

 private static final String OPCODE="code";
 private static final String OPNAME="name";

 public ProgressTask(ListActivity activity) {
     this.activity = activity;
     context = activity;
     dialog = new ProgressDialog(context);
 }

 protected void onPreExecute() {
     this.dialog.setMessage("Progress start");
     this.dialog.show();
 }

@Override
protected Void doInBackground(final String... args) {
    JSONParser jParser = new JSONParser();
    JSONArray json = jParser.getJSONFromUrl(url);

    for (int i = 0; i < json.length(); i++)
    {
        try {
            JSONObject c = json.getJSONObject(i);
            String opcode = c.getString(OPCODE);
            String opname = c.getString(OPNAME);

            HashMap<String, String> map = new HashMap<String, String>();

            map.put(OPCODE,opcode);
            map.put(OPNAME,opname);
            jsonlist.add(map);
        }
        catch(JSONException e)
        {
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPostExecute(final Boolean success) {
  if (dialog.isShowing()) {
    dialog.dismiss();
  }

  ListAdapter adapter = new SimpleAdapter(context, jsonlist,
        R.layout.list_item, new String[] { OPCODE, OPNAME}, new int[] {
        R.id.opcode, R.id.opname});

  activity.setListAdapter(adapter);

  // select single ListView item
  lv = activity.getListView();
}

You need to use activity object to setListAdapter and getListView. And if you don't want doInBackground(...) return a Boolean, you can use Void and return a null.



来源:https://stackoverflow.com/questions/21695064/having-trouble-with-async-and-listadapter-can-you-help-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!