具有异步和ListAdapter麻烦你帮帮忙? [英] Having trouble with async and ListAdapter can you help out?
本文介绍了具有异步和ListAdapter麻烦你帮帮忙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package com.example.simpleasync;
具有setLIstViewAdapter和GetlistView问题及其表现出一些错误,并告诉我要创建它的方法IM ..
而doInBackground是要求返回一个布尔值。能否帮我在clearning这些错误
谢谢你们
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
推荐答案
也许你可以试试这个:
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();
}
您需要使用活动
对象 setListAdapter
和 getListView
。如果你不想让 doInBackground(...)
返回布尔
,您可以使用虚空
键,返回空。
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.
这篇关于具有异步和ListAdapter麻烦你帮帮忙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文