android - 如何在不单击的情况下在适配器类中显示警报对话框?
问题描述
我试图在适配器类中显示警报对话框而不单击任何项目。当对话框出现时,它将从适配器类的第一行获取值,该值将显示在对话框中,当我单击对话框中的按钮时,将调用下一个活动。
警报对话框将显示在此行之前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,""));
}
});
解决方案
我不确定它是否会起作用,但请在您的 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()
}
推荐阅读
- bert-language-model - 微调bert模型时嵌入文章标题和文本
- r - 从 R markdown 生成 HTML 文档时,LaTeX 不会在使用 ggplotly 创建的绘图中呈现
- .net - 邮递员响应中未显示数据注释错误消息
- cpu - 缺页导致高 KVM CPU
- python - Python海龟bgpic在python中瞬间消失
- django - 循环导入 Django,我迷路了...... :(
- pandas - 熊猫中连续非空值的平均值
- javascript - p5.js 无法识别其他文件中的代码
- excel - 如果没有结果,VBA 数据透视表添加文本
- mysql - 如何在tableau的不同数据源中使用具有相同列名的过滤器