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