java 如何在 Android 中将项目从 ArrayList<Model> 添加到 ArrayList<String>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37435187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add items from ArrayList<Model> to ArrayList<String> in Android?
提问by Steve C.
I've been searching all over the internet and SO for hours. I've seen numerous examples/answers for how to add items from ArrayList to another ArrayList but can't seem to find anything that explains how to add items from an ArrayList to ArrayList.
我已经在互联网上搜索了几个小时。我已经看到了许多关于如何将 ArrayList 中的项目添加到另一个 ArrayList 的示例/答案,但似乎找不到任何解释如何将 ArrayList 中的项目添加到 ArrayList 的内容。
I have a wizard that saves selections to two different ArrayList's. The first screen saves the selected items to an ArrayList, another screen saves selected items to an ArrayList then the last screen displays the total items selected from the ArrayList.
我有一个向导,可以将选择保存到两个不同的 ArrayList 中。第一个屏幕将所选项目保存到 ArrayList,另一个屏幕将所选项目保存到 ArrayList,然后最后一个屏幕显示从 ArrayList 中选择的总项目。
I've tried stringArrayList.add(modelArrayList)
but that gives the error that String cannot be applied to model.
我试过了,stringArrayList.add(modelArrayList)
但这给出了 String 不能应用于模型的错误。
EDIT: Added classes
编辑:添加类
To clarify and make things more clear as to what I'm working with. I'm implementing a "wizard" using WizarDroid Libraryand the page I'm trying to merge two different ArrayList's on is based off of this multi choice tutorial here.
澄清并使事情更清楚我正在处理的事情。我实施“向导”使用WizarDroid图书馆,我试图合并两个不同的ArrayList对这个多选择教程是基于关闭页面这里。
Model Class:
型号分类:
public class NSBaseModel {
private String baseName;
public NSBaseModel(String name)
{
baseName = name;
}
public String getBaseName() {
return baseName;
}
}
Wizard page:
向导页面:
public class FormStepBase extends WizardStep {
ArrayList<NSBaseModel> baseNames;
ArrayList<NSBaseModel> selectedItems = new ArrayList<NSBaseModel>();
NSBaseAdapter myAdapter;
ListView myListView;
//You must have an empty constructor for every step
public FormStepBase() {
}
//Set your layout here
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
List<String> Lines = Arrays.asList(getResources().getStringArray(R.array.NAL_smoothie_base));
baseNames = new ArrayList<NSBaseModel>();
baseNames.add...//Long list of items here
View v = inflater.inflate(R.layout.step_base, container, false);
myListView = (ListView)v.findViewById(R.id.nsBaseListView);
myAdapter = new NSBaseAdapter(v.getContext(), R.layout.row_base_layout, baseNames);
myListView.setAdapter(myAdapter);
myListView.setItemsCanFocus(false);
return v;
}
/**
* Called whenever the wizard proceeds to the next step or goes back to the previous step
*/
@Override
public void onExit(int exitCode) {
switch (exitCode) {
case WizardStep.EXIT_NEXT:
//saveBaseIngredients();
final SparseBooleanArray checkedItems = myListView.getCheckedItemPositions();
int checkedItemsCount = checkedItems.size();
for (int i = 0; i < checkedItemsCount; ++i) {
// Item position in adapter
int position = checkedItems.keyAt(i);
// Add team if item is checked == TRUE!
if(checkedItems.valueAt(i))
selectedItems.add(myAdapter.getItem(position));
for (int i = 0; i < selectedItems.size(); i++) {
selectedItems.get(i);
System.out.print("option list size");
System.out.print(selectedItems.size());
//reference to static ArrayList<String> here
CreateList.nsList.add(selectedItems);
}
}
if(selectedItems.size() < 2)
Toast.makeText(getContext(), "Need to select two or more items.", Toast.LENGTH_SHORT).show();
else
{
// Just logging the output.
for(NSBaseModel t : selectedItems)
Log.d("SELECTED TEAMS: ", t.getBaseName());
//Thought I could get the strings from here since they show in the logcat
}
break;
case WizardStep.EXIT_PREVIOUS:
//Do nothing...
break;
}
}
}
In my CreateList class I have public static ArrayList<String> nsList = new ArrayList<String>();
在我的 CreateList 类中,我有 public static ArrayList<String> nsList = new ArrayList<String>();
EDIT
编辑
Solved my issue thanks to @Polichronis Charitidis. His answer ended up being the correct solution. The reason it wasn't working for me is because I had declared int i
in both my for loops thus confusing the system and result. I had to change the int i
variable in my second for loop to a different letter and now it works. This is how my second "for" loop looks now.
感谢@Polichronis Charitidis 解决了我的问题。他的回答最终成为正确的解决方案。它对我不起作用的原因是因为我int i
在两个 for 循环中都进行了声明,从而混淆了系统和结果。我不得不将int i
第二个 for 循环中的变量更改为不同的字母,现在它可以工作了。这就是我的第二个“for”循环现在的样子。
for (int j = 0; j < selectedItems.size(); j++) {
selectedItems.get(i);
System.out.print("option list size");
System.out.print(selectedItems.size());
CreateList.nsList.add(selectedItems.get(j).getBaseName());
}
采纳答案by xro7
you need to change this line
你需要改变这一行
stringArrayList.add(modelArrayList);
to this:
对此:
stringArrayList.add(modelArrayList.get(i).getBaseName());
so that you can add the String name from the Model ArrayList to the String ArrayList.
这样您就可以将 Model ArrayList 中的 String 名称添加到 String ArrayList。
回答by Nowshad
why not is to loop over it and convert each item into a new Arraylist of strings yourself:
为什么不循环遍历它并自己将每个项目转换为新的字符串数组列表:
List<String> strings = new ArrayList<>();
for (Object yourmodelArralistObject: list) {
strings.add(yourmodelArralistObject!= null ?
yourmodelArralistObject.toString() : null);
}
Note that you should be declaring against the interface (java.util.List in this case), not the implementation.
请注意,您应该针对接口(在本例中为 java.util.List)而不是实现进行声明。
回答by anthonymonori
For a start, in the above code (which I am not quoting here, because it is plain wrong) you are iterating though the array list and at every single iteration you are adding the whole array list to another one. That is just wrong and won't work.
首先,在上面的代码(我没有在这里引用,因为它完全错误)中,您正在遍历数组列表,并且在每次迭代时,您都将整个数组列表添加到另一个列表中。那是错误的,是行不通的。
First make sure you initialize the stringArrayList before (you are not showing that in the above snippet, and I expect you doing so, but just to be sure...
Use for loops as they should be used:
ArrayList stringArrayList = new ArrayList<>(); for (int i = 0; i < modelArrayList.size(); i++) { stringArrayList.add(modelArrayList.get(i); }
As
String
is anObject
and not a primitive, you are adding references to theArrayList
and therefore even if you add it from oneArrayList
to another one, it will be the same object, and not two differentObjects
. If you modify it in one of them, it will change in the other too. Implement a way to properly copy it.
首先确保你之前初始化了 stringArrayList(你没有在上面的代码片段中显示它,我希望你这样做,但只是为了确定......
使用 for 循环,因为它们应该被使用:
ArrayList stringArrayList = new ArrayList<>(); for (int i = 0; i < modelArrayList.size(); i++) { stringArrayList.add(modelArrayList.get(i); }
由于
String
是 anObject
而不是原语,您正在添加对 的引用ArrayList
,因此即使您将它从一个添加ArrayList
到另一个,它也将是同一个对象,而不是两个不同的Objects
. 如果您在其中一个中修改它,它也会在另一个中更改。实施一种正确复制它的方法。
Hope that helps!
希望有帮助!