我是开发应用程序的新手。我尝试创建预订系统。
我想检索数据并显示在listview上,如下所示;
名称应在名称的文本视图下,电话应在电话等下。
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);
}
}
这是我的活动
我还用getter和setter创建了Guest类。
感谢前进
答案 0 :(得分:0)
您需要将 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 (侦听器)异步工作,因此,如果您在适配器的外部中填充适配器,则始终为空听众。