使用AsyncTask在ListView中显示数据

3

我需要一点帮助。我想使用asynctask在ListView中显示数据,但是我不知道如何做,因为我是Android编程的新手...非常感谢任何帮助。

public class Main extends ListActivity {
Button buttonbg;   
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.listplaceholder);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    JSONObject json = JSONfunctions.getJSONfromURL("http://10.10.10.10/data.php");

    try{

        JSONArray  ip = json.getJSONArray("ip");

        for(int i=0;i<ip.length();i++){                     
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = ip.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("data1", e.getString("date"));
            map.put("data2", "Location:" +  e.getString("location") + "   Status:" + e.getString("status"));
            mylist.add(map);            
        }       
    }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "data1", "data2" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);

}}


1
你有查看文档吗? - dmon
1
请发布您的Logcat错误并告诉我们您的具体问题。 - Sam
我建议您查看文档。还有这个例子 - Paresh Mayani
2个回答

8

试试这个

new MyAsyncTask.execute("http://10.10.10.10/data.php");

将任务声明为

class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(String... params) {

        JSONObject json = JSONfunctions.getJSONfromURL(params[0]);

        try {
            JSONArray  ip = json.getJSONArray("ip");

            for (int i=0;i<ip.length();i++) {                     
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = ip.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("data1", e.getString("date"));
                map.put("data2", "Location:" +  e.getString("location") + "   Status:" + e.getString("status"));
                mylist.add(map);            
            } 
            return mylist
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        ListAdapter adapter = new SimpleAdapter(YourActivity.this, result , R.layout.main, 
                new String[] { "data1", "data2" }, 
                new int[] { R.id.item_title, R.id.item_subtitle });
        YourActivity.this.setListAdapter(adapter); // If Activity extends ListActivity
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);

    }

希望这能有所帮助。

你的活动名称是指代表您的活动。在这种情况下,它是“主要”... :) - Som
1
没问题!把它标记为答案,这可能会帮助其他人。 - Som

2
不要在onCreate()中下载任何数据 - 如果花费的时间太长,您将会收到ANR异常(Activity Not Responding)。您应该像您的问题中一样使用AsyncTask。关于AsyncTask,在Android网站上有非常好的示例:

http://developer.android.com/reference/android/os/AsyncTask.html

你应该将JSONfunctions.getJSONfromURL()放在doInBackground()里面,而将下面的所有内容放在onPostExecute()里面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接