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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…