Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

android - Call custom dialog in Adapter class

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...