Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 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 Answer

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~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...