2

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.

Athos Tokbi
  • 195
  • 1
  • 17

2 Answers2

1

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

learner
  • 3,092
  • 2
  • 21
  • 33
  • I have tried your code but the value which I have stored in the ```ArrayList> itemDataList``` is a bit different where I have used ```int``` as value as *Key*, and ```ProductList``` as the *Object* where the ```int``` value keeps increment as the ```productList``` Object keeps storing. So, the ```int``` value is different for different ```Object```. Becuase of that when I use your ```Code``` I will get Null. – Athos Tokbi Jun 26 '20 at 16:11
  • I came to know that after the value is stored and I have checked and I have found that all the values are the same. It seems like the ```int``` which I am using is static which ultimately replace all the value with the last updated value in the Map. – Athos Tokbi Jun 26 '20 at 16:13
  • I have to try a different approach to store the value in the Map. Can you suggest a different way as the error in my code is because of static value. And I can't find as link or related info with hashMap – Athos Tokbi Jun 26 '20 at 16:17
  • there was no possibility of overriding since n++ increment every iteration but each item could have more than values in map – learner Jun 26 '20 at 16:18
  • What I would recommend is do not nest the collections, iterations would require in an adapter if you do nest. I would suggest you to organize the data and process the logic as your requirement and send to the adapter as Arraylist – learner Jun 26 '20 at 16:22
  • Okay!. Is it possible to save multiple ```Object``` in ArrayList with the same `````` name, I mean can I use it multiple times, in the same Arraylist?? – Athos Tokbi Jun 26 '20 at 16:26
  • you are setting item name once `holder.recyclerViewItemNameAdapter.setText(data.getITEM_NAME());'. Why you are required multiple productList in each index? – learner Jun 26 '20 at 16:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216738/discussion-between-learner-and-athos-tokbi). – learner Jun 26 '20 at 16:34
  • bro, can you check this problem out: [Link](https://stackoverflow.com/questions/62643429/how-do-i-pass-arraylistobject-from-one-activity-to-another-activity) – Athos Tokbi Jun 29 '20 at 17:22
0

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!!

Athos Tokbi
  • 195
  • 1
  • 17