简体   繁体   English

从Firebase数据库检索时未显示数据

[英]Data not showing when retrieved from firebase database

App not retrieving any data from firebase database. 应用未从Firebase数据库检索任何数据。 App runs nicely but shows nothing inside the app, just shows blank. 应用运行良好,但在应用内部未显示任何内容,仅显示空白。 Here is my code 这是我的代码

public class profilemain extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

    private Firebase url;

    private ListView listview;
    ArrayList<String> arrayList=new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profilemain);

        listview=(ListView) findViewById(R.id.listview);

        Firebase.setAndroidContext(this);
        url=new Firebase("https://-------.firebaseio.com/users");
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
        listview.setAdapter(arrayAdapter);
        url.addChildEventListener(new com.firebase.client.ChildEventListener() {
            @Override
            public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
                String value=dataSnapshot.getValue(String.class);
                arrayList.add(value);
                arrayAdapter.notifyDataSetChanged();
            }

            // .. Other Overriden functions
        });

        mAuth=FirebaseAuth.getInstance();
        mAuthListener =new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser()==null){
                    startActivity(new Intent(profilemain.this,MainActivity.class));

                }
            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }
}

And here is my xml file below. 这是我下面的xml文件。 The app is running nicely but it is not showing the data. 该应用程序运行良好,但未显示数据。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.food.sheenishere.stark.profilemain">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ListView
            android:id="@+id/listview"
            style="@style/Widget.AppCompat.ListView"
            android:layout_width="wrap_content"
            android:layout_height="337dp" />
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>

Here's the schema of my Firebase database. 这是我的Firebase数据库的架构。

在此处输入图片说明

You need to have a class like this 你需要上这样的课

public class Users {
    List<String> users;
}

Now get the users from your Firebase database like this. 现在像这样从Firebase数据库中获取用户。

url.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
      Users userList = dataSnapshot.getValue(Users.class);

      // Add all the users fetch in the ArrayList. 
      for (String user : userList.users)
          arrayList.add(user);

      arrayAdapter.notifyDataSetChanged();
  }

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

You likely are not signed in, while the default security rules require that you're authenticated when you attach the listener . 您可能未登录,而默认的安全规则要求在附加侦听器时对您进行身份验证。 A quick fix: 快速修复:

    mAuth=FirebaseAuth.getInstance();
    mAuthListener =new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser()==null){

                startActivity(new Intent(profilemain.this,MainActivity.class));

                arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
                listview.setAdapter(arrayAdapter);
                url.addChildEventListener(new com.firebase.client.ChildEventListener() {
                    @Override
                    public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
                        String value=dataSnapshot.getValue(String.class);
                        arrayList.add(value);
                        arrayAdapter.notifyDataSetChanged();
                    }

                    // .. Other Overriden functions
                });
            }
        }
    };

But there are too many other things that could go wrong here. 但是还有太多其他事情可能在这里出错。 For one: you're on a very old Firebase version. 例如:您使用的是很旧的Firebase版本。 I recommend starting fresh with the latest documentation and this codelab . 我建议从最新的文档和本代码实验室重新开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM