简体   繁体   English

将Firebase数据检索到Android Studio中的ListView

[英]Retrieve Firebase data to ListView in Android Studio

I am currently building an Android app and using Firebase as its backend database, however i'm having difficulties to retrieve the data and display them in a ListView . 我目前正在构建一个Android应用,并将Firebase用作其后端数据库,但是我很难检索数据并将其显示在ListView I tried few codes but the app is crashing as soon as i display the list activity. 我尝试了一些代码,但显示列表活动后,应用程序立即崩溃。

Below is a screenshot of my database and also my code and the error I get when I try to run: 下面是我的数据库的屏幕快照,还有我的代码和尝试运行时遇到的错误:

Firebase Databse Picture Firebase数据库图片

在此处输入图片说明

public class ClientLIstActivity extends AppCompatActivity {

private Button clientSelect;
private ListView clientlistView;
private FirebaseDatabase database;
private DatabaseReference databaseReference;

private ArrayList<String> list = new ArrayList<String>();
private ArrayAdapter<String> adapter;

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


    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
    clientlistView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    databaseReference = FirebaseDatabase.getInstance().getReference().child("Clients");

    clientlistView = (ListView) findViewById(R.id.clientListView);
    clientSelect = (Button) findViewById(R.id.selectClient);

    clientSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });

    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
           String value = dataSnapshot.getValue(String.class);
           list.add(value);
        }

        @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) {

        }
    });
}


}

Error Log: 错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.javytharanee.quicksolattendance, PID: 6602
                  com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
                      at com.google.android.gms.internal.zzepg.zzb(Unknown Source:93)
                      at com.google.android.gms.internal.zzepg.zza(Unknown Source:0)
                      at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
                      at com.javytharanee.quicksolattendance.ClientLIstActivity$2.onChildAdded(ClientLIstActivity.java:53)
                      at com.google.android.gms.internal.zzegg.zza(Unknown Source:71)
                      at com.google.android.gms.internal.zzelk.zzcal(Unknown Source:2)
                      at com.google.android.gms.internal.zzelq.run(Unknown Source:71)
                      at android.os.Handler.handleCallback(Handler.java:873)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:6669)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

To solve this, just move the following line of code: 要解决此问题,只需移动以下代码行:

clientlistView = (ListView) findViewById(R.id.clientListView);

right before this line: 就在这行之前:

clientlistView.setAdapter(adapter);

You first need to instantiate the ListView object before actually using it. 在实际使用ListView对象之前,您首先需要实例化它。

You missed create the instance of your listview clientListView = findViewById(R.id.your_listView_id); 您错过了创建列表视图的实例的步骤clientListView = findViewById(R.id.your_listView_id);

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

    clientListView = findViewById(R.id.your_listView_id);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
    clientlistView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    databaseReference = FirebaseDatabase.getInstance().getReference().child("Clients");
....

Firstly you need to initialize the clientlistView and the you have to set the listview to the adapter, 首先,您需要初始化clientlistView,并且必须将listview设置为适配器,

In your programm you are setting the adapter view without initializing the clientlistview 在您的程序中,您正在设置适配器视图而不初始化clientlistview

you need to write your code like this 您需要这样编写代码

clientListView = findViewById(R.id.your_listView_id);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
clientlistView.setAdapter(adapter);

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

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