Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

android - Hashmap is not attached to Adapter

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...