在listView中添加了不必要的视图

时间:2017-06-07 08:55:32

标签: android json listview layout

我正在使用ListView来显示JSON数据。在ListView中我实现了动态LinearLayout以填充listitems。我能够在listView中以及在linearlayout中显示JSON数据。当程序被执行时视图被不必要地添加。我认为问题是循环,但不确切知道。如何解决这个问题?

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

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


    TextView statustextView, feedescription, feeamount;
    ListView listViewfees, listviewfeedetail;
    List<StudentFeeInformation> yourData = new ArrayList<StudentFeeInformation>();

    public static final String Navigation_URL = "http://192.168.100.5:84/Api/financeApi/getAllFees";
    String Amount;
    String Descriptionlist, status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate;
    ArrayList arrayList = new ArrayList();
    String master_id;

    LinearLayout linearLayout;
    TextView textView;
    //  public JSONArray jArray1;


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


        View view = inflater.inflate(R.layout.student_fees_listview, container, false);
        inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // view1 = inflater.inflate(R.layout.student_fees_description_data, null);


        setHasOptionsMenu(true);
        receipt1 = (LinearLayout) view.findViewById(R.id.linear_receipt1_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();
        listViewfees = (ListView) view.findViewById(R.id.list_student_fees);
        // listviewfeedetail = (ListView) view.findViewById(R.id.listtest);

        //  DescriptionAmount = (LinearLayout) view.findViewById(R.id.student_profile_fee_linearlayout1);
        // DescriptionAmount.addView(view);


        linearLayout = (LinearLayout) view.findViewById(R.id.student_profile_fee_linearlayout1);
        //   v = (LinearLayout)( view.inflate(getContext(), R.layout.student_fees_description_data, null));
        //  linearLayout.addView(view);

        feedescription = (TextView) view.findViewById(R.id.student_profile_fee_description);
        feeamount = (TextView) view.findViewById(R.id.student_profile_fee_amount);


        textView = (TextView) view.findViewById(R.id.student_profile_fee_amount);


        getUsersListData();


        return view;
    }


    public void getUsersListData() {


        String URL = Navigation_URL + "?id=" + master_id + "&fromDate=" + "&toDate=";
        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);

                            //  studentFeeInformation = new StudentFeeInformation(response);
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject jsonObject = jArray.getJSONObject(i);
                                status = jsonObject.getString("Status");
                                DateofReceiptIssued = jsonObject.getString("DateOfReciept").substring(0, 10);
                                ReceiptNumber = jsonObject.getString("RecieptNo");
                                FeeReceivedDate = jsonObject.getString("recivedDate").substring(0, 10);


                                String Description = jsonObject.getString("Description");
                                JSONArray jArray1 = new JSONArray(Description);
                                //  student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate));
                                //
                                //JSONArray jArray1 = jsonObject.getJSONArray("Description");
                                StudentFeeInformation sInformation = new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate);

                                for (int j = 0; j < jArray1.length(); j++) {


                                    JSONObject jsonObjectinner = jArray1.getJSONObject(j);
                                    System.out.println("total length" + jArray1.length());
                                    Descriptionlist = jsonObjectinner.getString("des");
                                    Amount = jsonObjectinner.getString("Amount");
                                    arrayList.add(Descriptionlist);
                                    Log.d("ArrayList", String.valueOf(arrayList));
                                    //  student_list.add(new StudentFeeInformation(Descriptionlist, Amount));
                                    sInformation.addFeeDescription(Descriptionlist);
                                    sInformation.addAmount(Amount);


                                }

                                System.out.println("total lengthArray" + jArray.length());
                                student_list.add(sInformation);


                            }

                            System.out.println("student_list size:" + student_list.size());
                            CustomFeeListStudentAdapter customFeeListStudentAdapter = new CustomFeeListStudentAdapter(getActivity(), student_list);
                            listViewfees.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);


    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.dashboard, menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                // do s.th.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

CustomFeeListStudentAdapter

public class CustomFeeListStudentAdapter extends BaseAdapter {
    LinearLayout reciptViewDynamic;

    Context mContext;
    Fee fee = new Fee();

    ArrayList<StudentFeeInformation> student_list;


    LinearLayout linearLayout;

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

