【问题标题】:retrieve data from firestore by Datewise in Recyclerview在 Recyclerview 中按 Datewise 从 firestore 检索数据
【发布时间】:2018-07-24 11:34:00
【问题描述】:

我已经完成了我的应用程序,但在此,我想按日期显示数据。怎么可能?

这是我的数据库结构。

这是我的回收站适配器

public class BscRecyclerAdapter extends 
RecyclerView.Adapter<BscRecyclerAdapter.ViewHolder> {
private List<MainPage> mMainPage;
private Context context;


public BscRecyclerAdapter(Context context,List<MainPage> mMainPage){
    this.context = context;
    this.mMainPage =mMainPage;
}
@Override
public BscRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
    return new ViewHolder(view);   }

@Override
public void onBindViewHolder(BscRecyclerAdapter.ViewHolder holder, final int position) {
    holder.mTitle.setText(mMainPage.get(position).getTitle());
    holder.mDescription.setText(mMainPage.get(position).getDescription());
    holder.mDate.setText(mMainPage.get(position).getDate());
    final String Title = mMainPage.get(position).getTitle();
    final String Description = mMainPage.get(position).getDescription();
    final String Image = mMainPage.get(position).getImage();
    final String Date =  mMainPage.get(position).getDate();
    final String main_id =  mMainPage.get(position).mainId;

    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent UpdateIntent = new Intent(context,BscUpdateDelete.class);

            UpdateIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            UpdateIntent.putExtra("main_id",main_id);
            UpdateIntent.putExtra("title",Title);
            UpdateIntent.putExtra("description",Description);
            UpdateIntent.putExtra("date",Date);
            UpdateIntent.putExtra("image",Image);

            context.startActivity(UpdateIntent);

        }
    });
    holder.mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = mMainPage.get(position).getImage();
            Intent intent  = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
        }
    });

    ImageView post_image = holder.mImage;
    Glide.with(context).load(mMainPage.get(position).getImage()).into(post_image);

}

@Override
public int getItemCount() {
    return mMainPage.size();
}

我在 Google 上搜索了很多次,但没有得到任何解决方案 请帮忙。 我在我的 recycleradpter 中为按日期显示数据所做的工作。

这是我的片段。 我使用了按日期排序,但在这篇文章中按日期显示,但月份更改时

喜欢今天的日期是

25-07-2018

26-07-2018

27-07-2018

28-07-2018

然后当我创建日期为 27-06-2018 的新帖子时。

它在 2018 年 7 月 26 日之后向我显示此帖子。

 mFirestore.collection("Bsc").orderBy("date").addSnapshotListener(new 
EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            for (DocumentChange doc: documentSnapshots.getDocumentChanges()){
                if (doc.getType() == DocumentChange.Type.ADDED){
                    String doc_id = doc.getDocument().getId();
                    MainPage mainPage = doc.getDocument().toObject(MainPage.class).witId(doc_id);
                    mMainPage.add(mainPage);
                    mainPageRecyclerAdapter.notifyDataSetChanged();

                }
            }
        }
    });

【问题讨论】:

  • This 是一种推荐的方式,您可以通过这种方式从 Cloud Firestore 数据库中检索数据并使用 FirestoreRecyclerAdapter 将其显示在 RecyclerView 中。
  • @AlexMamo 你怎么可能给我任何关于这个的链接?
  • 查看上面的链接。
  • 在您尝试从数据库@sonusharma 检索代码的位置添加代码
  • @Raj 请看我已经添加了我的代码

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


【解决方案1】:

您还必须根据您的要求提及可以升序或降序的排序顺序。因此,将您的查询更改为:-

 mFirestore.collection("Bsc").orderBy("date", Direction.DESCENDING).addSnapshotListener()...

并且您将日期存储到 String 中,这可能会导致将来出现排序或其他问题。我建议使用 Timestamp 格式来存储日期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 2021-09-06
    • 2020-03-13
    • 2020-08-23
    • 2020-04-04
    • 2021-02-22
    相关资源
    最近更新 更多