在Adapter类中调用自定义对话框 [英] Call custom dialog in Adapter class
问题描述
我已经创建了一个简单的自定义对话框
类,我想在 RecycleView
中单击一行后显示它。
我的对话框类看起来是:
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");
}
}
}
我想知道如何正确调用 RecycleView
的 adapter
,其外观为:
(件)
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();
}
在我的自定义对话框中
类我需要在arg中使用 Activity
作为构造函数,但我不知道我应该在其中传递哪个 Activity
适配器
类
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
推荐答案
只需传递上下文当前
到 Activity
中的 Adapter
类中,并在创建实例时使用 AddToQueueDialog
。
Just pass the context
of your current Activity
to your Adapter
class and use it when you are creating an instance of AddToQueueDialog
.
例如:
MainActivity.java
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
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();
}
}
}
............
......................
}
希望这会有所帮助〜
这篇关于在Adapter类中调用自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!