首页 > 解决方案 > 尝试从博客文章列表中打开单个博客文章视图

问题描述

我正在尝试从使用回收器适配器创建的博客文章列表中打开一篇博客文章。我没有在单个博客帖子活动中使用任何适配器,我正在开发的是一个房屋应用程序,用户可以在其中发布他们的房屋,喜欢和评论他人。我要做的是在选择该帖子时查看单个博客帖子。因此,要使其正常工作,我需要获取所选博客文章的文档 ID,以便我可以从 Firestore DB 中检索它并将其显示在单个博客文章视图中。问题是,我很难检索文档并显示它,因为文档有一个自动生成的密钥。

我最初创建了一个可扩展类,它将文档自动生成的 id 传递给我为将文档发送到回收器适配器而创建的模型类,我想的是使用相同的可扩展类以便将文档 id 传递给单个博客发布视图,我该如何实现?

单篇博文java:

package com.example.android.houseap;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.houseap.Listings.HousePost;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

public class SingleHousePost extends AppCompatActivity {

    private  String mPost_key=null;
    private FirebaseFirestore firebaseFirestore;
    private TextView locationTextView;
    private DocumentReference documentRef;
    private FirebaseAuth firebaseAuth;

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

        firebaseFirestore=FirebaseFirestore.getInstance();
        firebaseAuth=FirebaseAuth.getInstance();


        String house_post_id = String.valueOf(HousePostId.class);  //method 1




        mPost_key=firebaseFirestore.collection("House_Post").document(house_post_id).getId();


        locationTextView=findViewById(R.id.location_city_town);

        String currentUser=firebaseAuth.getCurrentUser().getUid();


        DocumentReference docRef = firebaseFirestore.collection("House_Posts").document(mPost_key);
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        String locationHouse=task.getResult().getString("location");

                        locationTextView.setText(locationHouse);

                    } else {

                        Toast.makeText(SingleHousePost.this, "DATA DOES NOT EXISTS,PLEASE REGISTER", Toast.LENGTH_LONG).show();


                    }
                } else {

                    Toast.makeText(SingleHousePost.this, "CANNOT RETRIEVE DATA", Toast.LENGTH_LONG).show();


                }
            }
        });








    }


}


Extendable class:

        package com.example.android.houseap;
    import android.support.annotation.NonNull;

    import com.google.firebase.firestore.Exclude;

    public class HousePostId {

        @Exclude
        public  String HousePostId;

        public<T extends HousePostId>T withId(@NonNull final String id ){

            this.HousePostId=id;
            return (T) this;
        }

    }

当我按下查看房子按钮时,我应该只看到这个房子帖子的内容

应在此处查看所选房屋帖子的内容

数据库结构

标签: javaandroidgoogle-cloud-firestore

解决方案


推荐阅读