-2

I have a problem. In my app I have an Activity where I need to create x Button, then add to all this Button an ActionListener that start another activity.

In details. I have a Database table where I store x name.

In my Activity I need to have a Button for each name stored in the db and to add to each button an actionListener so when you click it it start another activity (which is the same for all buttons) with a putExtra String (which is unique for each button).

I thought that I can get all the name via an AsyncTask. But I can't figure out how to add the Buttons to the basic layout and add the Action Listener to them.

Anyone can help?

P.S. The "putExtra" String is the name I got from the db. So I also thought to get them in String[] array. Then create a Button[] array (don't know if it's possible) and create a new Button() foreach element of the String[] array. Then foreach element in the Button[] array I would putExtra the relative index String. Like Button[0] have as a putExtra the String[0], Button[1] the String[1] and so on.

  • I would suggest looking at the ListView and the Recyclerview. This allows you to create list of items. You don't have to create each view manually. – Rockney Mar 25 '18 at 18:22
  • @Rockney thanks, I done it (using Recyclerview). But how can I add `onClickListener` to my elements? – we love lama company Mar 26 '18 at 14:14
  • You should take a look at this stackoverflow post: https://stackoverflow.com/questions/24471109/recyclerview-onclick – Rockney Mar 26 '18 at 14:36
  • @Rockney thanks, but I don't understand where I have to put the code. So, the `@Override` of `onCreateViewHolder` should go in my Adapter's class. But where I have to put the `@Override` of `onClick` ? – we love lama company Mar 27 '18 at 11:44

1 Answers1

0

The solution is a RecyclerView layout with the listener that you can find at this thread