尝试从博客文章列表中打开单个博客文章视图

时间:2019-03-21 07:13:18

标签: java android google-cloud-firestore

我正在尝试从使用回收站适配器创建的博客文章列表中打开单个博客文章。我在单个博客文章活动中没有使用任何适配器,我正在研究的是房屋应用程序,用户可以在该应用程序中张贴房屋,并对他人发表评论并发表评论。我想做的是选择一个帖子后查看单个博客帖子。因此,要使其正常工作,我需要获取所选博客文章的文档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;
        }

    }

When i press the view house button i should see the contents of this house post only

the contents of the house post selected should be viewed here

Database structure

0 个答案:

没有答案