        System.out.println(student_list.size());
        return student_list.size();
    }

    @Override
    public Object getItem(int arg0) {
        return student_list.get(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);
            // viewHolder.student_profile_Fee_Amount = (TextView) convertView.findViewById(R.id.student_profile_fee_amount);
            // viewHolder.student_profile_Fee_Description = (TextView) convertView.findViewById(R.id.student_profile_fee_description);

            //added code
            viewHolder.receiptLinearLayout = (LinearLayout) convertView.findViewById(R.id.recipt_layout);


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


        Log.d(TAG, "@@ postion:" + postion + " getFeeDescription" + student_list.get(postion).getFeeDescription());
        Log.d(TAG, "@@ postion:" + postion + " getAmount" + student_list.get(postion).getAmount());


        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());
        //  viewHolder.student_profile_Fee_Amount.setText(student_list.get(postion).getAmount());
        // viewHolder.student_profile_Fee_Description.setText(student_list.get(postion).getFeeDescription());

        // viewHolder.receiptLinearLayout.removeAllViews();
        //added code

        //  Fee fee=new Fee();
        //   JSONArray x=fee.jArray1;

        Log.d(TAG, "@@ wrong information:" + student_list.get(postion).getFeeDescription().size());

        viewHolder.receiptLinearLayout.removeAllViews();
        for (int i = 0; i < student_list.get(postion).getFeeDescription().size(); i++) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //   reciptViewDynamic = (LinearLayout) inflater.inflate(R.layout.layout_bil_info, null);


            reciptViewDynamic = (LinearLayout) inflater.inflate(R.layout.layout_bil_info, parent, false);
            viewHolder.student_profile_Fee_Amount = (TextView) reciptViewDynamic.findViewById(R.id.student_profile_fee_amount);
            viewHolder.student_profile_Fee_Description = (TextView) reciptViewDynamic.findViewById(R.id.student_profile_fee_description);
            viewHolder.student_profile_Fee_Amount.setText(student_list.get(postion).getAmount().get(i));
            viewHolder.student_profile_Fee_Description.setText(student_list.get(postion).getFeeDescription().get(i));
            System.out.print(viewHolder);

            // Log.d(TAG, "@@ wrong information:" + student_list.get(postion).getFeeDescription());
            viewHolder.receiptLinearLayout.addView(reciptViewDynamic);


            Log.d(TAG, "@@  massive wrong information:" + student_list.get(postion).getFeeDescription().get(i));


        }

        // (reciptViewDynamic).removeView(convertView);
        convertView.setTag(viewHolder);
        return convertView;
    }

    class Holder {
        TextView student_profile_fee_status;
        TextView student_profile_ReceiptIssuedDate;
        TextView student_profile_ReceiptIssuedNumber;
        TextView student_profile_FeeReceivedDate;
        TextView student_profile_Fee_Amount;
        TextView student_profile_Fee_Description;
        LinearLayout receiptLinearLayout;
    }


}

JSON

[
  {
    "MasterID": "E0017",
    "StdID": 95,
    "Status": "U",
    "AmountPaid": 1200,
    "CLASS_ID": 2021,
    "DateOfReciept": "2017-06-01T00:00:00",
    "Description": "[{\"des\":\"Admission\",\"Amount\":1200},{\"des\":\"Due\",\"Amount\":0}]",
    "dueAmount": 0,
    "receivedAmount": 0,
    "RecieptNo": 80,
    "NAME": "Calvin Patterson",
    "CLASSNO": "1",
    "recivedDate": "2017-06-01T00:00:00",
    "dueDate": "2017-06-29T00:00:00",
    "reciever": "Hari Har Sharma"
  }
]

StudentFeeInformation

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


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


    }


    public StudentFeeInformation(String feeDescription, String amount) {

        FeeDescription = feeDescription;
        Amount = amount;

    }


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


    public String getAmount() {
        return Amount;
    }

    public void setAmount(String amount) {
        Amount = amount;
    }


}

logcat的

   06-08 12:40:21.519 28277-28277/com.example.user.mis D/TAG: MasterID:0095
