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);
}
}
}
しかし、何が問題なのかエラーが含まれていますか?