-2

如何使用asynctask从json显示listview如何放

onPreExecute()

onPostExecute()

做背景()

onProgressUpdate()

MainActivity.java

list = (ListView) activity.findViewById(R.id.listView1);
        ArrayList<HashMap<String, String>> followingList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(URL);

        try {
            // Getting Array of Following
            following = json.getJSONArray(KEY_FOLLOWING);

            // looping through All Following
            for(int i = 0; i < following.length(); i++){
                JSONObject c = following.getJSONObject(i);

                // Storing each json item in variable
                String nama = c.getString(KEY_NAMA);
                String instansi = c.getString(KEY_INSTANSI);
                String status = c.getString(KEY_STATUS);

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

                // adding each child node to HashMap key => value
                map.put(KEY_NAMA, nama);
                map.put(KEY_INSTANSI, instansi);
                map.put(KEY_STATUS, status);
                // adding HashList to ArrayList
                followingList.add(map);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        // Getting adapter by passing xml data ArrayList
        adapter1=new LazyAdapter(this, followingList);
        list.setAdapter(adapter1);

如何实现 asycntask 以显示列表视图?

仪表板任务

package net.drieanto.lagidimana;

import net.drieanto.lagidimana.library.DatabaseHandler;
import net.drieanto.lagidimana.library.JSONParser;
import net.drieanto.lagidimana.library.UserFunctions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class DashboardTask extends AsyncTask<String, Void, Integer> {

    private ProgressDialog progressDialog;
    private DashboardActivity activity;

    ListView list1;
    LazyAdapter adapter1;
    private ListView list;

    // All static variables
    static final String URL = "http://git.drieanto.net/LagiDimanaAPI/index.php/user/get_following/XX3";
    // JSON node keys
    private int responseCode = 0;
    static final String KEY_FOLLOWING = "following";
    static final String KEY_NAMA = "nama"; // parent node
    static final String KEY_INSTANSI = "instansi";
    static final String KEY_STATUS = "status";
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";

    // following JSONArray
    JSONArray following = null;

    public DashboardTask(DashboardActivity activity,
            ProgressDialog progressDialog) {
        this.activity = activity;
        this.progressDialog = progressDialog;
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... arg0) {
        // check for login response
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(URL);
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                String res = json.getString(KEY_SUCCESS);

                if (Integer.parseInt(res) == 1) {
                    responseCode = 1;

                } else {
                    responseCode = 0;
                    // Error in login
                }
            }

        } catch (NullPointerException e) {
            e.printStackTrace();

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return responseCode;
    }

    @Override
    protected void onPostExecute(Integer responseCode) {
        if (responseCode == 1) {
            progressDialog.dismiss();
            ArrayList<HashMap<String, String>> followingList = new ArrayList<HashMap<String, String>>();

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(URL);

            try {
                // Getting Array of Following
                following = json.getJSONArray(KEY_FOLLOWING);

                // looping through All Following
                for (int i = 0; i < following.length(); i++) {
                    JSONObject c = following.getJSONObject(i);

                    // Storing each json item in variable
                    String nama = c.getString(KEY_NAMA);
                    String instansi = c.getString(KEY_INSTANSI);
                    String status = c.getString(KEY_STATUS);

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

                    // adding each child node to HashMap key => value
                    map.put(KEY_NAMA, nama);
                    map.put(KEY_INSTANSI, instansi);
                    map.put(KEY_STATUS, status);
                    // adding HashList to ArrayList
                    followingList.add(map);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            // Getting adapter by passing xml data ArrayList
            adapter1 = new LazyAdapter(activity, followingList);
            list.setAdapter(adapter1);
        } else {
            progressDialog.dismiss();
            activity.showDashboardError(responseCode);

        }

    }

}

但是包含错误有什么问题吗?

4

1 回答 1

0

您可以使用以下

    ProgressDialog pd; 
    LazyAdapter adapter1;  
    ListView list; 
    String URL; 
    HashMap<String, String> map = new HashMap<String, String>();
    ArrayList<HashMap<String, String>> followingList = new ArrayList<HashMap<String, String>>();

在您的活动 onCreate()

    setContentView(R.layout.activity_main);
    list = (ListView)findViewById(R.id.listView1);    
    pd = new ProgressDialog(ActivityName.this);
    pd.setTitle("Processing..."); 
    URL ="your url";  
    new TheTask().execute();

将 Asynctask 定义为活动类中的内部类

    class extends AsyncTask<Void,Void,Void>
    {
    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    pd.show();
     }

   @Override
    protected Void doInBackground(Void... params) {
             // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(URL);

    try {
        // Getting Array of Following
        following = json.getJSONArray(KEY_FOLLOWING);

        // looping through All Following
        for(int i = 0; i < following.length(); i++){
            JSONObject c = following.getJSONObject(i);

            // Storing each json item in variable
            String nama = c.getString(KEY_NAMA);
            String instansi = c.getString(KEY_INSTANSI);
            String status = c.getString(KEY_STATUS)

            // adding each child node to HashMap key => value
            map.put(KEY_NAMA, nama);
            map.put(KEY_INSTANSI, instansi);
            map.put(KEY_STATUS, status);
            // adding HashList to ArrayList
            followingList.add(map);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
   }

   @Override
   protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    pd.dismiss();
    adapter1=new LazyAdapter(ActivityName.this, followingList);
    list.setAdapter(adapter1);
  }  
  } 

解释

在 UI 线程上调用 AsyncTask。

在 UI 线程上调用的 onPreExecute()。显示进度对话框

doInBackground() 在后台线程上调用。做一些操作。不要在此处更新 UI

在 UI 线程上调用的 OnPostExecute()。关闭对话框并在此处更新 ui。

有关更多信息,请查看文档

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

于 2013-05-10T08:12:46.523 回答