将ArrayAdapter转换为List <string> </string>

时间:2013-09-11 17:57:43

标签: android arraylist android-arrayadapter

我有一个数组适配器(字符串),并希望将其转换为List<String>,但经过一些谷歌搜索和几次尝试之后,我就不知道如何做到这一点。

我尝试了以下内容;

for(int i = 0; i < adapter./*what?*/; i++){
     //get each item and add it to the list
}

但这不起作用,因为似乎没有adapter.lengthadapter.size()方法或变量。

然后我尝试了这种类型的for循环

for (String s: adapter){
    //add s to the list
}

但是适配器不能在foreach循环中使用。

然后我做了一些谷歌搜索方法(在数组中),从适配器转换为列表,但没有找到任何内容。

最好的方法是什么?它甚至可能吗?

2 个答案:

答案 0 :(得分:5)

for(int i = 0; i < adapter.getCount(); i++){
     String str = (String)adapter.getItem(i);
}

答案 1 :(得分:2)

试试这个

// Note to the clown who attempted to edit this code.
// this is an input parameter to this code.
ArrayAdapter blammo; 
List<String> kapow = new LinkedList<String>(); // ArrayList if you prefer.

for (int index = 0; index < blammo.getCount(); ++index)
{
    String value = (String)blammo.getItem(index);
    // Option 2: String value = (blammo.getItem(index)).toString();
    kapow.add(value);
}

// kapow is a List<String> that contains each element in the blammo ArrayAdapter.

如果ArrayAdapter的元素不是字符串,请使用选项2.