开发者社区> 问答> 正文

为什么listview getcount()等于零?

我在计算ListView中的器官数量时遇到问题。我将值推入Firebase数据库的文件夹中。当我将它们拉到单独的活动上时,会得到推入的值的列表。问题是我想知道此清单上有多少个器官。我尝试使用listView.getCoun()以及arrayList.size(),尽管在列表本身中我确实看到了值,但两者均显示为零。为什么会这样呢?

这是我的java代码

     list = (ListView) findViewById(R.id.listViewId2);
        check2 = findViewById(R.id.check3);

        arrayList = new ArrayList<String>();
        keyList = new ArrayList<String>();
        arrayAdapter = new ArrayAdapter<String>(recommendations_weight.this ,android.R.layout.simple_list_item_1,arrayList);
        list.setAdapter(arrayAdapter);

        mDatabase = FirebaseDatabase.getInstance();
        mAuth = FirebaseAuth.getInstance();


        mDatabaseReference= FirebaseDatabase.getInstance().getReference().child("Recommendations").child("Blood").child("WBC").child("High").child("Weight").child("1");


        mDatabaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                String value = dataSnapshot.getValue(String.class);
                arrayList.add(value); // include the scan values
                keyList.add(dataSnapshot.getKey()); //include the id of each tests
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });


//check if we have item
    if (list.getAdapter().getCount() != 0){
        check2.setText("number");
    }

这是我的xml-



    <ListView
        android:id="@+id/listViewId2"
        android:layout_width="350dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:visibility="visible"

        android:textAlignment="center" />




    <Button
        android:id="@+id/returnButton"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="return to test page"
        android:textAllCaps="false"
        android:background="#60000000"
        android:layout_marginTop="30dp"
        android:textColor="#FFFFFF"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal"
        />

    <TextView
        android:id="@+id/check3"
        android:layout_marginTop="19dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
        android:text="check"
        />

战车

展开
收起
Puppet 2020-01-19 09:36:41 672 0
1 条回答
写回答
取消 提交回答
  • 由于正在检查,因此得到0 list.getAdapter().getCount()。我认为您想检查是否有任何列表变量保存您传入的数据(我怀疑是adapter)。

    所以也许:list.getAdapter.getArrayList().getCount()?

    2020-01-19 11:40:24
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载