【发布时间】:2016-09-22 22:37:42
【问题描述】:
我已经在互联网上搜索了好几个小时。我已经看到了许多关于如何将 ArrayList 中的项目添加到另一个 ArrayList 的示例/答案,但似乎找不到任何解释如何将 ArrayList 中的项目添加到 ArrayList 的内容。
我有一个将选择保存到两个不同 ArrayList 的向导。第一个屏幕将所选项目保存到 ArrayList,另一个屏幕将所选项目保存到 ArrayList,然后最后一个屏幕显示从 ArrayList 中选择的总项目。
我尝试过stringArrayList.add(modelArrayList),但这给出了字符串无法应用于模型的错误。
编辑:添加类
澄清并让我更清楚地了解我正在处理的内容。 我正在使用WizarDroid Library 实现一个“向导”,而我试图合并两个不同 ArrayList 的页面基于此多选教程here。
模型类:
public class NSBaseModel {
private String baseName;
public NSBaseModel(String name)
{
baseName = name;
}
public String getBaseName() {
return baseName;
}
}
向导页面:
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;
}
}
}
在我的 CreateList 类中,我有 public static ArrayList<String> nsList = new ArrayList<String>();
编辑
感谢@Polichronis Charitidis,解决了我的问题。他的回答最终成为正确的解决方案。它对我不起作用的原因是我在我的两个 for 循环中都声明了 int i,从而混淆了系统和结果。我不得不将第二个 for 循环中的 int i 变量更改为不同的字母,现在它可以工作了。这就是我的第二个“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());
}
【问题讨论】:
-
stringArrayList和modelArrayList声明在哪里,它们里面是什么? -
不能,只能将类型 String 添加到 List
并且只能将类型 Model 添加到 List -
显示你的
Model class -
请看我的编辑
标签: java android string arraylist