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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With