-2

I am trying to load a map into RecyclerAdapter to use the values to populate recyclerView but in valueEventlistener the values are getting loaded into the map but adapter is not getting notified of these changes. I tried checking the size of map it is showing the values correctly but when i checked the getitemcount() inside adapter it is zero.

private Map<String, OrderItem>    convoModels = new HashMap<>();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_conversation);
    inearLayoutManager mLayoutManager2
            = new LinearLayoutManager(OrderConversation.this, 
    LinearLayoutManager.VERTICAL, false);
    mCartList = (RecyclerView) findViewById(R.id.conversationlist);
    mCartList.setLayoutManager(mLayoutManager2); 
    mAdapterConvo  = new ConvoFirstAdapter(convoModels);
    mCartList.setAdapter(mAdapterConvo);
    loaddData();
  }

in loadData method

mDatabase.addValueEventListener(new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                 for(DataSnapshot ds: dataSnapshot.getChildren()){
                     GenericTypeIndicator<Map<String, OrderItem>> to = 
                     new GenericTypeIndicator<Map<String, OrderItem>>() 
                      {};
                     Map<String, OrderItem> map = ds.getValue(to);
                     convoModels =  ds.getValue(to);
                     mAdapterConvo.notifyDataSetChanged();
                     int i =   mAdapterConvo.getItemCount();
                     Toast.makeText(OrderConversation.this, "size of map is "+ 
                     convoModels.size() + "size of adap "+ i, 
                     Toast.LENGTH_SHORT).show();

                     for(OrderItem ml:convoModels.values()) {
                         String name = ml.getName();
                         Toast.makeText(OrderConversation.this, "name is "+ 
                         name, Toast.LENGTH_SHORT).show();
                     }
                   }
              }
             @Override
             public void onCancelled(DatabaseError databaseError) {
             }
         });

Adapter Class is

public class ConvoFirstAdapter extends 
RecyclerView.Adapter<OrderConversation.ConvoViewHolder> {
    private Map<String, OrderItem> userModels;
    private Context context;

public ConvoFirstAdapter() {
    this.userModels = new HashMap<>();
}
public ConvoFirstAdapter(Map<String, OrderItem> mMessageList) {
    this.userModels = mMessageList;
}

@Override
public OrderConversation.ConvoViewHolder onCreateViewHolder(ViewGroup 
parent, int viewType) {
    context = parent.getContext();
    return new 
    OrderConversation.ConvoViewHolder
             (LayoutInflater.from(parent.getContext())
            .inflate(R.layout.convo_layout, parent, false));

}

@Override
public void onBindViewHolder(OrderConversation.ConvoViewHolder holder, 
int position) {
    CustomLinearLayoutManager c = new 
    CustomLinearLayoutManager(context);
    holder.init();
    holder.setLayoutManager(c);
    holder.setConvoAdapter(userModels,context);
}

@Override
public int getItemCount() {

    return userModels.size();
}

}

the structure of my Database is like this below i am calling orders - userid- cusomerId node enter image description here

Jimmy
  • 87
  • 2
  • 9

1 Answers1

0
private Map<String, OrderItem>    convoModels = new HashMap<>();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_conversation);
    inearLayoutManager mLayoutManager2
            = new LinearLayoutManager(OrderConversation.this, 
    LinearLayoutManager.VERTICAL, false);
    mCartList = (RecyclerView) findViewById(R.id.conversationlist);
    mCartList.setLayoutManager(mLayoutManager2); 
Log.e(getClass().getSimpleName(),"Initi.... Adapter");
    mAdapterConvo  = new ConvoFirstAdapter(convoModels);
    mCartList.setAdapter(mAdapterConvo);
    loaddData();
  }

LoaddData();

mDatabase.addValueEventListener(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot) {
             for(DataSnapshot ds: dataSnapshot.getChildren()){
                 GenericTypeIndicator<Map<String, OrderItem>> to = 
                 new GenericTypeIndicator<Map<String, OrderItem>>() 
                  {};
                 Map<String, OrderItem> map = ds.getValue(to);
                 convoModels =  ds.getValue(to);
                 mAdapterConvo.notifyDataSetChanged();
                 int i =   mAdapterConvo.getItemCount();
                 /* Toast.makeText(OrderConversation.this, "size of map is "+ 
                 convoModels.size() + "size of adap "+ i, 
                 Toast.LENGTH_SHORT).show(); */
Log.e("AddValueEent","size of map is: "+convoModels.size()+" size of Adap : "+i);

                 for(OrderItem ml:convoModels.values()) {
                     String name = ml.getName();
                     /*Toast.makeText(OrderConversation.this, "name is "+ 
                     name, Toast.LENGTH_SHORT).show();*/
Log.e("inLoop","Name is : "+name);
                 }
               }
          }
         @Override
         public void onCancelled(DatabaseError databaseError) {
Log.e("inCancelled","Yes");
         }
     });
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
  • still the result is same – Jimmy Aug 06 '18 at 12:51
  • before init adapter check hasmap has value or not – Ashvin solanki Aug 06 '18 at 13:03
  • see https://stackoverflow.com/questions/18389135/how-to-verify-if-a-value-in-hashmap-exist – Ashvin solanki Aug 06 '18 at 13:04
  • is there any other way to use Hashmap inside the Adapter, i think its not getting attached to the adapter thats why it is returning null. – Jimmy Aug 07 '18 at 13:09
  • mAdapterConvo = new ConvoFirstAdapter(convoModels,getApplicationContext()); – Ashvin solanki Aug 08 '18 at 04:54
  • still the result is same, i think the hashmap is not getting attached to adapter or it is null somehow – Jimmy Aug 08 '18 at 05:07
  • oK let me know what is in Has map explain me with example – Ashvin solanki Aug 08 '18 at 05:09
  • which type of value you want to use in hasmap – Ashvin solanki Aug 08 '18 at 05:09
  • i am trying to get a list of objects from firebase which is of OrderItem class , so when i retrieve data from firebase it is giving me a Map which i am storing inside the convomodels and passing it to adapter where i want to use those items to generate a list kind of thing inside recycler view (each item in my recyclerview will be having a list) – Jimmy Aug 08 '18 at 05:14
  • but i am stuck at this i am unable to pass my map to adapter , second part i havent touch yet. i am getting values fine inside my valueeventlistener i have used a toast to check whether there are values in my map or not but after attaching calling notifyDatasetChanged() it should be updated inside the adapter also so i called the getItemcount() on the adapter and it is returning zero even though there are values inside the Map already , so either the map is not attached properly or there are some other issues with using map inside adapter – Jimmy Aug 08 '18 at 05:17
  • i have added my database structure above – Jimmy Aug 08 '18 at 05:29
  • create log inside the addValueEventListner replace toast use Log.e(); – Ashvin solanki Aug 08 '18 at 05:40
  • create log before initialize adapter run program and see the Logs – Ashvin solanki Aug 08 '18 at 05:41
  • if have no idea connect anydesk with me – Ashvin solanki Aug 08 '18 at 05:43
  • i dont get it can you please post some code as on how and where should i call the log – Jimmy Aug 08 '18 at 05:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177610/discussion-between-ashvin-solanki-and-jimmy). – Ashvin solanki Aug 08 '18 at 05:55