哈希图未附加到适配器

时间:2018-08-06 11:49:49

标签: android firebase firebase-realtime-database android-recyclerview

我正在尝试将var Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var result = []; result[0] = {January: 26.2, February: 25.9, March: 25.7, April: 23.4, May: 19.3, June: 15.9, July: 15.4, August: 16, September: 21.3, October: 24.8, November: 26.6, December: 26.6,Type: "Min"}; result[1] = {January: 36.3, February: 35.9, March: 36, April: 36.7, May: 34.2, June: 31.4, July: 32, August: 34, September: 37.8, October: 39.5, November: 39.8, December: 37.8,Type: "Max"}; var [min, max] = result.map(r => Months.map(m => { return r[m] })); console.log(min); console.log(max);加载到map中以使用值填充RecyclerAdapter,但是在recyclerView中,这些值已加载到地图中,但是{{1 }}不会收到有关这些更改的通知。我尝试检查valueEventlistener的大小是否正确显示了值,但是当我检查adapter内的map时它为零。

getitemcount()

adapter方法中

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

loadData类是

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

我的数据库结构如下所示,我正在调用订单-userid- cusomerId节点 enter image description here

1 个答案:

答案 0 :(得分: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");
         }
     });