06-08 12:40:21.519 28277-28277/com.example.user.mis D/TAG: Name:Calvin Patterson
06-08 12:40:21.519 28277-28277/com.example.user.mis D/TAG: Access:4
06-08 12:40:21.519 28277-28277/com.example.user.mis D/TAG: Issued:Thu, 08 Jun 2017 06:55:22 GMT
06-08 12:40:21.519 28277-28277/com.example.user.mis D/TAG: Expires:Fri, 09 Jun 2017 06:55:22 GMT
06-08 12:40:21.519 28277-28277/com.example.user.mis D/TAG: user_name_masterID1:0095
06-08 12:40:21.848 28277-28369/com.example.user.mis E/EGL_emulation: tid 28369: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
06-08 12:40:21.849 28277-28369/com.example.user.mis W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa827f920, error=EGL_BAD_MATCH
06-08 12:40:21.898 28277-28277/com.example.user.mis D/Term ID: 6014
06-08 12:40:21.898 28277-28277/com.example.user.mis D/Term ID: 6015
06-08 12:40:21.899 28277-28277/com.example.user.mis D/Term ID: 6016
06-08 12:40:21.899 28277-28277/com.example.user.mis D/Term ID: 6017
06-08 12:40:21.899 28277-28277/com.example.user.mis D/Term ID: 6018
06-08 12:40:21.899 28277-28277/com.example.user.mis D/Term ID: 6019
06-08 12:40:21.899 28277-28277/com.example.user.mis D/All Exam: 6
06-08 12:40:21.960 28277-28277/com.example.user.mis W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
06-08 12:40:24.041 28277-28277/com.example.user.mis I/System.out: total length2
06-08 12:40:24.041 28277-28277/com.example.user.mis D/ArrayList: [Admission]
06-08 12:40:24.041 28277-28277/com.example.user.mis I/System.out: total length2
06-08 12:40:24.041 28277-28277/com.example.user.mis D/ArrayList: [Admission, Due]
06-08 12:40:24.041 28277-28277/com.example.user.mis I/System.out: total lengthArray1
06-08 12:40:24.041 28277-28277/com.example.user.mis I/System.out: student_list size:1
06-08 12:40:24.041 28277-28277/com.example.user.mis I/System.out: 1
06-08 12:40:24.041 28277-28277/com.example.user.mis I/System.out: 1
06-08 12:40:24.041 28277-28277/com.example.user.mis I/System.out: response is [{"MasterID":"E0017","StdID":95,"Status":"P","AmountPaid":1200.0,"CLASS_ID":2021,"DateOfReciept":"2017-06-08T00:00:00","Description":"[{\"des\":\"Admission\",\"Amount\":1200},{\"des\":\"Due\",\"Amount\":0}]","dueAmount":1200.0,"receivedAmount":0.0,"RecieptNo":83,"NAME":"Calvin Patterson","CLASSNO":"1","recivedDate":"2017-06-08T00:00:00","dueDate":"2017-06-29T00:00:00","reciever":"Hari Har Sharma"}]
06-08 12:40:24.057 28277-28277/com.example.user.mis I/System.out: 1
06-08 12:40:24.057 28277-28277/com.example.user.mis I/System.out: 1
06-08 12:40:24.059 28277-28277/com.example.user.mis D/HomeTab_adapter: @@ wrong information:2
06-08 12:40:24.062 28277-28277/com.example.user.mis I/System.out: 1
06-08 12:40:24.062 28277-28277/com.example.user.mis D/HomeTab_adapter: @@ wrong information:2
06-08 12:40:24.423 28277-28277/com.example.user.mis I/System.out: 1
06-08 12:40:24.423 28277-28277/com.example.user.mis I/System.out: 1
06-08 12:40:24.426 28277-28277/com.example.user.mis D/HomeTab_adapter: @@ wrong information:2
06-08 12:40:24.427 28277-28277/com.example.user.mis I/System.out: 1

调试图片 enter image description here  在哪里我做错了我无法找到。这个问题可以解决吗?

1 个答案:

答案 0 :(得分:1)

根据以下内容更改StudentFeeInformation课程。

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

    // take a ArrayList object for description and amount as it can be multiple 
    public ArrayList<String> FeeDescription = new ArrayList<String>();
    public ArrayList<String> Amount = new ArrayList<String>();


    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 ArrayList<String> getFeeDescription() {
        return FeeDescription;
    }

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


    public ArrayList<String> getAmount() {
        return Amount;
    }

    public void setAmount(ArrayList<String> amount) {
        Amount = amount;
    }

    public void addFeeDescription(String description) {
        FeeDescription.add(description);
    }

    public void addAmount(String amount) {
        Amount.add(amount);
    }

}

现在,在for方法的内部getUsersListData()循环中,您正在创建StudentFeeInformation的新对象并在列表中添加两次。

student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate));

 for (int j = 0; j < jArray1.length(); j++) {

     JSONObject jsonObjectinner = jArray1.getJSONObject(j);
     System.out.println("total length" + jArray1.length());
     Descriptionlist = jsonObjectinner.getString("des");
     Amount = jsonObjectinner.getString("Amount");
     arrayList.add(Descriptionlist);
     Log.d("ArrayList", String.valueOf(arrayList));
     student_list.add(new StudentFeeInformation(Descriptionlist, Amount));
}

更改以上代码。

// create new object of StudentFeeInformation
StudentFeeInformation sInformation = new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate));

 for (int j = 0; j < jArray1.length(); j++) {

     JSONObject jsonObjectinner = jArray1.getJSONObject(j);
     System.out.println("total length" + jArray1.length());
     Descriptionlist = jsonObjectinner.getString("des");
     Amount = jsonObjectinner.getString("Amount");
     arrayList.add(Descriptionlist);
     Log.d("ArrayList", String.valueOf(arrayList));

     // Add fee Description and amount in same object instead of create new object every time
     sInformation.addFeeDescription(Descriptionlist);
     sInformation.addAmount(Amount)    
}
student_list.add(sInformation);

最后,在适配器的getView()方法中更改代码,如下所示。

    for (int i = 0; i < student_list.get(postion).getFeeDescription().size(); i++) {

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //   reciptViewDynamic = (LinearLayout) inflater.inflate(R.layout.layout_bil_info, null);


        reciptViewDynamic = (LinearLayout) inflater.inflate(R.layout.layout_bil_info, parent, false);
        viewHolder.student_profile_Fee_Amount = (TextView) reciptViewDynamic.findViewById(R.id.student_profile_fee_amount);
        viewHolder.student_profile_Fee_Description = (TextView) reciptViewDynamic.findViewById(R.id.student_profile_fee_description);
        viewHolder.student_profile_Fee_Amount.setText(student_list.get(postion).getAmount().get(i));
        viewHolder.student_profile_Fee_Description.setText(student_list.get(postion).getFeeDescription().get(i));

        viewHolder.receiptLinearLayout.addView(reciptViewDynamic);
    }