无法在ListView中显示JSON数据?

时间:2017-03-14 09:40:31

标签: android listview android-volley

我无法使用Volley Library在ListView中显示JSON数据。

费用等级

public class Fee extends Fragment /*implements View.OnClickListener   */ {

    LinearLayout receipt1, receipt2, receipt3, receipt4;
    LinearLayout receipt1detail, receipt2detail, receipt3detail, receipt4detail;


    TextView statustextView;

    ListView listView;
    List<StudentFeeInformation> yourData = new ArrayList<StudentFeeInformation>();
    public static final String Navigation_URL = "http://192.168.100.5:84/Api/financeApi/getAllFees";
    //  public static final String Navigation_URL = "http://192.168.100.5:84/Api/financeApi/getAllFees?ID=111";
    String master_id;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.student_fees_listview, container, false);
        setHasOptionsMenu(true);
        //  receipt1 = (LinearLayout) view.findViewById(R.id.linear_receipt1_fee);
        //  receipt2 = (LinearLayout) view.findViewById(R.id.linear_receipt2_fee);
        //  receipt3 = (LinearLayout) view.findViewById(R.id.linear_receipt3_fee);
        //  receipt4 = (LinearLayout) view.findViewById(R.id.linear_receipt4_fee);

        //  receipt1detail = (LinearLayout) view.findViewById(R.id.receipt1_fee);
        //  receipt2detail = (LinearLayout) view.findViewById(R.id.receipt2_fee);
        //  receipt3detail = (LinearLayout) view.findViewById(R.id.receipt3_fee);
        //  receipt4detail = (LinearLayout) view.findViewById(R.id.receipt4_fee);

        //   receipt1.setOnClickListener(this);
        //   receipt2.setOnClickListener(this);
        //   receipt3.setOnClickListener(this);
        //   receipt4.setOnClickListener(this);


        //   receipt1detail.setVisibility(View.VISIBLE);
        //   receipt2detail.setVisibility(View.GONE);
        //   receipt3detail.setVisibility(View.GONE);
        //   receipt4detail.setVisibility(View.GONE);

        statustextView = (TextView) view.findViewById(R.id.student_profile_fee_status);


        SessionManagement sessionManagement = new SessionManagement(getContext());
        master_id = sessionManagement.getMasterId();

        listView = (ListView) view.findViewById(R.id.list_student_fees);
        CustomFeeListStudentAdapter customFeeListStudentAdapter = new CustomFeeListStudentAdapter(getContext(), R.layout.fragment_fee, yourData);
        listView.setAdapter(customFeeListStudentAdapter);


        getUsersListData();

        return view;
    }


    StudentFeeInformation studentFeeInformation;


    private void getUsersListData() {
        String URL = Navigation_URL + "?id=" + master_id;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {

                            JSONArray jArray = new JSONArray(response);
                            studentFeeInformation = new StudentFeeInformation();
                            for (int i = 0; i < jArray.length(); i++) {

                                JSONObject jsonObject = jArray.getJSONObject(i);
                                studentFeeInformation.Status = jsonObject.getString("Status");
                                System.out.println("This is Good");
                                System.out.print(studentFeeInformation.Status);
                            }


                        } catch (JSONException e) {

                            System.out.println("This is not good");

                            e.printStackTrace();

                        }

                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // Toast.makeText(view.Fee.this, error.toString(), Toast.LENGTH_LONG).show();

            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                return headers;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);


    }
  }

StudentFeeInformation

public class StudentFeeInformation implements Serializable {
    public String Status;

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }
}

CustomFeeListStudentAdapter

public class CustomFeeListStudentAdapter extends ArrayAdapter<StudentFeeInformation> {

    private List<StudentFeeInformation> items;

    public CustomFeeListStudentAdapter(Context context, int resource) {
        super(context, resource);
    }

    public CustomFeeListStudentAdapter(Context context, int resource, List<StudentFeeInformation> items) {
        super(context, resource, items);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        TextView tt = null;


        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.fragment_fee, null);

            tt = (TextView) v.findViewById(R.id.student_profile_fee_status);

        }

        StudentFeeInformation p = items.get(position);

        if (p != null) {

            if (tt != null) {
                tt.setText("" + p.getStatus());
            }

        }


        return v;

    }


}

student_fees_listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2E353D"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:padding="3dp"
                android:src="@mipmap/fee" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="8dp"
                android:text="Fees"
                android:textColor="#fff"
                android:textSize="17dp"
                android:textStyle="bold" />

        </LinearLayout>
    </LinearLayout>


    <ListView
        android:id="@+id/list_student_fees"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />


</LinearLayout>

fragment_fee

  <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="#DCE2E8"
                            android:orientation="horizontal"
                            android:padding="5dp">

                            <TextView
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:paddingLeft="70dp"
                                android:text="Status"
                                android:textSize="10dp"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/student_profile_fee_status"
                                android:layout_width="0dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:paddingLeft="70dp"
                                android:text="Paid"
                                android:textColor="#108B89"
                                android:textSize="10dp"
                                android:textStyle="bold" />

                        </LinearLayout>

JSON

