0

I am creating an AlertDialog custom class, called ActionDialog, which will contains a RecyclerView containing Buttons. I have a List of Button that I populate in the custom class ActionDialog (for now i just populate with useless Button just to try to use it, except one which I create in another class).

The problem is that when i create the AlertDialog, all buttons are showing empty, they are showed but with no text/no clicklistener (as you can see in the image below). (I have added a custom ActionListener to a Button in another class and then give it as parameter in ActionDialog class. Will it lose the ActionListener?)

Here is the result.

RecyclerView of Button, all empty

I will leave here my ActionDialog class code, and the adapter class.

This is ActionDialog class:

public class ActionDialog extends AlertDialog{

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private Button actionButtons;
private List<Button> buttons;
private Activity context;

public ActionDialog(@NonNull Activity context, Button actionButtons) {
    super(context);

    this.context = context;
    this.actionButtons = actionButtons;
    buttons = new ArrayList<>();

    initButton();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //requestWindowFeature(Window.FEATURE_NO_TITLE);



}

private void initButton(){

    initZoneButton();

    //TODO init all buttons

    Button b1 = new Button(context);
    b1.setText("ExampleButton1");

    Button b2 = new Button(context);
    b2.setText("ExampleButton2");
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String a;
        }
    });

    buttons.add(b1);
    buttons.add(b2);
}

private void initZoneButton(){

    buttons.add(actionButtons); //this button is created in another class and give as parameter in this class

    }


public void createDialog(){

    Builder mBuilder = new Builder(context);
    View view = context.getLayoutInflater().inflate(R.layout.dialog_actionbuttons_layout, null);

    mRecyclerView = view.findViewById(R.id.dialog_actionbuttons_rv);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(context);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new ActionButtonsAdapter(buttons);

    mRecyclerView.setAdapter(mAdapter);

    mBuilder.setView(view);
    mBuilder.create().show();

}
}

Here is the RecyclerView adapter class:

public class ActionButtonsAdapter extends RecyclerView.Adapter<ActionButtonsAdapter.ViewHolder>{

private List<Button> dataButtons;

static class ViewHolder extends RecyclerView.ViewHolder {

    Button actionButton;


    ViewHolder(View v) {
        super(v);

        actionButton = v.findViewById(R.id.action_button_rv);

    }

}

public ActionButtonsAdapter(List<Button> dataButtons){

    this.dataButtons = dataButtons;

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.actionButton = dataButtons.get(position);
    //i think the problem is here, maybe
}

@Override
public ActionButtonsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_actionbutton_layout, parent, false);
    return new ViewHolder(v);

}

@Override
public int getItemCount() {
    return dataButtons.size();
}

}

Tomas Jablonskis
  • 4,246
  • 4
  • 22
  • 40
Fyruz
  • 75
  • 1
  • 20
  • Don't create your own separate `List` of `Button`s. The `Button`s are being created in the `Adapter`. Your `List` should be the data that you want to set on those `Button`s. – Mike M. Jul 28 '18 at 12:00
  • Ok, but why? Data of Button is not stored? – Fyruz Jul 28 '18 at 12:41
  • I'm not sure what you're asking. – Mike M. Jul 28 '18 at 12:49
  • Why a Button that i create in another class is not accessible from the adapter ? (i mean the data of the Button) – Fyruz Jul 28 '18 at 12:52
  • It's not necessarily that. It's that it's pointless to create `Button`s twice, and essentially discarding half of them. You're already creating `Button`s in the `Adapter`. All that needs to be in the `List` is the data to set on them. That's generally how `Adapter`s work. – Mike M. Jul 28 '18 at 13:01

1 Answers1

2

I think in the onBindViewHolder method you should do what ever you want to do with your button.

Also there is no need for the list of buttons here. Make a list the data you need to be held in the Buttons RecyclerView.

I have a RecyclerView that will display Genres for restaurants lets say, So I will create a List of strings to hold these genres names (chickens, meats, etc,..)

Setting its text

holder.actionButton.setText(// Make use of position here);

Or Click Listeners.

Update

You can check google samples for recyclerview here

@Override

    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        Log.d(TAG, "Element " + position + " set.");

        // Get element from your dataset at this position and replace the contents of the view
        // with that element
        viewHolder.getTextView().setText(mDataSet[position]);
}

wheres mDataset is Array of Strings.

Xexolas
  • 229
  • 2
  • 3
  • 15