gpt4 book ai didi

android - 在 RecyclerView 的 onBindViewHolder 中调用的自定义对话框按钮

转载 作者:行者123 更新时间:2023-11-29 01:30:07 24 4
gpt4 key购买 nike

我正在尝试让自定义对话框的 OnClick 删除并更新从 RecyclerView 单击的项目。

    final Dialog rightDialog = new Dialog(context1);
...
TextView textOKRight = (TextView) rightDialog.findViewById(R.id.text_ok);
textOKRight.setText("NEXT");
textOKRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// MainAdapter.ViewHolder ccd = new MainAdapter.ViewHolder(itemView, listener);
// ccd.removeCaller();
rightDialog.dismiss();
}
});
rightDialog.show();

主适配器

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>{
..
private static ViewHolder.IndividualClickListener mListenerCallback;
private static ViewHolder.IndividualClickListener mListener;
private MainAdapter mMyAdapter;
public AnimatorSampleActivity animatorActivity;

public MainAdapter(Context context, ArrayList<Question> dataSet) {
mContext = context;
questions = dataSet;
...
}


public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public IndividualClickListener mListener;
...

public ViewHolder(View itemView, IndividualClickListener listener) {
super(itemView);
mListener = listener;
image = (ImageView) itemView.findViewById(R.id.imageMic);
imageOne = (ImageView) itemView.findViewById(R.id.imageOne);
text = (TextView) itemView.findViewById(R.id.text);
itemView.setOnClickListener(this);
image.setOnClickListener(this);
}

//////THIS IS THE METHOD I WOULD LIKE TO CALL FROM THE DIALOG///////
public void removeCaller() {
mListener.remove1(getLayoutPosition());
Log.d(TAG, "Main Adapter Called remove1 from removeCaller()");
}

@Override
public void onClick(View v) {

if (v instanceof ImageView) {

} else {

}

}

public interface IndividualClickListener {

void onTomato(ImageView callerImage, int position);
void onPotato(View caller, int position);
void remove1(int position);
void onDialogClick(TextView textOKRight);

}

}

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

}


@Override
public void onBindViewHolder(final MainAdapter.ViewHolder holder, final int position) {
Question currentQuestion = questions.get(position);
///I CREATED A DIALOG HERE, Dialog.Builder AND IM ABLE TO HAVE THE DIALOG DIMISS AND REMOVE THE ITEM, BUT I CANT FIGURE OUT HOW TO DO IT FROM MY CUSTOM DIALOG OUTSIDE MainAdapter////
Picasso.with(mContext).load(currentQuestion.drawableId).into(holder.getImageView());
Picasso.with(mContext).load(currentQuestion.iconId).into(holder.getImageView1());

holder.getImageView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v instanceof ImageView) {
//holder.removeCaller();
//mAdapterCallback.remove1(position);
mListenerCallback.onTomato((ImageView) v, position);
Log.d(TAG, "Main Adapter Called remove from OnBindViewHolder"+""+position);

}else{
mListenerCallback.onPotato((ImageView) v, position);
}
}
});

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_list_item, parent, false);

final MainAdapter.ViewHolder vh = new ViewHolder(v, new MainAdapter.ViewHolder.IndividualClickListener(){

@Override
public void onTomato(ImageView callerImage, int position) {
Log.d(TAG, "Main Adapter Called OnTomato OnCreateVIewHolder");

}

@Override
public void onPotato(View caller, int position) {

}
@Override
public void remove1(int position) {
questions.remove(position);
notifyItemRemoved(position);
Log.d(TAG, "Main Adapter Called remove1 from OnCreaterViewHolder" + position);

}

@Override
public void onDialogClick(TextView textOKRight) {

}


});

return vh;
}


public interface callerCallBack{
.....

}

}

更新

public class NewCustomDialog extends Dialog   {
private static dialogCallerCallBack mdialogCallerCallBack;
private MainAdapter.ViewHolder.IndividualClickListener listener;

public NewCustomDialog(Activity a, dialogCallerCallBack mdialogCallerCallBack, Context dContext, TextView dialogTitleRight, TextView rightMsgRight, TextView rightAnswerRight, TextView textOKRight, ImageView imageRight) {
super(a);
this.dialogTitleRight = dialogTitleRight;
this.rightAnswerRight = rightAnswerRight;
this.rightMsgRight = rightMsgRight;
this.textOKRight = textOKRight;
this.imageRight = imageRight;
this.dContext = dContext;
//pass this so it can be used
this.mdialogCallerCallBack=mdialogCallerCallBack;

this.c =a;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//final Dialog rightDialog = new Dialog(context);
getWindow().setBackgroundDrawable(new ColorDrawable(0));
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mt_alert_right_dialog);
setCanceledOnTouchOutside(false);
setCancelable(false);
TextView dialogTitleRight = (TextView) findViewById(R.id.txt_title);
TextView rightMsgRight = (TextView) findViewById(R.id.right_msg);
TextView rightAnswerRight = (TextView) findViewById(R.id.right_answer);
final TextView textOKRight = (TextView) findViewById(R.id.text_ok);
ImageView imageRight = (ImageView) findViewById(R.id.image);
textOKRight.setText("NEXT");
textOKRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mdialogCallerCallBack.onDialogClick(((TextView)textOKRight));

}
});
}


public interface dialogCallerCallBack{
void onDialogClick(TextView textOKRight);
}
}

错误

Process: jp.wasabeef.example.recyclerview, PID: 28523
java.lang.NullPointerException at jp.wasabeef.example.recyclerview.NewCustomDialog$1.onClick(NewCustomDialog.java:82)

第82行是

mdialogCallerCallBack.onDialogClick(((TextView)textOKRight));

最佳答案

如果您的问题是使用 getFragmentManaget 来实现,请执行以下操作:创建

FragmentManager fm = getFragmentManager();

在您的 Fragment 中并将其传递给您的 RecyclerView 然后再次将其传递给您的 ViewHolder

注意:传递意味着将它作为参数发送给构造函数

关于android - 在 RecyclerView 的 onBindViewHolder 中调用的自定义对话框按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31685842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com