The title might seem familiar, but the problem isn't. I've referred many posts/articles, and finally posting my question.
I receive data from JSON similar to this, and from "Home" fragment I save data to a getter setter class "StudentDetails", and then access it from another fragment "StudentFees".
PROBLEM: The last object of the inner array gets printed the number of times the outer array runs.
Below is my "Home" Fragment
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);
}
Below is "StudentDetails" class
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;
}
Below is "StudentFees" Fragment
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());
}
}
}
Data insertion log @ Home fragment:
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
Data retrieval @ StudentFees fragment
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
Desired output is smilar to data inserted.
NOTE: I've referred this, but still used "static" ArrayList variable on purpose, as I need to access this ArrayList from other fragments.
JSON Data:
{
"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"
}]
}]
}