【问题标题】:I am not able retrieve firebase data in expendable listview我无法在消耗性列表视图中检索 Firebase 数据
【发布时间】:2017-11-07 09:42:42
【问题描述】:

我想在 expandableListView 中从 firebase 检索数据。在标题中,我想要 4 个值,如表号、发票号、日期、账单金额。帮我找出解决方案 使用发票编号和日期进行搜索,单击项目时展开项目并显示项目信息。

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

}

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-authentication expandablelistview


    【解决方案1】:

    由于异步行为,您需要将billarry ArrayList 的声明移动到onDataChange() 方法内。

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

    所以请记住,onDataChange 总是被异步调用。这意味着将BillModel 类的对象添加到列表的语句在调用onDataChange 之前执行。这就是为什么您的列表在该方法之外是空的。

    其他方法请访问postpost

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 2017-11-18
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多