1

Im making workout log and i want that ther user will be able to add new exercise types.

I made string array in the resources and a spinner that will take the strings from the string array in the resources.

Im not sure how to add new string to resources string array from java code. I tried this code, but i get exception.

thanks for helping :)

public void onClick(View v) {

    switch(v.getId()){

    case R.id.setNewExercise:
        String[] exerciseStringArray = getResources().getStringArray(R.array.exerciseTypes);
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(this, R.array.exerciseTypes, android.R.layout.simple_list_item_1);

        String exerciseCheck = addNewExercise.getText().toString();
        try{
        for (int i = 0; i < exerciseStringArray.length; i++)
        {
            if (exerciseCheck.equals(exerciseStringArray[i]))
            {
                Toast.makeText(getApplicationContext(), "This exercise already exist", Toast.LENGTH_LONG).show();
                break;
            }
            else if (i == exerciseStringArray.length - 1 && exerciseCheck.equals(exerciseStringArray[i]) == false)
            {
                list.add(exerciseCheck);
                Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_LONG).show();
                break;
            }
        }
        }catch(Exception e){

            Toast.makeText(getApplicationContext(), "wtf", Toast.LENGTH_LONG).show();
        }

    }

}

Ive tried now this code, and my spinner is loaded empty and i get force closed when trying to save new string.

    /////////////////////Exercise spinner/////////////////////
    // Create an ArrayAdapter using the string array and a default spinner layout
    list = new ArrayList<CharSequence>();
    exerciseAdapter = new ArrayAdapter<CharSequence>(this, R.array.exerciseTypes, list);

    // Specify the layout to use when the list of choices appears
    exerciseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Apply the adapter to the spinner
    workOutChoose.setAdapter(exerciseAdapter);


}

@Override
public void onClick(View v) {

    switch(v.getId()){

    case R.id.setNewExercise:


        String exerciseCheck = addNewExercise.getText().toString();

        try{

            if (exerciseAdapter.getPosition(exerciseCheck) >= 0)
            {
                Toast.makeText(getApplicationContext(), "This exercise already exist", Toast.LENGTH_LONG).show();
                break;
            }
            else 
            {
                exerciseAdapter.add(exerciseCheck);
                exerciseAdapter.notifyDataSetChanged();

                Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_LONG).show();
                break;
            }
        }
        catch(Exception e){

            Toast.makeText(getApplicationContext(), "wtf", Toast.LENGTH_LONG).show();
        }

    }

}

1 Answer 1

1

You cannot modify the resources themselves. You are welcome to convert your String[] into an ArrayList<String> and add in additional strings, obtained from elsewhere (e.g., file, database, SharedPreferences) if you wish.

add() on an ArrayAdapter works if the ArrayAdapter was made from an ArrayList, not a Java array ([]), as a Java array's size cannot be changed at runtime.

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

3 Comments

@tomer: This free excerpt from my book covers the use of ArrayAdapter: commonsware.com/Android/excerpt.pdf
looks nice, thanks. @CommonsWare: I posted new code, and still got error... can you please take a look at him?
@tomer: Look at LogCat and examine the Java stack trace associated with your crash. BTW, you do not need to call notifyDataSetChanged() after add(), as add() does that for you automatically. If you need further assistance, please post a new StackOverflow question with the code, the stack trace, and an indication of what lines of code are referred to in that stack trace.

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.