在ListView中解析LIstView中的JSON数据?

时间:2017-03-15 10:29:42

标签: android json listview

我能够解析ListView中的数据项列表.DataItems也有listView但我无法在LIstView中使用ListView。

费用类 outerListView类

 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<>();
                            ArrayList<StudentFeeInformation> student_list_description = new ArrayList<>();

                            JSONArray jArray = new JSONArray(response);
                            //  studentFeeInformation = new StudentFeeInformation(response);
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject jsonObject = jArray.getJSONObject(i);
                                String status = jsonObject.getString("Status");
                                String DateofReceiptIssued = jsonObject.getString("DateOfReciept").substring(0, 10);
                                String ReceiptNumber = jsonObject.getString("RecieptNo");
                                String FeeReceivedDate = jsonObject.getString("recivedDate").substring(0, 10);
                                student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate));
                            }
                            System.out.println("student_list size:" + student_list.size());
                            CustomFeeListStudentAdapter customFeeListStudentAdapter = new CustomFeeListStudentAdapter(getActivity(), student_list);
                            CustomFeeDescriptionListStudentAdapter customFeeDescriptionListStudentAdapter = new CustomFeeDescriptionListStudentAdapter(getActivity(), student_list_description);
                            listViewfees.setAdapter(customFeeListStudentAdapter);
                         //   listviewfeedetail.setAdapter(customFeeDescriptionListStudentAdapter); how can the 2nd listView be implemented


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


    }

CustomFeeListStudentAdapter (外部listView适配器)

public class CustomFeeListStudentAdapter extends BaseAdapter {


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

    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.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);
            viewHolder.student_profile_ReceiptIssuedDate = (TextView) convertView.findViewById(R.id.student_profile_fee_ReceiptIssuedDate);
            viewHolder.student_profile_ReceiptIssuedNumber = (TextView) convertView.findViewById(R.id.student_profile_fee_ReceiptIssuedNumber);
            viewHolder.student_profile_FeeReceivedDate = (TextView) convertView.findViewById(R.id.student_profile_fee_FeeReceiptDate);
        } 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());
        viewHolder.student_profile_ReceiptIssuedDate.setText(student_list.get(postion).getReceiptIssuedDate());
        viewHolder.student_profile_ReceiptIssuedNumber.setText(student_list.get(postion).getReceiptIssuedNumber());
        viewHolder.student_profile_FeeReceivedDate.setText(student_list.get(postion).getFeeReceivedDate());


        convertView.setTag(viewHolder);
        return convertView;
    }

    class Holder {
        TextView student_profile_fee_status;
        TextView student_profile_ReceiptIssuedDate;
        TextView student_profile_ReceiptIssuedNumber;
        TextView student_profile_FeeReceivedDate;

    }

StudentFeeInformation (模特类)

public class StudentFeeInformation implements Serializable {
    public String Status;
    public String ReceiptIssuedDate;
    public String ReceiptIssuedNumber;
    public String FeeReceivedDate;
    public String FeeDescription;


    public StudentFeeInformation(String status, String ReceiptissuedDate, String ReceiptissuedNumber, String FeereceivedDate) {
        Status = status;
        ReceiptIssuedDate = ReceiptissuedDate;
        ReceiptIssuedNumber = ReceiptissuedNumber;
        FeeReceivedDate = FeereceivedDate;


    }

    public String getStatus() {
        return Status;
    }

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

    public String getReceiptIssuedDate() {
        return ReceiptIssuedDate;
    }

    public void setReceiptIssuedDate(String receiptIssuedDate) {
        ReceiptIssuedDate = receiptIssuedDate;
    }

    public String getReceiptIssuedNumber() {
        return ReceiptIssuedNumber;
    }

    public void setReceiptIssuedNumber(String receiptIssuedNumber) {
        ReceiptIssuedNumber = receiptIssuedNumber;
    }

    public String getFeeReceivedDate() {
        return FeeReceivedDate;
    }

    public void setFeeReceivedDate(String feeReceivedDate) {
        FeeReceivedDate = feeReceivedDate;
    }


    public String getFeeDescription() {
        return FeeDescription;
    }

    public void setFeeDescription(String feeDescription) {
        FeeDescription = feeDescription;
    }


}

fragment_fee (外部布局)

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/linear_receipt1_fee"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#008b8b"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/student_profile_fee_ReceiptIssuedDate"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text="Date"
                        android:textColor="#fff"
                        android:textStyle="bold" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="match_parent"
                        android:background="#FFFFFF" />

                    <TextView
                        android:id="@+id/student_profile_fee_ReceiptIssuedNumber"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text="Receipt number"
                        android:textColor="#fff"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/receipt1_fee"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

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

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:padding="5dp"
                            android:text="Description"
                            android:textSize="12dp"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center_horizontal"
                            android:padding="5dp"
                            android:text="Amount (Nrs) "
                            android:textSize="12dp"
                            android:textStyle="bold">


                        </TextView>

                    </LinearLayout>

                    <ListView  //inner listview
                        android:id="@+id/list_student_fees_description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        />

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>

CustomFeeDescriptionListStudentAdapter (内部适配器)

public class CustomFeeDescriptionListStudentAdapter extends BaseAdapter {


    Context mContext;

    ArrayList<StudentFeeInformation> student_list_fee_description;
    String TAG = "HomeTab_adapter";

    public CustomFeeDescriptionListStudentAdapter(Context mContext, ArrayList<StudentFeeInformation> student_list_fee_description) {
        super();
        this.mContext = mContext;
        this.student_list_fee_description = student_list_fee_description;
    }

    @Override
    public int getCount() {
        return student_list_fee_description.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 CustomFeeDescriptionListStudentAdapter.Holder viewHolder;
        if (convertView == null) {
            // inflate the layout
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.student_fees_description_data, parent, false);

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

        } else {
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (CustomFeeDescriptionListStudentAdapter.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_description;
        TextView student_profile_fee_amount;


    }
}

student_fees_description (要填充的内部视图)

<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:orientation="horizontal">

        <TextView
            android:id="@+id/student_profile_fee_description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left|center"
            android:paddingLeft="70dp"
            android:paddingTop="10dp"
            android:text="Description"
            android:textSize="10dp" />
        <TextView
            android:id="@+id/student_profile_fee_amount"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left|center"
            android:paddingLeft="70dp"
            android:paddingTop="10dp"
            android:text="Amount"
            android:textSize="10dp" />



    </LinearLayout>
    <View
        android:layout_width="0.5dp"
        android:layout_height="match_parent"
        android:background="#C8CBCC" />
</LinearLayout>

我的json

[
  {
    "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"
  }
]

如何在listView中的数据项中填充描述?

0 个答案:

没有答案