从Firebase填充ListView,具有代码但未接收任何数据

时间:2019-02-20 15:31:47

标签: android firebase firebase-realtime-database

我有我先前项目的代码,可以访问Firebase并从这些值填充列表视图。麻烦的是我不再有这个项目,所以无法使用它作为参考。

我遇到的问题是我没有收到数据库返回的任何对象,因此无法在nothin'上设置onClickListener。

这是新闻提要类中的代码:

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

    DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://apintofplain.firebaseio.com/apintofplain/Drinks");
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference allBooksRef = rootRef.child("Drinks");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<Drink> bookModelList = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                Drink book = ds.getValue(Drink.class);
                bookModelList.add(book);
            }
            ListView list = (ListView) findViewById(R.id.listView);
            CustomListView adapter = new CustomListView(Feed.this, bookModelList);
            list.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    allBooksRef.addListenerForSingleValueEvent(valueEventListener);



    list.setClickable(true);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String make = pub[position];
            Toast.makeText(Feed.this, make, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Feed.this, MainActivity.class);
            startActivity(intent);
        }
    });
}

这是我的CustomListView类中的代码:

 public CustomListView(Feed feed, List<Drink> drinkList) {
    super(feed, R.layout.listview_layout, drinkList);

}

我做错了什么吗?

enter image description here

String pub;
String location;
String price;
String rating;
String comment;
String signature;
String imageURL;

public Drink(){

}

public Drink(String pub, String location, String price, String rating, String comment, String signature, String imageURL){
    this.pub = pub;
    this.location = location;
    this.price = price;
    this.rating = rating;
    this.comment = comment;
    this.signature = signature;
    this.imageURL = imageURL;
}

public String getPub() {
    return pub;
}

public String getLocation() {
    return location;
}

public String getPrice() {
    return price;
}

public String getRating() {
    return rating;
}

public String getComment() {
    return comment;
}

public String getSignature() {
    return signature;
}

public String getImageURL(){
    return imageURL;
}

}

enter image description here

0 个答案:

没有答案