Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add items from ArrayList<Model> to ArrayList<String> in Android?

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.

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.

I've tried stringArrayList.add(modelArrayList) but that gives the error that String cannot be applied to model.

EDIT: Added classes

To clarify and make things more clear as to what I'm working with. I'm implementing a "wizard" using WizarDroid Library and the page I'm trying to merge two different ArrayList's on is based off of this multi choice tutorial here.

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>();

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.

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());

                }
like image 411
Steve C. Avatar asked Oct 30 '22 03:10

Steve C.


1 Answers

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.

like image 168
xro7 Avatar answered Nov 14 '22 06:11

xro7