1

Lately, I have been working on a project on a blogging app wherein I save and retrieve all the posts using Firebase and I am developing my app in Android Studio.

The problem is, whenever a new post is posted, the latest one goes to the bottom of the list of post; hence, the user has to go all the way down to the post activity to see the new post. How can I reverse it? I want the latest post to appear on the top. What code am I suppose to write?

This is my code:

public class AlertsActivity extends AppCompatActivity{

    private Toolbar toolbar;
    private RecyclerView mbloglist;
    private DatabaseReference mDatabase;

    @Override
    protected void onCreate(Bundle SavedInstanceState) {
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.alert);

        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDatabase= FirebaseDatabase.getInstance().getReference().child("Blog");
        mDatabase.keepSynced(true);
        mbloglist= (RecyclerView) findViewById(R.id.alert_list);
        mbloglist.setHasFixedSize(true);
        mbloglist.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<Blog,BlogViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
                Blog.class,
                R.layout.blog_row,
                BlogViewHolder.class,mDatabase

        ){
            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesp(model.getDesp());

            }
        };

        mbloglist.setAdapter(firebaseRecyclerAdapter);
    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder{
        View mview;

        public BlogViewHolder(View itemView) {
            super(itemView);
            mview=itemView;
        }

        public void setTitle(String title){
            TextView post_title= (TextView) mview.findViewById(R.id.blog_title);
            post_title.setText(title);

        }

        public void setDesp(String desp){
            TextView post_desp= (TextView) mview.findViewById(R.id.blog_desp);
            post_desp.setText(desp);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_alerts,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId()==R.id.action_add){
            Intent intent=new Intent(AlertsActivity.this,PostActivity.class);

            startActivity(intent);

        }
        return super.onOptionsItemSelected(item);
    }
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
digital_pro
  • 183
  • 2
  • 13

1 Answers1

0

Please add one more field ,timestamp to each post .While fetching all posts from Firebase Database use orderByChild("timestamp") to sort posts by date.

Aravindraj
  • 590
  • 5
  • 23