应用程序未将Firebase数据库中的数据检索到列表视图中

时间:2017-07-15 05:16:41

标签: android firebase firebase-realtime-database

我的应用运行没有错误但没有检索数据。它显示一个空屏幕。 在我的XML文件中有一些错误,这些都不妨碍我吗? 因此,当我运行应用程序时,它不会从数据库中检索数据。 我需要创建另一个类吗? 这是我的代码

xml image design view

    <?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="wrap_content"
        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="match_parent" />
    </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>





 my java code
    public class profilemain extends AppCompatActivity {


    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;


    private Firebase mref;

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


    private BottomNavigationView.OnNavigationItemSelectedListener 
    mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    Toast.makeText(profilemain.this,"hi 
    hello",Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.navigation_dashboard:
                    Toast.makeText(profilemain.this,"hi hello",Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.navigation_notifications:
                    Toast.makeText(profilemain.this,"hi hello",Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.navigation_signout:

                    Toast.makeText(profilemain.this,"You have successfully 
    Signed out",Toast.LENGTH_SHORT).show();
                    mAuth.signOut();
                    return true;
            }
            return false;
        }

    };

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

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

        Firebase.setAndroidContext(this);
        mref=new Firebase("https://stark-1dffd.firebaseio.com/users");
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
        mlistview.setAdapter(arrayAdapter);





       mref.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();

            }

            @Override
            public void onChildChanged(com.firebase.client.DataSnapshot 
    dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(com.firebase.client.DataSnapshot 
    dataSnapshot) {

            }

            @Override
            public void onChildMoved(com.firebase.client.DataSnapshot 
    dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });




        BottomNavigationView navigation = (BottomNavigationView) 
    findViewById(R.id.navigation);






      navigation.setOnNavigationItemSelectedListener
        (mOnNavigationItemSelectedListener
      );
                    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);
        }    

        }

0 个答案:

没有答案