在Adapter类中调用自定义对话框

时间:2017-05-05 16:49:35

标签: android dialog android-recyclerview adapter

我创建了一个简单的Custom dialog类,我想在点击RecycleView中的一行后显示它。 我的对话框类看起来:

public class AddToQueueDialog extends Dialog implements View.OnClickListener {

 Activity mActivity;
private TextView textView1;
private TextView textView2;
private Button save;

public AddToQueueDialog(Activity activity){
    super(activity);
    mActivity = activity;
}

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_to_queue);
        textView1 = (TextView) findViewById(R.id.textView5);
        textView2 = (TextView) findViewById(R.id.textView6);
        save = (Button) findViewById(R.id.button4);
        save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == save.getId()){
            Log.d("save", "save");
        }
    }
}

我想知道如何正确地调用adapter RecycleView看起来: public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public ViewHolder(Context context, View itemView, List<WashLocation> washLocations) { super(itemView); this.context = context; info = (TextView) itemView.findViewById(R.id.textView); favorite = (Button) itemView.findViewById(R.id.addToFav); favorite.setOnClickListener(this); info.setOnClickListener(this); this.washLocations = washLocations; dataBaseHelper = new DataBaseHelper(context); } @Override public void onClick(View v) { if(v.getId() == info.getId()){ AddToQueueDialog addToQueueDialog = new AddToQueueDialog(MapsActivity.this); addToQueueDialog.show(); } (片)

Custom dialog

在我的Activity课程中,我需要在arg中使用Activity作为构造函数,但我不知道应该在Adapter类<{1}}中传递哪个import glob import numpy as np from PIL import Image filename = glob.glob('/home/ns3/PycharmProjects/untitled1/stego.pgm') im= Image.open(filename) (x,y) = im.size I = np.array(im.getdata()).reshape(y, x) / p>

1 个答案:

答案 0 :(得分:2)

class MyAdapter extends RecyclerView.Adapter<VH> {

  // set this field through setter or constructor
  private OnClickListener mMyOnClickListener;

  ...

  void onBindViewHolder(..., VH viewHolder) {
    viewHolder.rootView.setOnClickListener(() -> {
       if (mOnClickListener != null) {
          mOnClickListener.onClick();
       }
     });
  }

  static class VH extends ViewHolder {
    View rootView;

    VH(View itemView) {
      super(itemView);
      rootView = itemView;
    }
  }
}

class MainActivity extends AppCompatActivity {

  ...

  void setUpRecyclerView(){
    ...
    adapter.setMyOnClickListener(() -> {
       new Dialog(MainActivity.this).show();
     });
  }
}