android - 使用 AsyncTask 在 ListView 中显示数据

标签 android listview android-asynctask

我需要一点帮助。我需要使用 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);

}}

最佳答案

试试这个

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);

    }

希望对您有所帮助。

关于android - 使用 AsyncTask 在 ListView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10335718/

相关文章:

android - 如何获取对最后一个 backstack 弹出 fragment 的引用?

c# - 如何在 C# Winform 应用程序中将复选框添加到 ListView 列标题?

java.lang.IllegalStateException : The content of the adapter has changed but ListView did not receive a notification in android Studio

java - 如何为我的游戏创建计时器?

java - Scene2D Actor 在本应不可见的情况下在屏幕上闪烁 1 帧

android - 如何处理 loopj/android-async-http 中的后按 Action

java - 异步加载内容时闪烁

android - 如何将数组列表从 AsyncTask 返回到 Main Activity?

java - 无法从另一个 Activity 的数据库中导出 SQLite 数据库中的数据

.net - 绘制 ListView 列标题分隔符?