首页 > 解决方案 > 如何在不单击的情况下在适配器类中显示警报对话框?

问题描述

我试图在适配器类中显示警报对话框而不单击任何项​​目。当对话框出现时,它将从适配器类的第一行获取值,该值将显示在对话框中,当我单击对话框中的按钮时,将调用下一个活动。

警报对话框将显示在此行之前Utility.makecall(mContext, mData.get(position).getPhone(),SharedPref.Getsimetype(SharedPref.SIMTYPE,""));,并且当我单击对话框中的按钮时执行此行。

public class Contactadapter extends RecyclerView.Adapter<Contactadapter.MyViewHolder> {
private Context mContext;
private List<Callcontactsmodel> mData;
private Callcontactfragment callcontactfragment;

public Contactadapter(Context mContext, Callcontactfragment callcontactfragment) {
    this.mContext = mContext;
    this.mData = new ArrayList<>();
    this.callcontactfragment = callcontactfragment;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    LayoutInflater mInflater = LayoutInflater.from(mContext);
    view = mInflater.inflate(R.layout.rv_contacts, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.setIsRecyclable(false);
    try {
        holder.tv_numbers.setText(mData.get(position).getPhone() == null ? "" : mData.get(position).getPhone());
        holder.tv_time.setText(mData.get(position).getName() == null ? "" : mData.get(position).getName());
        holder.tv_type.setText(mData.get(position).getContact_type().getName() == null ? "" : mData.get(position).getContact_type().getName());
        if (0 == position && callcontactfragment.auto_call){
            callcontactfragment.setCallcontactid(mData.get(position).getId().toString());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Utility.makecall(mContext, mData.get(position).getPhone(),SharedPref.Getsimetype(SharedPref.SIMTYPE,""));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public void setItems(List<Callcontactsmodel> items) {
    mData = items;
    notifyDataSetChanged();
}

public void addItems(List<Callcontactsmodel> items) {
    mData.addAll(items);
    notifyDataSetChanged();
}

public void clearitem() {
    mData.clear();
    notifyDataSetChanged();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.tv_time)
    AppCompatTextView tv_time;
    @BindView(R.id.tv_type)
    AppCompatTextView tv_type;
    @BindView(R.id.tv_numbers)
    AppCompatTextView tv_numbers;

    public MyViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        tv_numbers.setOnClickListener(v -> {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                callcontactfragment.setCallcontactid(mData.get(getAdapterPosition()).getId().toString());
                Utility.makecall(mContext, mData.get(getAdapterPosition()).getPhone(),SharedPref.Getsimetype(SharedPref.SIMTYPE,""));
            }
        });

标签: android

解决方案


我不确定它是否会起作用,但请在您的 onBindViewHolder 中尝试检查位置

 if(position == 0){
     MaterialAlertDialogBuilder(mContext)
        .setTitle("Your title")
        .setMessage("pass your first row data as in string form")
        // add buttons positive or negative
        .create()
        .show()
 }

推荐阅读