[
  {
    "MasterID": "E0017",
    "StdID": 111,
    "Status": "P",
    "AmountPaid": 6645,
    "Class": 8,
    "DateOfReciept": "2017-01-01T00:00:00",
    "Description": "[{\"des\":\"Admission\",\"Amount\":300},{\"des\":\"Monthly Fee\",\"Amount\":5400},{\"des\":\"Exam Fee\",\"Amount\":200},{\"des\":\"Extra Charge\",\"Amount\":400},{\"des\":\"Late Charge\",\"Amount\":345}]",
    "RecieptNo": 1011,
    "NAME": "Uzumaki Naruto",
    "recivedDate": "2017-03-10T00:00:00",
    "reciever": "Cynthia Irwin"
  },
  {
    "MasterID": "E0017",
    "StdID": 111,
    "Status": "U",
    "AmountPaid": 5600,
    "Class": 8,
    "DateOfReciept": "2017-03-01T00:00:00",
    "Description": "[{\"des\":\"Exam Fee\",\"Amount\":200},{\"des\":\"Monthly Fee\",\"Amount\":5400}]",
    "RecieptNo": 1012,
    "NAME": "Uzumaki Naruto",
    "recivedDate": "2017-03-06T00:00:00",
    "reciever": "Cynthia Irwin"
  }
]
  

如何使用JSON数据填充listView?请告诉我   我做错了。

2 个答案:

答案 0 :(得分:1)

似乎您错过了将studentFeeInformation添加到yourData列表中。在studentFeeInformation中添加创建的getUsersListData(),在完成for循环后,调用适配器的notifyDataSetChanged()

答案 1 :(得分:1)

费用等级

    public class Fee extends Fragment /*implements View.OnClickListener   */ {

        LinearLayout receipt1, receipt2, receipt3, receipt4;
        LinearLayout receipt1detail, receipt2detail, receipt3detail, receipt4detail;


        TextView statustextView;

        ListView listView;
        List<StudentFeeInformation> yourData = new ArrayList<StudentFeeInformation>();
        public static final String Navigation_URL = "http://192.168.100.5:84/Api/financeApi/getAllFees";
        //  public static final String Navigation_URL = "http://192.168.100.5:84/Api/financeApi/getAllFees?ID=111";
        String master_id;
        StudentFeeInformation studentFeeInformation;


        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.student_fees_listview, container, false);
            setHasOptionsMenu(true);

            statustextView = (TextView) view.findViewById(R.id.student_profile_fee_status);


            SessionManagement sessionManagement = new SessionManagement(getContext());
            master_id = sessionManagement.getMasterId();

            listView = (ListView) view.findViewById(R.id.list_student_fees);
            //CustomFeeListStudentAdapter customFeeListStudentAdapter = new CustomFeeListStudentAdapter(getContext(), R.layout.fragment_fee, yourData);
            //listView.setAdapter(customFeeListStudentAdapter);


            getUsersListData();

            return view;
        }

        private void getUsersListData() {
            String URL = Navigation_URL + "?id=" + master_id;
            StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {

                                ArrayList<StudentFeeInformation> student_list=new ArrayList<>();
                                JSONArray jArray = new JSONArray(response);

                                for (int i = 0; i < jArray.length(); i++) {

                                    JSONObject jsonObject = jArray.getJSONObject(i);
                                    String status = jsonObject.getString("Status");
                                    System.out.println("status:"+status);
                                    student_list.add(new StudentFeeInformation(status));
                                }

                                System.out.println("student_list size:"+student_list.size());
                                CustomFeeListStudentAdapter customFeeListStudentAdapter = new CustomFeeListStudentAdapter(getActivity(),  student_list);
                                listView.setAdapter(customFeeListStudentAdapter);
                            } catch (JSONException e) {
                                System.out.println("This is not good");
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Toast.makeText(view.Fee.this, error.toString(), Toast.LENGTH_LONG).show();

                }
            }) {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<String, String>();
                    return headers;
                }

            };

            RequestQueue requestQueue = Volley.newRequestQueue(getContext());
            requestQueue.add(stringRequest);
        }
      }

StudentFeeInformation模型类:

    public class StudentFeeInformation implements Serializable {
        public String Status;


        public StudentFeeInformation(String status) {
            Status = status;
        }

        public String getStatus() {
            return Status;
        }

        public void setStatus(String status) {
            Status = status;
        }
    }

CustomFeeListStudentAdapter:

    public class CustomFeeListStudentAdapter extends BaseAdapter {

        Context mContext;

        ArrayList<StudentFeeInformation>student_list;
        String TAG="HomeTab_adapter";
        public CustomFeeListStudentAdapter(Context mContext, ArrayList<StudentFeeInformation> student_list) {
            super();
            this.mContext = mContext;
            this.student_list=student_list;
        }

        @Override
        public int getCount() {
            return student_list.size();
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(final int postion, View convertView, ViewGroup parent) 
        {
            final Holder viewHolder;
            if(convertView==null)
            {
                // inflate the layout
                LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView=inflater.inflate(R.layout.R.layout.fragment_fee, parent, false);

                // well set up the ViewHolder
                viewHolder = new Holder();
                viewHolder.student_profile_fee_status = (TextView)convertView.findViewById(R.id.student_profile_fee_status);

            }
            else{
                // we've just avoided calling findViewById() on resource everytime
                // just use the viewHolder
                viewHolder = (Holder) convertView.getTag();
            }

            viewHolder.student_profile_fee_status.setText(student_list.get(postion).getStatus());


            convertView.setTag(viewHolder);
            return convertView;
        }

        class Holder{
            TextView student_profile_fee_status;

        }
    }