将数据从Firebase数据库检索到ListView

时间:2017-07-15 15:04:38

标签: android firebase firebase-realtime-database

我想将Firebase数据库中的数据检索到ListView中,但应用程序在打开空白活动后几秒钟就会崩溃。当我打开要检索数据的活动时,它会保持空白3到4秒,然后崩溃。

主要java:

public class home extends AppCompatActivity {


    DatabaseReference mref = FirebaseDatabase.getInstance().getReference();
    ListView mlistview;
    ArrayList < String > arrayList = new ArrayList < > ();
    ArrayAdapter < String > arrayAdapter;

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

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

        DatabaseReference ref = FirebaseDatabase.getInstance()
            .getReferenceFromUrl("https://stark-1dffd.firebaseio.com/users");
        //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 ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String value = dataSnapshot.getValue(String.class);

                arrayList.add(value);
                arrayAdapter.notifyDataSetChanged();
            }

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

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
}

logcat的:

Process: com.food.sheenishere.stark, PID: 12421

com.google.firebase.database.DatabaseException: Failed to convert value of 
type java.util.HashMap to String

at com.google.android.gms.internal.zg.zzb(Unknown Source)

at com.google.android.gms.internal.zg.zza(Unknown Source)

at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)

at com.food.sheenishere.stark.home$1.onChildAdded(home.java:49)

at com.google.android.gms.internal.px.zza(Unknown Source)

at com.google.android.gms.internal.vj.zzHX(Unknown Source)

at com.google.android.gms.internal.vp.run(Unknown Source)

at android.os.Handler.handleCallback(Handler.java:751)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6119)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller
.run(ZygoteInit.java:886)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

0 个答案:

没有答案