通过Android Studio中的列表呼叫联系人 [英] Call contacts from a list in Android Studio

查看:179
本文介绍了通过Android Studio中的列表呼叫联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Android application where I used Linear Layout that shows a list of contacts. Now what I want is that when selecting the contacts, call the indicated number.

目前,它所做的只是显示联系人,但是当我单击通话时,它不执行任何操作.我不知道该怎么做

For now, all it do is show the contacts, but when I click on call, it does not take any action. I do not know how to make to mark

我尝试了setOnClickListener().但是没有成功

I tried with setOnClickListener().But Unsuccessful

我的代码:

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格式.请帮助我

Here I show my XML of how I have. Help me please

 <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>

推荐答案

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

You just need to add following code in setOnClickListener(). This code is used to call particular number.

@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);


    }
});


}

这篇关于通过Android Studio中的列表呼叫联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