Firestore如何在回收站视图中从firestore检索数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Firestore如何在回收站视图中从firestore检索数据相关的知识,希望对你有一定的参考价值。

背景

我的应用程序允许用户在特定类别中发布图像,然后允许用户点击帖子以显示消息传递活动

问题

我目前已经进行了设置,因此它是一个全局聊天(任何用户都可以加入,并且在同一文档的所有帖子之间读取和写入相同)用于测试目的但是我想拥有它所以它是两者之间的私人聊天用户。这是使用我正在迁移到Firestore的实时数据库创建的,因此我还必须更改“chatActivity”的代码

我做了什么

创建帖子后,它会将新文档添加到该帖子的messages集合中。然后将与该帖子相关联的消息文档名称存储在帖子中。

我被卡住了

在我的聊天活动中,我需要能够获取帖子的ID,以便我可以检索包含与该帖子相关的消息的文档的位置

目的

为了能够让用户发布图像并在“消息”集合中创建文档,请创建完成,然后让第二个用户来看看所述图像,然后能够打开为该文档创建的文档图像,这样两个用户就可以在彼此之间交换消息,使其在两个用户之间保密,因为他们只是从与该帖子相关联的文档中读取

app workflow这应该清除任何混乱

数据库:database

写入数据库的代码:

filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    final String downloadUrl =
                                            uri.toString();
                                    Log.d("tag", downloadUrl);

                                    FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                                    String uid = Objects.requireNonNull(current_user).getUid();

                                    final Map<String, Object> postMap = new HashMap<>();
                                    // No thumb ?????
                                    postMap.put("image_url", downloadUrl);
                                    postMap.put("desc", desc);
                                    postMap.put("user_id", current_user_id);
                                    postMap.put("message Doc", uid + postCategory);
                                    postMap.put("timestamp", FieldValue.serverTimestamp());


                                    firebaseFirestore.collection(postCategory).add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentReference> task) {

                                            if (task.isSuccessful()) {

                                                firebaseFirestore.collection("Posts").add(postMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                                    @Override
                                                    public void onComplete(@NonNull Task<DocumentReference> task) {

                                                        FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
                                                        String uid = Objects.requireNonNull(current_user).getUid();

                                                        final Map<String, String> chatMap = new HashMap<>();
                                                        postMap.put("timestamp", FieldValue.serverTimestamp());
                                                        postMap.put("name", current_user_id);
                                                        postMap.put("message", "");

                                                        firebaseFirestore.collection("Messages")
                                                                .document(uid + postCategory)
                                                                .set(chatMap)
                                                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                                                    @Override
                                                                    public void onSuccess(Void aVoid) {

                                                                    }
                                                                });

聊天代码

public class ChatActivity extends AppCompatActivity {

    public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;
    private static final int GALLERY_PICK = 1;

    private ListView mMessageListView;
    private MessageAdapter mMessageAdapter;// This is to do with the file messageadapter
    private ProgressBar mProgressBar;
    private ImageButton mPhotoPickerButton;
    private EditText mMessageEditText;
    private Button mSendButton;
    private String mUsername;
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference mMessagedatabaseReference;
    private ChildEventListener mChildEventListner;
    private ValueEventListener mValueEventListner;
    private FirebaseUser mCurrentUser;
    private FirebaseStorage mFirebaseStorage;
    private ProgressDialog mProgressDialog;
    private StorageReference mChatPhotosStorageReference;

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

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mMessagedatabaseReference = mFirebaseDatabase.getReference().child("messages");


        //new shit

        // Map<String, Object> usersChat = new HashMap<>();
        // usersChat.put("user 1 id", mCurrentUser);
        // usersChat.put("user2Id", )





        mFirebaseStorage = FirebaseStorage.getInstance();
        mChatPhotosStorageReference = mFirebaseStorage.getReference().child("chat_photos");

        // Initialize references to views
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mMessageListView = (ListView) findViewById(R.id.messageListView);
        mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
        mMessageEditText = (EditText) findViewById(R.id.messageEditText);
        mSendButton = (Button) findViewById(R.id.sendButton);

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        final String current_uid = mCurrentUser.getUid();

        // Initialize progress bar
        mProgressBar.setVisibility(ProgressBar.INVISIBLE);

        //Initialize message ListView and its adapter
        List<FriendlyMessage> friendlyMessages = new ArrayList<>();
        mMessageAdapter = new MessageAdapter(this, R.layout.item_message, friendlyMessages);
        mMessageListView.setAdapter(mMessageAdapter);

        // ImagePickerButton shows an image picker to upload a image for a message
        mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent galleryIntent = new Intent();
                galleryIntent.setType("image/*");
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(galleryIntent, "Select Image"), GALLERY_PICK);
            }
        });

        // Enable Send button when there's text to send
        mMessageEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().trim().length() > 0) {
                    mSendButton.setEnabled(true);
                } else {
                    mSendButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});

        // Send button sends a message and clears the EditText
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString());
                mMessagedatabaseReference.push().setValue(friendlyMessage);

                // Clear input box
                mMessageEditText.setText("");
            }
        });

        mChildEventListner = new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                FriendlyMessage friendlyMessage = dataSnapshot.getValue(FriendlyMessage.class);
                mMessageAdapter.add(friendlyMessage);

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };
        mMessagedatabaseReference.addChildEventListener(mChildEventListner);

        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), current_uid, null);
                mMessagedatabaseReference.push().setValue(friendlyMessage);

                // Clear input box
                mMessageEditText.setText("");
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {

            Uri selectedImageUri = data.getData();

            final StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());

            photoRef.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            String downloadUrl = uri.toString();
                            Log.d("tag", downloadUrl);
                            FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl);
                            mMessagedatabaseReference.push().setValue(friendlyMessage);
                        }
                    });
                }
            });

        }
    }
