Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a menu structure that outputs a list of favourite configuration items.

In the Android SDK in the Views examples there is a view example that I would like to use called ExpandableList1.java. In order to use the view I have to pass a String[] groups structure and a String[][] children structure.

I have no problem converting a list of strings from the menu objects to an array using groups.toArray. The problem that I have is with converting the favourites items to an array of arrays. The favourite items are array lists contained in the menu object.

The relevant parts of the code is pasted here. First we call MyExpandableListAdapter with an array of strings:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayList<String> favouriteMenuList = Main.menuList.getFavouriteMenusFullPath();        

    mAdapter = new MyExpandableListAdapter(favouriteMenuList);
    setListAdapter(mAdapter);
    registerForContextMenu(getExpandableListView());
}

Next MyExpandableListAdapter and it's constructor is show where the array conversion happens:

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    // private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String [] groups;
    private String[][] children = {
             { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };        

    public MyExpandableListAdapter(ArrayList<String> groups) {          
        this.groups = groups.toArray(new String[groups.size()]);
    }

As you can see in the above code snippet there is no problem converting to String[] groups. My idea is to iterate over the menu objects, extract the list of favourites, and then?? How would I build a dynamic array in Java since array sizes are so fixed.

Here is the outer loop I have in mind:

public ArrayList<FavouritesObject> getFavouriteItems() {
    ArrayList<FavouritesObject> favouritesList = new ArrayList<FavouritesObject>();

    for (MenuObject m : allMenusList) {
        if (m.isFavourite) {
            favouritesList.add(m.getFavouriteItems());
        }           
    }
    return favouritesList;
}
share|improve this question
    
Work with a list of lists, then iterate the list and use toArray? –  Johan Sjöberg Mar 17 '11 at 9:09
    
As arrays are best in a fixed size for performance reasons, wouldn't it be good to calculate the size of the array, create it with that size, and then add the child arrays to it ? –  Uw Concept Mar 17 '11 at 9:10
    
Not related. @Eugene van der Merwe, since you own Snowball.co.za, could you, perhaps, provide hosting services for java applications? Preferably GlassFish, Tomcat, JBoss? –  Buhake Sindi Mar 17 '11 at 9:11

2 Answers 2

up vote 1 down vote accepted

Assuming for example that you have a List<Menu> and each Menu can return a List<String> of favourites, I believe you want this:

String[][] children = new String[menus.size()][];
for (int i = 0; i < menus.size(); i++) {
    List<String> favourites = menus.get(i).getFavourites();
    children[i] = favourites.toArray(new String[favourites.size()]);
}
share|improve this answer
    
This looks very promising I will try it in the next three hours and let you know. –  Eugene van der Merwe Mar 17 '11 at 9:31
    
Freaking awesome! Working like a charm. Thank you. –  Eugene van der Merwe Mar 17 '11 at 17:25

Use an ArrayList (res) to dynamically add items and convert it to an Array (mString).

ArrayList<String> res = new ArrayList<String>();
res.add("Item");
String[] mString = (String[]) res.toArray(new String[res.size()]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.