Android嵌套ArrayList打印异常:内部数组内容重复问题求助
Hey there, let's figure out why you're seeing the last transaction array repeated for every student. The core issue here is misusing static variables in your StudentDetails class, plus a small mistake in how you're assigning the transaction list to each student.
1. Static Variables Are Shared Across All Instances
In your StudentDetails class, arrayListTransaction is declared as static:
private static ArrayList<StudentDetails> arrayListTransaction = null;
Static variables belong to the class itself, not individual objects. That means every StudentDetails instance shares the exact same arrayListTransaction reference. When you loop through students and set this list, you're overwriting the same static list every time. By the end of your loop, all students will point to the last transaction list you created.
2. Wrong Object Assignment in Home Fragment
Looking at your Home Fragment code, after building the arrayListTransaction for a student, you're setting it to studentDetails1 (which is a single transaction item) instead of the parent studentDetails (the actual student object):
// Incorrect: You're setting the transaction list to a transaction item, not the student studentDetails1.setArrayListTransaction(arrayListTransaction);
This doesn't associate the transaction list with the correct student, and combined with the static variable issue, it makes all students end up with the last list.
3. Incorrectly Fetching Transactions in StudentFees Fragment
In StudentFees, you're creating a new StudentDetails instance to get the transaction list:
StudentDetails studentDetails1 = new StudentDetails(); ArrayList<StudentDetails> arrayListTransaction = studentDetails1.getArrayTransaction();
Since arrayListTransaction is static, this new instance just returns the same shared list (the last one set). You should instead get the transaction list directly from the current student in your arrayListFees.
Fixes Step by Step
Step 1: Fix the Static Variable in StudentDetails
Change arrayListTransaction to an instance variable (remove the static keyword). We'll keep arrayListDetails static for your cross-fragment access (though I'd recommend using ViewModel for this in production, but let's stick to your current approach for now):
public class StudentDetails { private static ArrayList<StudentDetails> arrayListDetails = null; // Remove static here private 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; } }
Step 2: Correct the Assignment in Home Fragment
Update the line where you set the transaction list to assign it to the current studentDetails (the parent student object), not studentDetails1:
// Correct: Assign the transaction list to the student object studentDetails.setArrayTransaction(arrayListTransaction); arrayListHome.add(studentDetails);
Step 3: Fix Transaction Fetching in StudentFees Fragment
Get the transaction list directly from the current student in your loop, instead of creating a new StudentDetails instance:
public class StudentFees extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.your_fragment_layout, container, false); 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()); // Don't forget to add this TextView to your view hierarchy // Get the transaction list FROM THE CURRENT STUDENT ArrayList<StudentDetails> arrayListTransaction = arrayListFees.get(k).getArrayTransaction(); TextView textViewReceiptNo = null; // Second Array for (int l = 0; l < arrayListTransaction.size(); l++) { textViewReceiptNo = new TextView(getActivity()); textViewReceiptNo.setId(View.generateViewId()); Log.d("At Fees receipt no.", arrayListTransaction.get(l).getReceiptNo()); textViewReceiptNo.setText(arrayListTransaction.get(l).getReceiptNo()); // Add this TextView to your view hierarchy too } } return view; } }
Why This Works
- By making
arrayListTransactionan instance variable, eachStudentDetails(student) gets its own separate transaction list, so they don't overwrite each other. - Assigning the list to the correct student object ensures each student is linked to their own transactions.
- Fetching the list directly from the current student in StudentFees means you get the correct transactions for that specific student.
After applying these fixes, your logs should match what you see in the Home Fragment, with each student showing their own unique transaction receipts.
内容的提问来源于stack exchange,提问作者User210282

