简体   繁体   中英

Fragment with ListView and JSON Asynctask

I have a fragment that uses an AsyncTask . However, I want to display the values in a ListView . This is the code I have for JSON data but I don't know how to do make it so I am able to display these data in a ListView . Please help me out.

Code:

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


    }
}

}

Retrieve the information from your JSON, put it in a POJO class, pass the Array into a Custom Adapter (or pass the JSON straight to your Custom ListView Adapter). There a many tutorials teaching how to fill a ListView with data, this is one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM