我通过JSON数据在线性布局的列表项中动态添加线性布局。我面临的问题是,线性布局的添加量超过了JSON的长度。如何解决这个问题?< / p>
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;
}
}
费
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);
}
}
添加的视图多于指定的JSON长度 linearlayout。我无法找到确切的问题。
答案 0 :(得分:1)
你应该在linearLayout上使用removeAllViews(),因为如果convertView没有null,它已经包含你使用addView添加的视图
viewHolder.receiptLinearLayout.removeAllViews(); // before entering the for loop
你看到这条线的时候
if (convertView == null) {
} else {
}
您告诉您的适配器回收视图(如果已经存在),所以当第一次视图不存在并且您在linearLayout上使用addView时,它将根据您的json数据长度添加视图但是当它回收视图时(convertView!= null),您添加到LinearLayout的视图也被回收,而不使用removeAllViews(),就像您再次添加相同的视图一样。 希望很清楚:)