-2

asynctask を使用して json からリストビューを表示する方法

onPreExecute()

onPostExecute()

doInBackground()

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

リストビューを表示するためにasyncntaskを実装するには?

ダッシュボードタスク

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

説明

AsyncTask は UI スレッドで呼び出されます。

UI スレッドで呼び出される onPreExecute()。進行状況ダイアログを表示する

バックグラウンド スレッドで呼び出される doInBackground()。何らかの操作を行います。ここで UI を更新しないでください

UI スレッドで呼び出される OnPostExecute()。ダイアログを閉じて、ここで ui を更新します。

詳細については、ドキュメントを確認してください

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

于 2013-05-10T08:12:46.523 に答える