1

expandableListViewのfirebaseからデータを取得します。ヘッダーでは、テーブル番号、請求書番号、日付、請求金額のような4つの値が必要です。請求書番号と日付を使用して検索すると、アイテムを展開してアイテム情報を表示すると、解決方法 を見つけることができます。そのため、非同期動作の消耗品リストビューでfirebaseデータを取得できません

public class MainActivity extends ActionBarActivity { 
private static ExpandableListView expandableListView; 
private static ExpandableListAdapter adapter; 
private DatabaseReference mRef; 
private FirebaseDatabase mFirebaseInstance; 
BillModel bill; 




ArrayList<BillModel> billarry = new ArrayList<BillModel>(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    expandableListView = (ExpandableListView) findViewById(R.id.simple_expandable_listview); 
    mFirebaseInstance = FirebaseDatabase.getInstance(); 
    // Setting group indicator null for custom indicator 
    expandableListView.setGroupIndicator(null); 

    setItems(); 
    setListener(); 

} 
void setItems() { 

    mRef = mFirebaseInstance.getReference(); 
    DatabaseReference queryrecord = mRef.child("bill"); 
    queryrecord.addValueEventListener(new ValueEventListener() { 
     public void onDataChange(DataSnapshot snapshot) { 
      billarry.clear(); 
      for (DataSnapshot postSnapshot : snapshot.getChildren()) { 
       bill = postSnapshot.getValue(BillModel.class); 
       String bill_date = bill.getBill_date(); 
       String Bill_amount = bill.getBill_amount(); 
       String Bill_tableno = bill.getBill_tableno(); 
       String Incvoice_no = bill.getIncvoice_no(); 
       Log.d("result", "Bill---" + bill_date+","+Bill_amount+","+Bill_tableno+","+Incvoice_no); 
       billarry.add(bill); 
       // here you can access to name property like university.name 

      } 
     } 
@Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 


    }); 
// Array list for header 
    ArrayList<String> header = new ArrayList<String>(); 

    // Array list for child items 
    List<String> child1 = new ArrayList<String>(); 
    List<String> child2 = new ArrayList<String>(); 
    List<String> child3 = new ArrayList<String>(); 
    List<String> child4 = new ArrayList<String>(); 

    // Hash map for both header and child 
    HashMap<ArrayList<BillModel>, List<String>> hashMap = new HashMap<>(); 


    // Adding header and childs to hash map 
    hashMap.put(billarry, child1); 
Log.d("result", "HAsh-Bill---" + hashMap.put(billarry,child1)); 


    hashMap.put(billmodel.get(1), child2); 
    hashMap.put(billmodel.get(2), child3); 
    hashMap.put(billmodel.get(3), child4); 

    adapter = new ExpandableListAdapter(MainActivity.this, header, hashMap); 

    // Setting adpater over expandablelistview 
    expandableListView.setAdapter(adapter); 
} 
void setListener() { 

    // This listener will show toast on group click 
    expandableListView.setOnGroupClickListener(new OnGroupClickListener() { 

     @Override 
     public boolean onGroupClick(ExpandableListView listview, View view, 
            int group_pos, long id) { 

      Toast.makeText(MainActivity.this, 
        "You clicked : " + adapter.getGroup(group_pos), 
        Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    }); 
expandableListView 
      .setOnGroupExpandListener(new OnGroupExpandListener() { 

       // Default position 
       int previousGroup = -1; 

       @Override 
       public void onGroupExpand(int groupPosition) { 
        if (groupPosition != previousGroup) 

         // Collapse the expanded group 
         expandableListView.collapseGroup(previousGroup); 
        previousGroup = groupPosition; 
       } 

      }); 
// This listener will show toast on child click 
    expandableListView.setOnChildClickListener(new OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView listview, View view, 
            int groupPos, int childPos, long id) { 
      Toast.makeText(
        MainActivity.this, 
        "You clicked : " + adapter.getChild(groupPos, childPos), 
        Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    }); 
} 

}

答えて

0

、あなたはonDataChange()メソッド内billarryのArrayListの宣言を移動する必要があります。

ArrayList<BillModel> billarry = new ArrayList<BillModel>(); 

したがって、onDataChangeは常に非同期に呼び出されます。つまり、BillModelクラスのオブジェクトをリストに追加する文は、onDataChangeが呼び出される前に実行されます。だからあなたのリストはそのメソッドの外側で空です。

他のアプローチでは、このpostこのpostをご覧ください。

希望が助けます。

関連する問題