【问题标题】:Fragment with ListView and JSON Asynctask带有 ListView 和 JSON Asynctask 的片段
【发布时间】:2015-07-07 00:32:20
【问题描述】:

我有一个使用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();


    }
}

}

【问题讨论】:

  • 您的列表视图在哪里我在您的代码中没有看到它。别告诉我你没试过

标签: android json listview android-fragments android-asynctask


【解决方案1】:

从您的 JSON 中检索信息,将其放入 POJO 类中,将数组传递给自定义适配器(或将 JSON 直接传递给您的自定义 ListView 适配器)。有很多教程教授如何用数据填充 ListView,this 就是其中之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    相关资源
    最近更新 更多