从Android Studio

时间:2018-04-26 20:59:00

标签: android android-studio android-recyclerview onclick

我有一个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>

1 个答案:

答案 0 :(得分:0)

您只需在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);


    }
});


}