在Android Studio中检索Firebase数据并在ListView中显示

时间:2018-09-04 14:27:55

标签: java android firebase firebase-realtime-database

我目前正在构建一个Android应用,并将Firebase用作其后端数据库,但是我很难检索数据并将其显示在ListView中。我尝试了一些代码,但显示列表活动后,该应用程序便崩溃了。

下面是我的数据库的屏幕快照,还有我的代码和尝试运行时遇到的错误:

Firebase数据库

enter image description here

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

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


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

        }
    });
}


}

错误日志:

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)

客户端对象类:

public class Client {
String name;
String location;
String latitude;
String longitude;

public Client(String location, String latitude, String longitude) {
    this.location = location;
    this.latitude = latitude;
    this.longitude = longitude;
}

public Map toMap() {
    Map client = new HashMap();

    client.put("Name", this.name);
    client.put("Location", this.location);
    client.put("Latitude", this.latitude);
    client.put("Longitude", this.longitude);

    return client;
}

public void setName(String name) {
    this.name = name;
}
}

2 个答案:

答案 0 :(得分:0)

您正在得到该错误,因为您缺少一个孩子。要解决此问题,请更改以下代码行:

String value = dataSnapshot.getValue(String.class);

String value = dataSnapshot.child("Name").getValue(String.class);

登录名中的输出将是所有客户端的名称。

PS 另外请注意,您在项目中还会遇到其他错误,因为Client类中的字段名称全部为小写,而数据库中的所有字段均以大写字母开头信件。名称应相同。还请看看 here

public class Client {
    private String name, location, latitude, longitude;

    public Client() {}

    public Client(String name, String location, String latitude, String longitude) {
        this.name = name;
        this.location = location;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String getName() { return name; }
    public String getLocation() { return location; }
    public String getLatitude() { return latitude; }
    public String getLongitude() { return longitude; }
}

答案 1 :(得分:0)

创建POJOArrayList并将其添加到您的列表中,检查下面的代码

    public class ClientLIstActivity extends AppCompatActivity {

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

private ArrayList<Client> list = new ArrayList>(); // 1=change here
private ArrayAdapter<String> adapter;

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

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


    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) {
           Client value = dataSnapshot.getValue(Client.class);  // 2=change here
           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) {

        }
    });
}


}