简体   繁体   English

将ArrayList中的字符串对象转换为数组数组

[英]Convert String Objects in ArrayList to Array of Arrays

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 . 在Android SDK的Views示例中,有一个我想使用的View示例,称为ExpandableList1.java In order to use the view I have to pass a String[] groups structure and a String[][] children structure. 为了使用视图,我必须传递String[] groups结构和String[][] children结构。

I have no problem converting a list of strings from the menu objects to an array using groups.toArray . 我没有问题,可以使用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: 首先,我们使用字符串数组调用MyExpandableListAdapter:

 @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: 接下来的MyExpandableListAdapter及其构造函数显示了发生数组转换的位置:

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 . 如您在上面的代码片段中看到的,转换为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. 由于数组大小是如此固定,我将如何在Java中构建动态数组。

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;
}

Use an ArrayList ( res ) to dynamically add items and convert it to an Array ( mString ). 使用ArrayList( res )动态添加项目并将其转换为Array( mString )。

ArrayList<String> res = new ArrayList<String>();
res.add("Item");
String[] mString = (String[]) res.toArray(new String[res.size()]);

Assuming for example that you have a List<Menu> and each Menu can return a List<String> of favourites, I believe you want this: 假设您有一个List<Menu>并且每个Menu都可以返回一个收藏夹的List<String> ,我相信您会这样:

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()]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM