public class OrderHistoryActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private RecyclerView parentRecyclerView;
private ArrayList<MyOrder> parentModelArrayList;
private RecyclerView.Adapter ParentAdapter;
private RecyclerView.LayoutManager parentLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_history);
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference("CurrentUser").child(firebaseUser.getUid()).child("MyOrder");
parentRecyclerView = findViewById(R.id.Parent_recyclerView);
parentRecyclerView.setHasFixedSize(true);
parentLayoutManager = new LinearLayoutManager(this);
parentRecyclerView.setLayoutManager(parentLayoutManager);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
parentModelArrayList = new ArrayList<>();
parentModelArrayList.clear();
for (DataSnapshot parentSnapshot : dataSnapshot.getChildren()) {
String key = parentSnapshot.getKey();
ArrayList<MyOrder> childModelArrayList = new ArrayList<>();
databaseReference.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<MyOrder> childModelArrayList = new ArrayList<>();
for (DataSnapshot childSnapshot : parentSnapshot.getChildren()) {
MyOrder parentOrder = childSnapshot.getValue(MyOrder.class);
parentModelArrayList.add(parentOrder);
MyOrder childOrder = childSnapshot.getValue(MyOrder.class);
if (!childModelArrayList.contains(childOrder)) {
childModelArrayList.add(childOrder);
}
parentOrder.setChildModelArrayList(childModelArrayList);
}
ParentAdapter = new OrderHistoryParentRcyAdapter(parentModelArrayList, OrderHistoryActivity.this);
parentRecyclerView.setAdapter(ParentAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("OrderHistoryActivity", String.valueOf(databaseError.toException()));
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("OrderHistoryActivity", String.valueOf(databaseError.toException()));
}
});
}
when running the code.
When looking at the picture of the app running, you can see that it is fetching the same duplicate data values. I want to solve this problem.
Just in case, I also attach the adapter code. Please give me a quick answer.
OrderHistoryParentRcyAdapter.java
public class OrderHistoryParentRcyAdapter extends RecyclerView.Adapter<OrderHistoryParentRcyAdapter.MyViewHolder> {
private ArrayList<MyOrder> parentModelArrayList;
public Context cxt;
public OrderHistoryParentRcyAdapter(ArrayList<MyOrder> parentModelArrayList ,Context context) {
this.parentModelArrayList = parentModelArrayList;
this.cxt = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_orderhistory_parent, parent, false);
return new MyViewHolder(view);
}
@Override
public int getItemCount() {
if(parentModelArrayList != null){
return parentModelArrayList.size();
}
return 0;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.orderDate.setText(parentModelArrayList.get(position).getOrderDate());
ArrayList<MyOrder> childModelArrayList = parentModelArrayList.get(position).getChildModelArrayList();
OrderHistoryChildRcyAdapter childRecyclerViewAdapter = new OrderHistoryChildRcyAdapter(childModelArrayList, holder.childRecyclerView.getContext());
holder.childRecyclerView.setAdapter(childRecyclerViewAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(cxt, LinearLayoutManager.VERTICAL, false);
holder.childRecyclerView.setLayoutManager(layoutManager);
holder.childRecyclerView.setHasFixedSize(true);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView orderDate;
public RecyclerView childRecyclerView;
public MyViewHolder(View itemView) {
super(itemView);
orderDate = itemView.findViewById(R.id.orderDate);
childRecyclerView = itemView.findViewById(R.id.Child_RV);
}
}
}
I thought it might be a problem with the execution order in OrderHistoryActivity.java, so I tried changing the order, but the parent part representing the date in the app execution picture and the child representing the product picture, product name, and price did not appear at the same time. I tried, but the problem was not solved
2
Answers
enter image description here
enter image description here
2: https://phpout.com/wp-content/uploads/2023/08/icaaJ.png (parent.xml), enter image description here (child.xmk)
I’m not sure if it is causing the problem, but your code is loading the data from the database twice.
This line loads all the data under
databaseReference
already:This gives you a snapshot of all data under
databaseReference
, so you don’t have to load child nodes separately.Knowing this, the code to load data can be simplified to: