简体   繁体   English

如何使用多个 firebase 项目填充列表视图

[英]how to populate listview with multiple firebase items

I'm working on a social app for android I'm trying to retrieve data from firebase and display ( at least 15 items ) The problem is that my code displays just the first item this is my code我正在开发适用于 android 的社交应用程序我正在尝试从 firebase 检索数据并显示(至少 15 个项目)问题是我的代码只显示第一个项目这是我的代码

FirebaseHelper Firebase 助手

public class FirebaseHelper {
 DatabaseReference db;
 ArrayList<Questions> questionEntries =new ArrayList<>();
public FirebaseHelper(DatabaseReference db)  {
    this.db=db;
    }

private void fetchData (DataSnapshot dataSnapshot){
questionEntries.clear();
    Questions questions = dataSnapshot.getValue(Questions.class);
    questionEntries.add(questions);}


public ArrayList<Questions> retreive (){
db.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        fetchData(dataSnapshot);
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        fetchData(dataSnapshot);
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
return questionEntries;
}

this is my activity where the list is displayed这是我显示列表的活动

public class Actu extends Fragment {
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.activity_actu, container, false);
    lv = (ListView) v.findViewById(R.id.actu_rv);
    db = FirebaseDatabase.getInstance().getReference("Questions");

    helper = new FirebaseHelper(db);
    adapter = new QuestionAdapter(getContext(),helper.retreive());
    lv.setAdapter(adapter);


    return v;        }
}

这是我的 firebase 数据库结构

Here is what you can do:您可以执行以下操作:

public ArrayList<Questions> retreive (){
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    Questions questions = dataSnapshot.getValue(Questions.class);
    questionEntries.add(questions);

}

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

Inside the onChildAdded() method you can retrieve the data of the children.onChildAdded()方法中,您可以检索孩子的数据。

In the database you have Question-KxOmo9FQ7QZMtq2wUIT , Remove the Question from there and just keep the id there, this is like the id primary key in sql database, so you dont need Question here..在数据库中,您有Question-KxOmo9FQ7QZMtq2wUIT ,从那里删除Question ,然后将id保留在那里,这就像 sql 数据库中的id主键,因此您在这里不需要Question ..

So remove the fetchdata() method.所以删除fetchdata()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM