Ghimire Ghimire -4 years ago 46
JSON Question

Why the Dynamic Linear Layout is Adding More View than required JSON length?

I am adding Linear Layout Dynamically within the listitem of the linear layout through the JSON Data.The Problem i am facing is that ,the linear layout are added more than the length of the JSON .How can this issue be solved?

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


}


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


}


Fee

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<>();
System.out.println(student_list.size());

JSONArray jArray = new JSONArray(response);

// studentFeeInformation = new StudentFeeInformation(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
System.out.println(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);
System.out.println(student_list.size());
listViewfees.setAdapter(customFeeListStudentAdapter);

System.out.println("response is " + response);


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



The view are added more than specified by the JSON length in the
linearlayout.I am not able to find exact issue.

Answer Source

You should use removeAllViews() on your linearLayout because in case of convertView is no null it already contains views that you've added using addView

viewHolder.receiptLinearLayout.removeAllViews(); // before entering the for loop

You see when you right this line

if (convertView == null) {
} else {
}

You are telling your adapter to recycle the view if already exists so when the first time the view does'nt exists and you use addView on your linearLayout it will add the view according to your json data lenght but when it recycle the view(convertView != null), the views that you added to your LinearLayout are also recycled and without using removeAllViews() on it, it just like your adding the same views again. Hope it's clear :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download