通过Firestore数据迭代FirestoreRecyclerView

时间:2019-06-09 06:24:47

标签: android firebase android-recyclerview google-cloud-firestore firebaseui

我正在尝试获取Firestore数据以显示在我的应用中。

我在YouTube上观看了一个教程:https://www.youtube.com/watch?v=lAGI6jGS4vs&list=PLrnPJCHvNZuAXdWxOzsN5rgG2M4uJ8bH1&index=3

在本教程中,教师的应用程序按“优先级”降序显示了完整的文档。我创建了一个包含两个文档的集合,这些文档类似于导师的Firestore的结构。但是,我的应用程序仅显示最后一个“优先级”的文档,而其他文档没有显示。

root
 |
 |------Beatles1(collection)
           |                     |---title : "Abbey Road"
           |--------Abbey Road---|---priority : 1969
           |                     |---description : "Crossing street"
           |
           |                      |---title : "The Beatles"
           |--------The Beatles---|---priority : 1970
                                  |---description : "White album"

这是我的代码。 OrderActicvity.java

public class OrderActivity extends AppCompatActivity {
    private CollectionReference orderRef = db.collection("Beatles1");
    private NoteAdapter adapter;

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

    setUpRecyclerView();
    }

    private void setUpRecyclerView() {
        Query query = orderRef.orderBy("priority", Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
                .setQuery(query, Note.class)
                .build();

        adapter = new NoteAdapter(options);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Note.java

public class Note {
    private String title;
    private String description;
    private int priority;

    public Note()  {

    }

    public Note(String title, String description, int priority)  {
        this.title = title;
        this.description = description;
        this.priority = priority;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public int getPriority() {
        return priority;
    }
}

NoteAdapter.java

public class NoteAdapter extends FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder> {

    public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull Note model) {
        holder.textViewTitle.setText(model.getTitle());
        holder.textViewDescription.setText(model.getDescription());
        holder.textViewPriority.setText(String.valueOf(model.getPriority()));
    }

    @NonNull
    @Override
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.note_item,
                viewGroup, false);

        return new NoteHolder(v);
    }

    class NoteHolder extends RecyclerView.ViewHolder {
        TextView textViewTitle;
        TextView textViewDescription;
        TextView textViewPriority;

        public NoteHolder(@NonNull View itemView) {
            super(itemView);

            textViewTitle = itemView.findViewById(R.id.text_view_title);
            textViewDescription = itemView.findViewById(R.id.text_view_description);
            textViewPriority = itemView.findViewById(R.id.text_view_priority);
        }
    }
}

我的应用仅显示“甲壳虫乐队”,而不显示“艾比路”。我在导师的代码和我的代码中找不到任何区别。我的代码有什么问题?

0 个答案:

没有答案