问题
I have created a simple Custom dialog
class and I want to display it after clicking on a row in RecycleView
.
My dialog class looks:
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");
}
}
}
And I'm wondering how to properly call in adapter
of RecycleView
which looks:
(piece)
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();
}
In my Custom dialog
class I need an Activity
in arg as a constructor but I don't know which Activity
should I pass there in Adapter
class
回答1:
Just pass the context
of your current Activity
to your Adapter
class and use it when you are creating an instance of AddToQueueDialog
.
For example:
MainActivity.java
public class MainActivity extends AppCompatActivity {
Context mContext;
// Views
RecyclerView mRecyclerView;
// Values
List<Recipe> mRecipeList;
// Adapter
RecipeAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
// Views
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
.................
..........................
// Values
mRecipeList = new ArrayList<Recipe>();
// Adapter
mAdapter = new RecipeAdapter(mContext, mRecipeList);
// Set adapter to RecyclerView
mRecyclerView.setAdapter(mAdapter);
.................
..........................
}
}
RecipeAdapter.java
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {
Context mContext;
LayoutInflater mInflater;
// List
List<Recipe> mRecipeList;
public GroupListAdapter(Context context, List<Recipe> listRecipe) {
this.mContext = context;
this.mRecipeList = listRecipe;
mInflater = LayoutInflater.from(mContext);
}
.....................
...............................
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(View itemView) {
super(itemView);
info = (TextView) itemView.findViewById(R.id.textView);
favorite = (Button) itemView.findViewById(R.id.addToFav);
favorite.setOnClickListener(this);
info.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == info.getId()){
AddToQueueDialog addToQueueDialog = new AddToQueueDialog(mContext);
addToQueueDialog.show();
}
}
}
............
......................
}
Hope this will help~
回答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();
});
}
}
来源:https://stackoverflow.com/questions/43810106/call-custom-dialog-in-adapter-class