Ste*_* C. 5 java string android arraylist
我一直在互联网上搜索数小时。我已经看到了很多有关如何将项目从ArrayList添加到另一个ArrayList的示例/答案,但是似乎找不到任何解释如何将项目从ArrayList添加到ArrayList的内容。
我有一个向导,将选择保存到两个不同的ArrayList。第一个屏幕将所选项目保存到ArrayList,另一个屏幕将所选项目保存到ArrayList,然后最后一个屏幕显示从ArrayList中选择的总项目。
我已经尝试过了,stringArrayList.add(modelArrayList)但是这给出了String不能应用于模型的错误。
编辑:添加的类
为了弄清楚我正在使用的东西并使之更清楚。我正在使用WizarDroid库实现“向导”,并且我尝试合并两个不同ArrayList的页面基于此处的多选教程。
型号类别:
public class NSBaseModel {
private String baseName;
public NSBaseModel(String name)
{
baseName = name;
}
public String getBaseName() {
return baseName;
}
}
Run Code Online (Sandbox Code Playgroud)
向导页面:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的CreateList类中, public static ArrayList<String> nsList = new ArrayList<String>();
编辑
@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());
}
Run Code Online (Sandbox Code Playgroud)
你需要改变这一行
stringArrayList.add(modelArrayList);
Run Code Online (Sandbox Code Playgroud)
对此:
stringArrayList.add(modelArrayList.get(i).getBaseName());
Run Code Online (Sandbox Code Playgroud)
这样您就可以将模型 ArrayList 中的字符串名称添加到字符串 ArrayList 中。
| 归档时间: |
|
| 查看次数: |
9350 次 |
| 最近记录: |