我如何在MessageActivity的编辑文本中设置接收者的用户名?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何在MessageActivity的编辑文本中设置接收者的用户名?相关的知识,希望对你有一定的参考价值。

在我的消息活动中,我想在图像视图旁边的上方工具栏中设置要向其发送消息的人的姓名。我认为,如果我们可以获取接收方的唯一ID,就可以完成此操作,但是我不确定该如何完成。 Firestore Database

MessageActivity.java

package com.example.authenticatorapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.authenticatorapp.Model.Chat;
import com.example.authenticatorapp.Model.User;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

import de.hdodenhof.circleimageview.CircleImageView;

public class MessageActivity extends AppCompatActivity 
    CircleImageView profile_image;
    TextView username;
    EditText mMessage;
    FirebaseUser fUser;
    DocumentReference reference;
    String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                finish();
            
        );
        profile_image = findViewById(R.id.profile_image);
        profile_image.setImageResource(R.drawable.ic_default);
        username = findViewById(R.id.username);
        mMessage = findViewById(R.id.text_send);
        fUser= FirebaseAuth.getInstance().getCurrentUser();
        userId= FirebaseAuth.getInstance().getCurrentUser().getUid();
        reference= FirebaseFirestore.getInstance().collection("users").document(userId);


    

activity_message.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MessageActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#660000"
            android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">

            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/profile_image"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/username"
                android:textSize="18sp"
                android:layout_marginLeft="25dp"
                android:textColor="#FFEB3B"
                android:textStyle="bold"
                android:layout_marginStart="25dp" />

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view"
        android:background="@drawable/images"
        android:layout_below="@id/bar_layout"
        android:layout_above="@id/bottom"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:padding="5dp"
        android:id="@+id/bottom"
        android:background="#660000"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text_send"
            android:background="@android:color/transparent"
            android:hint="Type a message..."
            android:textColor="#FFEB3B"
            android:textColorHint="#F6F6F6"
            android:layout_toLeftOf="@id/btn_send"
            android:layout_centerVertical="true"/>

        <ImageButton
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_send"
            android:id="@+id/btn_send"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

</RelativeLayout>

模型类User.java

package com.example.authenticatorapp.Model;

public class User 

    private String fName;

    public String getfName() 
        return fName;
    

    public void setfName(String fName) 
        this.fName = fName;
    


适配器类UserAdapter.java

package com.example.authenticatorapp.Adapters;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.authenticatorapp.MessageActivity;
import com.example.authenticatorapp.R;
import com.example.authenticatorapp.Model.User;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;


public class UserAdapter extends FirestoreRecyclerAdapter<User, UserAdapter.ViewHolder> 
    public UserAdapter(@NonNull FirestoreRecyclerOptions<User> options) 
        super(options);
    

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull User model) 
        holder.fName.setText(model.getfName());
        holder.profile_image.setImageResource(R.mipmap.ic_launcher);
        holder.itemView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent intent = new Intent(v.getContext(), MessageActivity.class);
                v.getContext().startActivity(intent);
            
        );
    

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent, false);
        return new UserAdapter.ViewHolder(view);
    



    class ViewHolder extends RecyclerView.ViewHolder
        public TextView fName;
        public ImageView profile_image;

        public ViewHolder(View itemView)
            super(itemView);
            fName = itemView.findViewById(R.id.username);
            profile_image = itemView.findViewById(R.id.profile_image);
        
    

Fragment UsersFragment.java

package com.example.authenticatorapp.Fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.authenticatorapp.Adapters.UserAdapter;
import com.example.authenticatorapp.R;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.example.authenticatorapp.Model.User;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;

public class UsersFragment extends Fragment 
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference userRef = db.collection("users");

    private UserAdapter userAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.fragment_users, container, false);
        RecyclerView recyclerView1 = view.findViewById(R.id.recyclerview);
        recyclerView1.setHasFixedSize(true);
        recyclerView1.setLayoutManager(new LinearLayoutManager(getContext()));
        readUsers();
        recyclerView1.setAdapter(userAdapter);
        return  view;
    
    private void readUsers()

        FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
                .setQuery(userRef, User.class)
                .setLifecycleOwner(this)
                .build();
        userAdapter = new UserAdapter(options);
    

答案

userId类中添加User字段,并在您的消息意图中传递收件人的ID。因此,在适配器的onBindViewHolder()中:

@Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull User model) 
        holder.fName.setText(model.getfName());
        holder.profile_image.setImageResource(R.mipmap.ic_launcher);
        holder.itemView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent intent = new Intent(v.getContext(), MessageActivity.class);
                intent.putExtra("recipientId", model.getUserId()); // pass user id
                v.getContext().startActivity(intent);
            
        );
    

并且在您的MessageActivityonCreate()中:

String recipientId = getIntent().getStringExtra("recipientId");
recipientReference = FirebaseFirestore.getInstance().collection("users").document(recipientId);
recipientReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() 
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) 
        if (task.isSuccessful()) 
            DocumentSnapshot document = task.getResult();
            if (document.exists()) 
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                User user = document.toObject(User.class);
                username.setText(user.getfName());
             else 
                Log.d(TAG, "No such document");
            
         else 
            Log.d(TAG, "get failed with ", task.getException());
        
    
);

以上是关于我如何在MessageActivity的编辑文本中设置接收者的用户名?的主要内容,如果未能解决你的问题,请参考以下文章

如何在编辑文本中居中图标?

如何从summernote文本编辑器导出纯文本

如何在我的终端中编辑文本文件

如何在 Eclipse 中编辑 .txt 文件中的文本颜色

如何在 Rails 应用程序中编辑 .vtt 轨道的提示文本

如何在vim中编辑多列中的文本