Hashmap is not attached to Adapter

风格不统一 提交于 2019-12-13 11:05:32

问题


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


回答1:


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");
         }
     });


来源:https://stackoverflow.com/questions/51706961/hashmap-is-not-attached-to-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!