Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android fill singlechoiceitems dialog with arraylist values

Tags:

java

android

I defined an array like,

String[] types = {"item0", "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9"};

Once I fill it, I use its results to fill an alert dialog with single choice items,

AlertDialog.Builder b = new Builder(this);
b.setTitle("Results");

b.setSingleChoiceItems(types, 0, new  DialogInterface.OnClickListener() {

problem is that result size is variable and item list is filled always with fixed value. I would like to use an ArrayList but setSingleChoiceItems method doesn't accept it. How to reach it? Thank you

like image 214
Jaume Avatar asked Nov 25 '13 07:11

Jaume


People also ask

How do you toast an arrayList?

Like donfuxx said, you need to create your arrayList outside of your onclicklistener. As the user clicks an item, it will be added to your arrayList. Then loop over the list to fill a string called allItems, then show allItems in a toast.

How do I show a list in alerts?

Navigate to the app > res > layout and create a new layout file. Add a ListView as shown below. This layout would be displayed inside the AlertDialog.


1 Answers

Use ArrayList to build your items:

List<String> items = new ArrayList<String>();
items.add("this");
items.add("that");

Then convert it to an array when passing it to setSingleChoiceItems:

b.setSingleChoiceItems(items.toArray(new String[items.size()]), 0, new  DialogInterface.OnClickListener() { ... });

If you wanted to implement a single datasource, you could implement the Cursor interface but that might be overkill for this.

like image 196
CodingIntrigue Avatar answered Oct 12 '22 23:10

CodingIntrigue