如何在recyclerview上添加新帖子?

时间:2017-06-02 06:16:33

标签: android android-recyclerview

我应该在代码中添加的位置和内容?我搜索了许多答案,但无法找到我的代码中添加的确切位置。帮帮我

private void startPosting() {
        mProgress.setMessage("Posting to Blog....");

    final String title_val = mPostTitle.getText().toString().trim();
    final String desc_val = mPostdesc.getText().toString().trim();
    final String date_val=txtDate.getText().toString().trim();

    if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null) {

        mProgress.show();

        StorageReference filepath =mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());

       filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
           @Override
           public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

               @SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();

               DatabaseReference newPost = mDatabase.push();
               newPost.child("title").setValue(title_val);
               newPost.child("desc").setValue(desc_val);
               newPost.child("Date").setValue(date_val);
               newPost.child("image").setValue(downloadUrl.toString());

               mProgress.dismiss();

               startActivity(new Intent(PostActivity.this,MainActivity.class));

           }

1 个答案:

答案 0 :(得分:0)

看来,
您正在使用应用中的Firebase数据库!并希望在所有人中添加新帖子。实际上,Firebase会将新节点推到最后位置。

所以,
你可以做的是,插入帖子的时间和所有信息,并使用orderBy("child")查询数据库以按时间获得有序列表。

现在,
您只需按客户端的DESC订单重新排序此列表,或在recyclerview上使用setStackFromEnd(boolean)方法