从firebase数据库中检索时未显示数据

时间:2021-12-08 16:51:34

App not retrieving any data from firebase database. App runs nicely but shows nothing inside the app, just shows blank. Here is my code

应用程序不从firebase数据库中检索任何数据。应用程序运行良好但在应用程序中没有显示任何内容,只显示空白。这是我的代码

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. The app is running nicely but it is not showing the data.

这是我下面的xml文件。该应用程序运行良好,但它没有显示数据。

<?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数据库的架构。

从firebase数据库中检索时未显示数据

2 个解决方案

#1


0  

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

#2


0  

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. I recommend starting fresh with the latest documentation and this codelab.

但是还有太多其他的东西可能会出错。例如:你使用的是旧版Firebase版本。我建议您使用最新文档和此codelab重新开始。

#1


0  

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

#2


0  

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. I recommend starting fresh with the latest documentation and this codelab.

但是还有太多其他的东西可能会出错。例如:你使用的是旧版Firebase版本。我建议您使用最新文档和此codelab重新开始。