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();
}
我在setLIstViewAdapter和GetlistView中遇到问题,它显示了一些错误并告诉我创建它的方法.. 并且doInBackground要求返回一个布尔值。可以帮助我清除这些错误 谢谢你们
答案 0 :(得分:0)
也许你可以试试这个:
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();
}
您需要使用activity
对象setListAdapter
和getListView
。如果您不希望doInBackground(...)
返回Boolean
,则可以使用Void
并返回空值。