-1

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

I tried with setOnClickListener().But Unsuccessful

My code:

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

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>
Karan sharma
  • 1,511
  • 1
  • 13
  • 37

1 Answers1

0

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


    }
});


}
Karan sharma
  • 1,511
  • 1
  • 13
  • 37