私は何時間もインターネットとSOを探してきました。私は、ArrayListから別のArrayListに項目を追加する方法について多くの例と答えを見てきましたが、ArrayListからArrayListに項目を追加する方法を説明するものは見つけられません。ArrayList <Model>のアイテムをAndroidのArrayList <String>に追加するにはどうすればよいですか?
2つの異なるArrayListに選択肢を保存するウィザードがあります。最初の画面では、選択した項目をArrayListに保存し、別の画面では選択した項目をArrayListに保存し、最後の画面にはArrayListから選択した合計項目を表示します。
私はstringArrayList.add(modelArrayList)
を試しましたが、文字列をモデルに適用できないというエラーが発生します。
EDIT:追加されたクラス
を明確にし、私が働いているものにのように物事をより明確にします。 WizarDroid Libraryを使用して「ウィザード」を実装しています.2つの異なるArrayListをマージしようとしているページは、この多肢選択チュートリアルhereに基づいています。
モデルクラス:
public class NSBaseModel {
private String baseName;
public NSBaseModel(String name)
{
baseName = name;
}
public String getBaseName() {
return baseName;
}
}
ウィザードページ:私のCreateListクラスで
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;
}
}
}
私はpublic static ArrayList<String> nsList = new ArrayList<String>();
EDIT
が@Polichronis Charitidisに私の問題のおかげで解決しました。彼の答えは正しい解決策に終わった。それが私のために働いていなかった理由は、私が両方のforループでint i
を宣言し、システムと結果を混乱させたからです。 2番目のforループの変数int i
を別の文字に変更しなければなりませんでした。これは私の2番目の "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型を追加することができないことができますし、 –
の一覧を表示するだけのタイプのモデルを追加することができますが –