将联系人的姓名和号码设置为工具栏中的标题和副标题

Setting contact's name and number to title and subtitle into a toolbar

我构建了一个从 phone(在本例中是我的 phone)读取联系人的应用程序。对于显示联系人的 activity,我使用了 TabLayout 和 ViewPager。为了显示联系人,我使用了 RecyclerView。关键是我处理如何读取联系人,然后我的下一个动作是在单击联系人后打开外部 activity (MessageToContact_Activity)。在这个 activity (MessageToContact_Activity) 中,我想向我选择的联系人发送消息,为此我首先尝试设置联系人的工具栏标题和副标题的名称和编号。我决定使用工具栏而不是默认的 ActionBar,因为它更灵活。问题是我不知道如何获取 name 和 number 的值并将它们用于 MessageToContact_Activity.

下面是 Contacts_Fragment 的实现:

        public class Contacts_Fragment extends Fragment {
            View v;

            public Contacts_Fragment() {
            }

            @Nullable
            @Override
            public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                v = inflater.inflate(R.layout.contacts_fragment, container, false);

                RecyclerView recyclerView = v.findViewById(R.id.recycleView_search);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                RecyclerView.LayoutManager layoutManager = linearLayoutManager;
                recyclerView.setLayoutManager(layoutManager);
                ContactsAdapter adapter = new ContactsAdapter(getContext(), getContacts());
                recyclerView.setAdapter(adapter);

                return v;
            }

            private List<ContactModel> getContacts() {

                List<ContactModel> list = new ArrayList<>();

                if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_CONTACTS}, 1);
                }

                Cursor cursor = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");

                cursor.moveToFirst();

                while (cursor.moveToNext()) {
                    list.add(new ContactModel(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                    )), cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
                }

                return list;
            }
        }

下面是 ContactsAdapter 的实现:

    public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {

        private Context mContext;
        private LayoutInflater inflater;
        private List<ContactModel> mListContacts;

        public ContactsAdapter(Context context, List<ContactModel> listContacts){
            this.mContext = context;
            this.mListContacts = listContacts;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            inflater = LayoutInflater.from(mContext);
            View view = inflater.inflate(R.layout.item_contact,parent, false);
            final ViewHolder viewHolder = new ViewHolder(view);

            viewHolder.item_contact.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View view) {
                    Toast.makeText(mContext, "Test Click "+String.valueOf(viewHolder.getAdapterPosition()), Toast.LENGTH_SHORT).show();

                    /*This is where I've tried to get them with setters and use them
                            with getters in MessageToContact_Activity*/
                    String name = mListContacts.get(viewHolder.getAdapterPosition()).getName();
                    String number = mListContacts.get(viewHolder.getAdapterPosition()).getNumber();

                    MessageToContact_Activity mC = new MessageToContact_Activity();
                    mC.setName(name);
                    mC.setNumber(number);
                    /*----------------------------------------------------------------------------*/

                    onContactClick(view);
                }
            });
            return viewHolder;
        }

        public void onContactClick(View v){
            Intent myIntent = new Intent(v.getContext(), MessageToContact_Activity.class);
            mContext.startActivity(myIntent);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            TextView c_name, c_number;

            c_name = holder.cV_name;
            c_number = holder.cV_number;

            c_name.setText(mListContacts.get(position).getName());
            c_number.setText(mListContacts.get(position).getNumber());
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder{
            TextView cV_name, cV_number;
            AppCompatImageButton messageButton;

            private LinearLayout item_contact;

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

                cV_name = itemView.findViewById(R.id.name_contact);
                cV_number = itemView.findViewById(R.id.phone_contact );
                messageButton = itemView.findViewById(R.id.message_button);

                item_contact = (LinearLayout) itemView.findViewById(R.id.contact_item_id);

            }
        }
    }

/**..*//**..*/ 之间看到一些代码的地方,我只是试图隔离我获取名称和数字值的方法,并使用它们从 MessageToContact_Activity。 这是 MessageToContact_Activity:

的实现

    public class MessageToContact_Activity extends AppCompatActivity{
        private String name, number;

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

            Toolbar toolbar = findViewById(R.id.messageToolbar);
            setSupportActionBar(toolbar);

            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            /*************************/
            getSupportActionBar().setTitle(getName());
            getSupportActionBar().setSubtitle(getNumber());
            /*************************/
        }
        /*************************/
        public MessageToContact_Activity(){}

        public String getName() {
            return name;
        }

        public String getNumber() {
            return number;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setNumber(String number) {
            this.number = number;
        }
        /*************************/
        @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {

            if(item.getItemId() == android.R.id.home){
                finish();
            }
            return super.onOptionsItemSelected(item);
        }
    }

以下是联系人的项目:


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Contact Name"
            android:id="@+id/name_contact" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone Number"
            android:id="@+id/phone_contact"/>
    </LinearLayout>

Here is the output when I use getSupportActionBar().setTitle(getName()); and getSupportActionBar().setSubtitle(getNumber());

Here is the output when I use getSupportActionBar().setTitle("Name"); and getSupportActionBar().setSubtitle("Number");

我将非常感谢任何能帮助我的人。我有一种预感,解决方案并不难,但我仍然不知道。如果我的代码中有一些错误,请原谅我,但我仍处于 android 应用程序开发的开始阶段。

活动不是使用 new MyActivity() 创建的,而是需要使用意图启动,如:

Intent i = new Intent(this, MyActivity.class);
startActivity(i);

如果您需要向新 activity 传递一些参数,请将它们作为 extras 添加到您的意图中,如下所示:

Intent i = new Intent(this, MyActivity.class);
i.putExtra("name", "Bob");
i.putExtra("number", "12345");
startActivity(i);

所以在你的情况下,你需要用这样的东西替换你的 onClick 方法:

@Override
public void onClick(View view) {
    String name = mListContacts.get(viewHolder.getAdapterPosition()).getName();
    String number = mListContacts.get(viewHolder.getAdapterPosition()).getNumber();

    Intent myIntent = new Intent(v.getContext(), MessageToContact_Activity.class);
    myIntent.putExtra("name", name);
    myIntent.putExtra("number", number);
    mContext.startActivity(myIntent);
}

然后在您的 MessageToContact_Activity 中,您可以通过以下方式访问这些额外内容:

Bundle extras = getIntent().getExtras();
String name = extras.getString("name");
String number = extras.getString("number");