我有一个使用AsyncTask
的片段。但是,我想在ListView
中显示值。这是我对JSON数据的代码,但我不知道怎么做,所以我能够在ListView
中显示这些数据。请帮帮我。
代码:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class NavLeaveAppFragment extends Fragment {
ProgressDialog pDialog;
ArrayList<HashMap<String, String>> emplist = new ArrayList<HashMap<String, String>>();
private static String url = "http://203.192.191.137:8080/apitest";
// JSON Node names
private static final String TAG_EMPLOYEE = "aaData";
private static final String TAG_ARRAY = "employees";
private static final String TAG_ID = "id";
private static final String TAG_FIRSTNAME = "first_name";
private static final String TAG_LASTNAME = "last_name";
private static final String TAG_GROUP = "group";
private static final String TAG_STATUS = "status";
private static final String TAG_PAYTYPE = "paytype";
private static final String TAG_WORKCODE = "workcode";
JSONArray employees = null;
public static NavLeaveAppFragment newInstance() {
NavLeaveAppFragment fragment = new NavLeaveAppFragment();
return fragment;
}
public NavLeaveAppFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_nav_leave_applications, container, false);
new GetLeaveInfo().execute();
return rootview;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((Dashboard) activity).onSectionAttached(2);
}
private class GetLeaveInfo extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setTitle("PHRISM");
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
employees = jsonObj.getJSONArray("aaData");
for (int i = 0; i < employees.length(); i++) {
JSONArray jsonArray = employees.getJSONArray(i);
String id = jsonArray.getString(0);
String firstname = jsonArray.getString(1);
String lastname = jsonArray.getString(2);
String group = jsonArray.getString(3);
String status = jsonArray.getString(4);
String paytype = jsonArray.getString(5);
String workcode = jsonArray.getString(6);
String shiftcode = jsonArray.getString(7);
HashMap<String, String> employee = new HashMap<String, String>();
// adding each child node to HashMap key => value
employee.put("id", id);
employee.put("first_name", firstname);
employee.put("last_name", lastname);
employee.put("group", group);
employee.put("status",status);
employee.put("paytype", paytype);
employee.put("workcode", workcode);
employee.put("shiftcode", shiftcode);
Log.e("Values", id + " " + firstname + " " + lastname);
// adding employee to employee list
emplist.add(employee);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
答案 0 :(得分:0)
从JSON中检索信息,将其放入POJO类,将Array传递到自定义适配器(或将JSON直接传递给自定义ListView适配器)。有许多教程教授如何使用数据填充ListView,this就是其中之一。