How can I inflate ArrayList<HashMap<Integer, Object>> value to RecyclerView

Viewed 89

My query is that my Values are stored in HashMap<Integer, Object> which are stored in ArrayList. I am able to inflate it however it only gets inflate the last value added.

There are 7 items based on user location and it can differ from location to location. The seven items are (A,B,C,D,E,F,G)

I am trying to test whether it prints all the seven-item or not. But it only prints the "G" since its added in the HashMap at the end. However, all the values are stored in the Arraylist

Here is the Code I for onBindViewHolder & getItemCount

 @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

ProductList data;
int n=0;
for (HashMap<Integer, Object> mapEntry: itemDataList){
   //mapEntery shows size: 3 and 
   //itemDataList shows size: 7 IDK Why there is a mismatch also.
   //Size of itemDataList is accurate which is why its prints 7 times

  data = (ProductList) mapEntry.get(n)
     System.out.println(data);

        holder.recyclerViewItemNameAdapter.setText(data.getITEM_NAME());
               n++;
  }
} 
  @Override
   public int getItemCount() {
    return itemDataList.size();
}

MyMainActivity:

                ValueEventListener eventListener= new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){

                        int n=0;
                        for (DataSnapshot ds: dataSnapshot.getChildren()){

                            for (int i=0; i<LocateUID.size();i++){

                                String uid= LpcateUID.get(i);
                                String dsUID= ds.getKey();

                                final HashMap<Integer, Object> id = new HashMap<>();

                                if(dsUID.equals(uid)){

                                    for (DataSnapshot d: ds.getChildren()){

                                        productList.setITEM_NAME(d.child("item_NAME").getValue(String.class));
                                        id.put(n,productList);

                                        arrayListProduct.add(id);
                                        n++;

                                    }

                                }

                            }

                        }


                    }

                    RecyclerItemList();

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            };

It only prints the last value stored for the length of an ArrayList.

Can anyone help me out how can I resolve this issue.

2 Answers

You are iterating the entire list every time you render the item. I would not recommend this as it is a time-consuming process. Your approach always results with the last item. Try out below one.

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {   
            ProductList data = itemDataList.get(position);   
            System.out.println(data);
            holder.recyclerViewItemNameAdapter.setText(data.getITEM_NAME());
        }

I would say you can optimize more on onDataChange

Thank you @learner, you help me a lot in resolving this issue. I was finally able to solve it by using Array Of Object, however, there was also another issue with my code is that the variable was static which eventually replaces all the variables with the last update in the ArrayList and I didn't even think about it. It was here:

              for (DataSnapshot d: ds.getChildren()){
              productList.setITEM_NAME(d.child("item_NAME").getValue(String.class));
                                        id.put(n,productList);
                                        // Here this variable "n" was static 
                                        //because of that all the values were replace
    
                                        arrayListProduct.add(id);
                                        n++;

I have made some changes as I have used the object of Array with ArrayList. By which I was finally able to resolve the problem. Happy Codding!!

Related