Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android firebase中找到列表的大小?_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

如何在android firebase中找到列表的大小?

如何在android firebase中找到列表的大小?,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我正在开发一个应用程序,该应用程序的活动具有动态的浏览量。我遇到的问题是,我不知道如何获得Firebase中数组的大小 以下是我的代码供参考: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_confirm_order); parentLinearLayout =

我正在开发一个应用程序,该应用程序的活动具有动态的浏览量。我遇到的问题是,我不知道如何获得Firebase中数组的大小

以下是我的代码供参考:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirm_order);
    parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);

    final FirebaseAuth mAuth = FirebaseAuth.getInstance();
    final DatabaseReference mCartReference = FirebaseDatabase.getInstance().getReference().child(mAuth.getCurrentUser().getUid().toString()).child("cart");
    SharedPreferences mPrefs = getSharedPreferences("label", 0);
    mString = mPrefs.getString("tabletIdNum", "default_value_if_variable_not_found");
    appContext = getApplicationContext();

    // generate items from sortedCart into Views
//  for(int i = 0; i < mCartReference.child((mString)).child("sortedCart").size(); i++){
//        generate number of views here
//    }

    Button confirmButton = findViewById(R.id.confirmOrderButton);
    confirmButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ArrayList<String> items = new ArrayList<>();

            final int childCount = parentLinearLayout.getChildCount();
            for(int i = 0; i < childCount; i++){
                System.out.println(parentLinearLayout.getChildAt(i));
                if(parentLinearLayout.getChildAt(i).getClass() == LinearLayout.class){
                    for (int j = 0; j < ((LinearLayout)parentLinearLayout.getChildAt(i)).getChildCount(); j++) {
                        System.out.println(((LinearLayout) parentLinearLayout.getChildAt(i)).getChildAt(j));
                        if(((LinearLayout) parentLinearLayout.getChildAt(i)).getChildAt(j).getClass() == CardView.class){
                            EditText et = (EditText)((RelativeLayout)((CardView)((LinearLayout) parentLinearLayout.getChildAt(i)).getChildAt(j)).getChildAt(0)).getChildAt(1);
                            items.add(et.getText().toString());
                            System.out.println(et.getText().toString());
                        }
                    }
                }
            }

            mCartReference.child(mString).child("itemCart").setValue(items);
            mCartReference.child(mString).child("name").setValue(mString);
            mCartReference.child(mString).child("translated").setValue(0);

            Intent startIntent = new Intent(ConfirmOrderActivity.this, ConfirmOrderActivity.class);
            startActivity(startIntent);
        }
    });
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u confirm\u order);
parentLinearLayout=(LinearLayout)findViewById(R.id.parent\u linear\u layout);
final FirebaseAuth mAuth=FirebaseAuth.getInstance();
final DatabaseReference mCartReference=FirebaseDatabase.getInstance().getReference().child(mAuth.getCurrentUser().getUid().toString()).child(“购物车”);
SharedReferences mPrefs=getSharedReferences(“标签”,0);
mString=mPrefs.getString(“tabletIdNum”,“如果找不到变量,则默认值”);
appContext=getApplicationContext();
//将项目从sortedCart生成到视图中
//对于(inti=0;i
在注释掉的代码段中,我试图生成与
.child(“sortedCart”)


我找不到数据库子级的
.length
.size()
,请告诉我您是否有任何解决方案来获取数组的大小(子节点数)

使用值侦听器

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbRef= database.getReference();

dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snap: dataSnapshot.getChildren()) {
        Log.e(snap.getKey(),snap.getChildrenCount() + ""); // here it'll get the size
    }
}

@Override
public void onCancelled(DatabaseError databaseError) {

}});

使用价值监听器

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbRef= database.getReference();

dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snap: dataSnapshot.getChildren()) {
        Log.e(snap.getKey(),snap.getChildrenCount() + ""); // here it'll get the size
    }
}

@Override
public void onCancelled(DatabaseError databaseError) {

}});

不知道为什么,但是
.getChildrenCount()
不起作用,所以我只做了
公共int-sortedCartCount并在for循环中递增:
sortedCartCount++
不知道为什么,但是
.getChildrenCount()
不起作用,所以我只做了
公共int-sortedCartCount并在for循环中递增:
sortedCartCount++