0

I have completed my app but in this, I want to show data by date wise. How it possible?

This is my database Structure.enter image description here

This is my Recycler Adapter

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();
}

I have searched on Google many Times but I not got any solution Please Help. what I do in my recycleradpter for showing data datewise.

This is my Fragment. I have used order by date but in this post showing by date but when month changed

like today date is

25-07-2018

26-07-2018

27-07-2018

28-07-2018

and then when I created a new post of date 27-06-2018.

its show me this post after 26-07-2018.

 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();

                }
            }
        }
    });
sonu sharma
  • 79
  • 2
  • 10
  • **[This](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)** is a recommended way in which you can retrieve data from a Cloud Firestore database and display it in a `RecyclerView` using `FirestoreRecyclerAdapter`. – Alex Mamo Jul 24 '18 at 11:37
  • @AlexMamo how it possible can you suggest me any link about this? – sonu sharma Jul 24 '18 at 11:42
  • Check the link above. – Alex Mamo Jul 24 '18 at 11:45
  • Add the code where you have tried to retrieved the code from database @sonusharma – Raj Jul 24 '18 at 14:59
  • @Raj please see that I have added my code – sonu sharma Jul 25 '18 at 02:49
  • @sonusharma Really, I have answered you this [I want to save a same string in my all documents of a collection](https://stackoverflow.com/questions/51656878/i-want-to-save-a-same-string-in-my-all-documents-of-a-collection) question and you deleted it? Why that? – Alex Mamo Aug 02 '18 at 16:15
  • @AlexMamo sorry for that, I have deleted question because the question I ask is wrong.. I have some problem in asking the question. because my English is – sonu sharma Aug 05 '18 at 07:35
  • @sonusharma Your english was not wrong. Your question was not wrong. Beside that, it's a common issue when it comes to NoSql databases. I'd appreciate it fi you'll restore the question and and you'll take an action about that answer. – Alex Mamo Aug 05 '18 at 07:44
  • @AlexMamo I undelete the question and I have tried your answer your answer is right Thanks, Or Again Sorry. – sonu sharma Aug 05 '18 at 08:04
  • @sonusharma Thanks for doing the right thing and happy to hear that it worked. – Alex Mamo Aug 05 '18 at 08:05
  • @AlexMamo Ok I vote-up your answer. – sonu sharma Aug 05 '18 at 08:11

1 Answers1

3

You also have to mention the sorting order that can be either ascending or descending depending as per your requirements. So, change your query to:-

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

And you are storing the date into String that might results in sorting or other problems in future. I suggest to use Timestamp format to store date.

Raj
  • 2,997
  • 2
  • 12
  • 30