0

So I'm doing this:

        int len = lv.getCount();

    List<String> Cool = null;
    SparseBooleanArray checked = lv.getCheckedItemPositions();
    for (int i = 0; i < len; i++)
        if (checked.get(i)) {
            String item = String.valueOf(names.get(i));
            int start = item.lastIndexOf('=') + 1;
            int end = item.lastIndexOf('}');
            String TEST = item.substring(start, end);

            Log.d("Log", TEST);

            Cool = new ArrayList<String>();

            Cool.add(TEST);

        }


            String NEW = StringUtils.join(Cool, ',');

            Log.d("Log", NEW);

Which evey time replaces the thing in the list with whatever the next item is. How do i make it put the strings after each other.

Thanks for the help.

3 Answers 3

2
List<String> Cool = new ArrayList<String>();

create the list at the top

Cool = new ArrayList<String>();

and delete this line because it will always create a new list what you dont want

Sign up to request clarification or add additional context in comments.

Comments

1

You're constructing a new ArrayList in every iteration of your for loop

 Log.d("Log", TEST);
 Cool = new ArrayList<String>(); // NOT HERE!!!!
 Cool.add(TEST);

construct it once, outside the loop

List<String> Cool = new ArrayList<String>(); // also Cool should be cool.

Comments

0

the reason it keeps resetting the list is because you initialized the List in a loop. Initialize it outside the loop and the algorithm will work.

Initialization:

Cool = new ArrayList();

Corrected code:

int len = lv.getCount();

List<String> Cool = new ArrayList<String>();
SparseBooleanArray checked = lv.getCheckedItemPositions();
for (int i = 0; i < len; i++)
    if (checked.get(i)) {
        String item = String.valueOf(names.get(i));
        int start = item.lastIndexOf('=') + 1;
        int end = item.lastIndexOf('}');
        String TEST = item.substring(start, end);

        Log.d("Log", TEST);

        Cool.add(TEST);

    }


        String NEW = StringUtils.join(Cool, ',');

        Log.d("Log", NEW);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.