答案

我被困的地方

在我的聊天活动中,我需要能够获取帖子的ID,以便我可以检索包含与该帖子相关的消息的文档的位置

我不确定,你想获得所有身份证或只有一个身份证?

如果你想从firestore获取id收集的所有music文件,请在你的代码中添加:

public void loadAlltQueries(){
 Query loadAllQueryId = firebaseFirestore
                       .collection("music")
                       .orderBy("timestamp", Query.Direction.DESCENDING);

  loadAllQueryId.addSnapshotListener(new EventListener<QuerySnapshot>(){
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e){


     for (DocumentChange doc : documentSnapshots.getDocumentChanges()){

       if (doc.getType() == DocumentChange.Type.ADDED){

          String musicId = doc.getDocument().getId();
          FriendlyMessage friendlyMessage = doc.getDocument().toObject(FriendlyMessage.class).withId(musicId);                        

          mMessageAdapter.add(friendlyMessage);                                                                       

          mMessageAdapter.notifyDataSetChanged(); //for update adapter
        }
     }

    }
  });
}

并制作MusicId.class

public class MusicId{

    @Exclude
    public String MusicId;

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

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

}

不要忘记在FriendlyMessage.class中添加它

public class FriendlyMessage extends MusicId {

// your constructor
// your getter

}

从您的适配器类获取您的getter

 final String musicId = contentList.get(position).MusicId;

现在你得到你的身份CHur40Nr ..

如果您希望获得与回收者视图中选择的任何帖子相对应的帖子的ID。请制作适配器类,因为holder方法将获得您在此案例持有者中为holder.setMessage(message);选择的帖子

public class AdapterFriendlyMessage extends RecyclerView.Adapter<FriendlyMessage.ViewHolder> {

    public List<FriendlyMessage> contentList;
    public Context context;

    private FirebaseFirestore firebaseFirestore;
    private FirebaseAuth firebaseAuth;

    public AdapterFriendlyMessage(List<FriendlyMessage> contentList){
        this.contentList = contentList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_friendly_message, parent, false);
        context = parent.getContext(); FriendlyMessage(container.getContext(), contentList);

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

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.setRecyclable(false);

        // GET MusicId
        final String  musicId = contentList.get(position). MusicId;
        final String currentUserId = firebaseAuth.getCurrentUser().getUid();   


        String uid = contentList.get(position).getUid();
        firebaseFirestore.collection(" Music").document( musicId).collection("FriendlyMessage").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>(){
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task){

                if (task.isSuccessful()) {

                    String message = task.getResult().getString("message");
                    holder.setMessage(message); // this is what you want
                } 
            }
        });       
    }

    @Override
    public int getItemCount() {
        return contentList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private View mView;    

        private TextView txtMessage;           

        public ViewHolder(View itemView){
            super(itemView);
            mView = itemView;   

        }

            public void setMessage(Sting text) {

                txtMessage = mView.findViewById(R.id.text_view_mesage);
                txtMessage.setText(text);
            }


    }
}

不要忘记将id从firestore传递到Adapter公共类FriendlyMessageRoom extends Fragment {

private RecyclerView recyclerMessage;
private List<FriendlyMessage > contentList;
private AdapterFriendlyMessage adapterFriendlyMessage;        

private FirebaseFirestore firebaseFirestore;
private FirebaseAuth mAuth;



public FriendlyMessageRoom() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_friendly_message_room, container, false);


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

    contentList = new ArrayList<>();
    recyclerMessage = view.findViewById(R.id.recycler_message);
    adapterFriendlyMessage = new AdapterFriendlyMessage(contentList);
    recyclerMessage.setLayoutManager(new LinearLayoutManager(container.getContext()));
    recyclerMessage.setAdapter(adapterFriendlyMessage);

    return view;
}

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

    loadAlltQueries(); // please see firebase query that i write above
}

注意:我的答案可能无法准确回答您的问题,因为在问题描述中很难想象您想要什么。

以上是关于Firestore如何在回收站视图中从firestore检索数据的主要内容,如果未能解决你的问题,请参考以下文章

Android Firestore 过滤器回收视图

从 Firestore 回收视图中获取文件时遇到问题 [重复]

使用警报对话框时在回收站视图中从 Firebase 中删除项目

如何在flutter中从云Firestore中获取数据?

将我的应用程序发布到 google playstore 后,我的应用程序无法获取 firestore 数据以回收分页视图

如何在 BigQuery 中从 firestore 导出的数组中查找元素