【问题标题】:Call contacts from a list in Android Studio从 Android Studio 的列表中呼叫联系人
【发布时间】:2018-04-26 20:59:00
【问题描述】:

我有一个 Android 应用程序,我在其中使用了显示联系人列表的线性布局。现在我想要的是,在选择联系人时,拨打指定的号码。

目前,它所做的只是显示联系人,但是当我点击通话时,它不会采取任何行动。我不知道怎么做标记

我尝试使用setOnClickListener()。但不成功

我的代码:

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

Dialog myDialog;


private List<ContactModel> contactModelList;
private Context mContext;
public ContactsAdapter(List<ContactModel> contactModelList, Context mContext){
    this.contactModelList = contactModelList;
    this.mContext = mContext;
}

@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.single_contact_view, null);
    ContactViewHolder contactViewHolder = new ContactViewHolder(view);
    return contactViewHolder;
}

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

    final ContactModel contactModel = contactModelList.get(position);
    holder.tvContactName.setText(contactModel.getContactName());
    holder.tvPhoneNumber.setText(contactModel.getContactNumber());


    holder.contactsRowLV.setOnClickListener(
            new View.OnClickListener()


    {
        @Override
        public void onClick(View view) {
            Toast.makeText(mContext,""+ contactModel.getContactNumber(),Toast.LENGTH_SHORT).show();



        }
    });


}

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

public static class ContactViewHolder extends RecyclerView.ViewHolder{

    ImageView ivContactImage;
    TextView tvContactName;
    TextView tvPhoneNumber;
    LinearLayout contactsRowLV;

    public ContactViewHolder(View itemView) {
        super(itemView);
        ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
        tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
        tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
        contactsRowLV = itemView.findViewById(R.id.contactsRowLV);
    }
}
}

在这里,我展示了我的 XML。请帮帮我

 <LinearLayout
    android:id="@+id/contactsRowLV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/ivContactImage"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:layout_gravity="center"
        android:src="@drawable/ic_person"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/tvContactName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:textSize="30dp"
            android:textStyle="bold"
            android:textColor="@android:color/primary_text_light"
            android:text="Name"/>

        <TextView
            android:id="@+id/tvPhoneNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:textSize="25dp"
            android:textColor="@android:color/primary_text_light"
            android:text="Phone"/>

    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 这与your previous question有何不同?
  • 如果你现在不用RecyclerView,为什么还要RecyclerView.Adapter
  • 你的意思是你想通过点击特定的行来呼叫特定的人?

标签: android android-studio android-recyclerview onclick


【解决方案1】:

您只需在setOnClickListener() 中添加以下代码。此代码用于拨打特定号码。

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

final ContactModel contactModel = contactModelList.get(position);
holder.tvContactName.setText(contactModel.getContactName());
holder.tvPhoneNumber.setText(contactModel.getContactNumber());


holder.contactsRowLV.setOnClickListener(
        new View.OnClickListener(){
    @Override
    public void onClick(View view) {
        Toast.makeText(mContext,""+ contactModel.getContactNumber(),Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" +contactModel.getContactNumber()));
        mContext.startActivity(intent);


    }
});


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多