How can I find the size of a list in android firebase?

问题: I'm working on an app that has an activity with a dynamic amount of Views. The problem I've ran into is that I don't know how to get the size of an array in Firebase. Her...

问题:

I'm working on an app that has an activity with a dynamic amount of Views. The problem I've ran into is that I don't know how to get the size of an array in Firebase.

Here is my code for reference:

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

In the code segment that's commented out, I'm trying to generate a set amount of views that directly correlates to the size of .child("sortedCart")

I couldn't find a .length or .size() for children of the database, let me know if you have any solutions to get the size of the array (number of child nodes)


回答1:

Use the Value listener;

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

}});
  • 发表于 2019-03-06 22:09
  • 阅读 ( 227 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除