如何在列表视图中一一显示数据库中的数据?

How can I display data from database in my listview one by one?

我是开发应用程序的新手。我尝试创建预订系统。

我想检索数据并显示在列表视图上,如下所示;

name 应该在 name 的 textview 下,phone for phone 等。

public class Resden extends AppCompatActivity {
    DatabaseReference depref;
    FirebaseDatabase fdata;
    TextView namev, phonev, emailv, arrv, depv, paxv, ratev, roomtypev;
    Guest newguest;

    ListView listView;
    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resden);
        listView = (ListView) findViewById(R.id.lviewy);
        list = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
        fdata = FirebaseDatabase.getInstance();
        depref = fdata.getReference("Guests");
        listView.setAdapter(arrayAdapter);


        depref.addChildEventListener(new ChildEventListener() {
                                         @Override
                                         public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                                             for(DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
                                                 String name = (String) childSnapShot.child("Guests").getValue();
                                                 String phone = (String) childSnapShot.child("phone").getValue();
                                                 String email = (String) childSnapShot.child("email").getValue();
                                                 String arriving = (String) childSnapShot.child("arriving").getValue();
                                                 String departure = (String) childSnapShot.child("departure").getValue();
                                                 String pax = (String) childSnapShot.child("pax").getValue();
                                                 String rate = (String) childSnapShot.child("rate").getValue();
                                                 String roomtype = (String) childSnapShot.child("roomtype").getValue();

                                                 list.add(name);
                                                 list.add(phone);
                                                 list.add(email);
                                                 list.add(arriving);
                                                 list.add(departure);
                                                 list.add(pax);
                                                 list.add(rate);
                                                 list.add(roomtype);



                                             }

                                         }

这是我的 activity

我还用 getter 和 setter 创建了访客 class..

提前致谢

您需要将 arrayAdapter 移动到 onChildAdded.

public class Resden extends AppCompatActivity {
DatabaseReference depref;
FirebaseDatabase fdata;
TextView namev, phonev, emailv, arrv, depv, paxv, ratev, roomtypev;
Guest newguest;

ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resden);
    listView = (ListView) findViewById(R.id.lviewy);

    fdata = FirebaseDatabase.getInstance();
    depref = fdata.getReference("Guests");


    depref.addChildEventListener(new ChildEventListener() {
                                     @Override
                                     public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                                         for(DataSnapshot childSnapShot : dataSnapshot.getChildren()) {
                                             String name = (String) childSnapShot.child("Guests").getValue();
                                             String phone = (String) childSnapShot.child("phone").getValue();
                                             String email = (String) childSnapShot.child("email").getValue();
                                             String arriving = (String) childSnapShot.child("arriving").getValue();
                                             String departure = (String) childSnapShot.child("departure").getValue();
                                             String pax = (String) childSnapShot.child("pax").getValue();
                                             String rate = (String) childSnapShot.child("rate").getValue();
                                             String roomtype = (String) childSnapShot.child("roomtype").getValue();

                                             list.add(name);
                                             list.add(phone);
                                             list.add(email);
                                             list.add(arriving);
                                             list.add(departure);
                                             list.add(pax);
                                             list.add(rate);
                                             list.add(roomtype);



                                         }

                                            arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
                                            listView.setAdapter(arrayAdapter);

                                     }

.addChildEventListener(listener) works async 所以 always null 如果你填写侦听器 外部 的适配器。