我建立了一个应用程序,从手机(在这种情况下,我的手机)读取联系人。对于显示联系人的活动,我使用了TabLayout和ViewPager。对于显示联系人的活动,我使用了RecyclerView。重点是,我处理了它如何读取联系人,之后我的下一步动作是打开一个外部活动(给联系人的信息_活动)后,点击一个联系人。在目前这个活动中(给联系人的信息_活动我想给我选择的联系人发送一条消息,首先我想把联系人的名字和号码设置成工具栏的标题和副标题。我决定使用工具栏而不是默认的ActionBar,因为它更灵活。问题是,我不知道如何获取姓名和号码的值,并将其用于MessageToContact_Activity。
以下是我的实现 联系人_碎片:
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;
}
}
以下是执行情况 联系人适配器:
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);
}
}
}
在那里你看到一些代码之间 /**..*/
和 /**..*/
我只是想把我获取名字和数字值的方法分离出来,用它们来设置标题和副标题。给联系人的信息_活动.这里是实现 给联系人的信息_活动:
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>
以下是我使用 getSupportActionBar().setTitle("Name"); and getSupportActionBar().setSubtitle("Number");
如果有谁能帮助我,我会非常感谢。我有一种预感,解决方法并不难,但我还是不知道。如果我的代码中存在一些错误,请原谅我,但我仍处于安卓应用开发的初级阶段。
活动没有被创建 new MyActivity()
相反,它们需要使用一个意图来启动,比如。
Intent i = new Intent(this, MyActivity.class);
startActivity(i);
如果你需要将一些参数传递给你的新活动,你可以将它们添加到你的意图中,如 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");