我已经创建了一个自定义listview
和一个自定义适配器。我想从firebase
加载数据,但是没有显示数据。我在
EventInfoAdapter eventInfoAdapter = new EventInfoAdapter(this, list);
代码如下: Event.java
final DatabaseReference ref =
FirebaseDatabase.getInstance().getReference("Hospital");
list = new ArrayList<>();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
showdata(key);
}
}
private void showdata (String key) {
ref.child(key).child("event").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Event event = child.getValue(Event.class);
list.add(event);
}
EventInfoAdapter eventInfoAdapter = new EventInfoAdapter(this, list);
listView.setAdapter(eventInfoAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
这是我的适配器:
public class EventInfoAdapter extends ArrayAdapter<Event> {
private Activity context;
private List<Event> eventList;
public EventInfoAdapter(Activity context, List<Event>evenList){
super(context, R.layout.list_item,evenList);
this.context = context;
this.eventList =evenList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
View listView = inflater.inflate(R.layout.list_item, null, true);
TextView namaevent = listView.findViewById(R.id.eventname);
TextView dt = listView.findViewById(R.id.date);
TextView tm = listView.findViewById(R.id.time);
TextView ln = listView.findViewById(R.id.location);
Event event = eventList.get(position);
namaevent.setText(event.getEvent());
dt.setText(event.getDate());
tm.setText(event.getTime());
ln.setText(event.getLocation());
return listView;
}
答案 0 :(得分:0)
private EventInfoAdapter eventInfoAdapter;
final DatabaseReference ref =
FirebaseDatabase.getInstance().getReference("Hospital");
list = new ArrayList<>();
eventInfoAdapter = new EventInfoAdapter(this, list);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String key = ds.getKey();
showdata(key);
}
}
private void showdata (String key) {
ref.child(key).child("event").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Event event = child.getValue(Event.class);
list.add(event);
}
listView. notifyDataSetChanged();//updated this one
listView.setAdapter(eventInfoAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}