标题可能看起来很熟悉,但问题并非如此。我已经推荐了很多帖子/文章,最后发表了我的问题。
我从JSON similar to this和" Home"片段我将数据保存到getter setter类" StudentDetails",然后从另一个片段访问它" StudentFees"。
问题:内部数组的最后一个对象被打印出外部数组运行的次数。
以下是我的" Home"片段
public class Home extends Fragment {
private ArrayList<StudentDetails> arrayListHome;
private ArrayList<StudentDetails> arrayListTransaction;
private StudentDetails studentDetails;
private StudentDetails studentDetails1;
JSONArray jsonArrayHome = jsonObjectHome.getJSONArray("responseData");
arrayListHome = new ArrayList<StudentDetails>();
// First array
for (int a = 0; a < jsonArrayHome.length(); a++) {
JSONObject jsonObjectStudent = jsonArrayHome.getJSONObject(a);
studentDetails = new StudentDetails();
// Set student ID
if (!jsonObjectStudent.isNull("student_id")) {
studentDetails.setStudentId(jsonObjectStudent.getString("student_id"));
}
JSONArray jsonArrayTransaction = jsonObjectStudent.getJSONArray("transaction");
arrayListTransaction = new ArrayList<StudentDetails>();
// Second array
for (int j = 0; j < jsonArrayTransaction.length(); j++) {
JSONObject jsonObjectTransaction = jsonArrayTransaction.getJSONObject(j);
studentDetails1 = new StudentDetails();
// Set receipt no.
if (!jsonObjectTransaction.isNull("receipt_no")) {
studentDetails1.setReceiptNo(jsonObjectTransaction.getString("receipt_no"));
}
arrayListTransaction.add(studentDetails1);
Log.d("At Home receipt no.", arrayListTransaction.get(j).getReceiptNo()+"");
}
studentDetails1.setArrayListTransaction(arrayListTransaction);
arrayListHome.add(studentDetails);
Log.d("At Home student id", arrayListHome.get(a).getStudentId());
}
studentDetails.setArray(arrayListHome);
}
以下是&#34; StudentDetails&#34;类
public class StudentDetails {
private static ArrayList<StudentDetails> arrayListDetails = null;
private static ArrayList<StudentDetails> arrayListTransaction = null;
private String studentId = null;
private String receiptNo = null;
public void setArray(ArrayList<StudentDetails> arrayList) {
arrayListDetails = arrayList;
}
public ArrayList<StudentDetails> getArray() {
return arrayListDetails;
}
public void setStudentId(String sId) {
studentId = sId;
}
public String getStudentId() {
return studentId;
}
public void setArrayTransaction(ArrayList<StudentDetails> transaction){
arrayListTransaction = transaction;
}
public ArrayList<StudentDetails> getArrayTransaction (){
return arrayListTransaction;
}
public void setReceiptNo(String sReceiptNo) {
receiptNo = sReceiptNo;
}
public String getReceiptNo() {
return receiptNo;
}
以下是&#34; StudentFees&#34;片段
public class StudentFees extends Fragment {
StudentDetails studentDetails = new StudentDetails();
final ArrayList<StudentDetails> arrayListFees = studentDetails.getArray();
// First Array
for (int k = 0; k < arrayListFees.size(); k++) {
final TextView textViewId = new TextView(getActivity());
textViewId.setId(View.generateViewId());
Log.d("At Fees student id", arrayListFees.get(k).getStudentId());
textViewId.setText(arrayListFees.get(k).getStudentId());
StudentDetails studentDetails1 = new StudentDetails();
ArrayList<StudentDetails> arrayListTransaction = studentDetails1.getArrayTransaction();
TextView textViewReceiptNo = null;
// Second Array
for (int l = 0; l < arrayListTransaction.size() /*studentDetails.getArrayTransaction().size()*/; l++) {
// StudentDetails studentDetails1 = new StudentDetails();
// ArrayList<StudentDetails> arrayListTransaction = studentDetails1.getArrayTransaction();
textViewReceiptNo = new TextView(getActivity());
textViewReceiptNo.setId(View.generateViewId());
Log.d("At Fees receipt no.", arrayListTransaction.get(l).getReceiptNo());
textViewReceiptNo.setText(arrayListTransaction.get(l).getReceiptNo());
}
}
}
数据插入日志@ Home片段:
D/At Home receipt no.: R/100/2017/04/2449
D/At Home receipt no.: R/100/2017/04/2450
D/At Home student id: 201510353
D/At Home receipt no.: R/100/2017/04/2536
D/At Home receipt no.: R/100/2017/04/2537
D/At Home receipt no.: R/100/2017/11/3024
D/At Home student id: 201610113
数据检索@ StudentFees片段
D/At Fees student id: 201510353
D/At Fees receipt no.: R/100/2017/04/2536
D/At Fees receipt no.: R/100/2017/04/2537
D/At Fees receipt no.: R/100/2017/11/3024
D/At Fees student id: 201610113
D/At Fees receipt no.: R/100/2017/04/2536
D/At Fees receipt no.: R/100/2017/04/2537
D/At Fees receipt no.: R/100/2017/11/3024
所需的输出对于插入的数据是微笑的。
注意:我已referred this,但仍使用&#34;静态&#34;有意的是ArrayList变量,因为我需要从其他片段访问这个ArrayList。
JSON数据:
{
"responseCode": "200",
"status": "true",
"responseData": [{
"student_id": "201510353",
"transaction": [{
"receipt_no": "R\/100\/2017\/04\/2449"
}, {
"receipt_no": "R\/100\/2017\/04\/2450"
}]
}, {
"student_id": "201610113",
"transaction": [{
"receipt_no": "R\/100\/2017\/04\/2536"
}, {
"receipt_no": "R\/100\/2017\/04\/2537"
}, {
"receipt_no": "R\/100\/2017\/11\/3024"
}]
}]
}