Android 向资源字符串数组动态添加新字符串

Android 向资源字符串数组动态添加新字符串,android,string,android-arrayadapter,arrays,Android,String,Android Arrayadapter,Arrays,我正在制作锻炼日志,我希望其他用户能够添加新的锻炼类型 我在参考资料中创建了字符串数组,并使用微调器从参考资料中的字符串数组中提取字符串 我不知道如何从java代码向资源字符串数组添加新字符串。 我尝试了这个代码,但我得到了异常 谢谢你的帮助:) public void onClick(视图v){ 开关(v.getId()){ 案例R.id.setNewExercise: 字符串[]exerciseStringArray=getResources().getStringArray(R.array

我正在制作锻炼日志,我希望其他用户能够添加新的锻炼类型

我在参考资料中创建了字符串数组,并使用微调器从参考资料中的字符串数组中提取字符串

我不知道如何从java代码向资源字符串数组添加新字符串。 我尝试了这个代码,但我得到了异常

谢谢你的帮助:)

public void onClick(视图v){
开关(v.getId()){
案例R.id.setNewExercise:
字符串[]exerciseStringArray=getResources().getStringArray(R.array.exerciseTypes);
ArrayAdapter list=ArrayAdapter.createFromResource(这个,R.array.exerciseTypes,android.R.layout.simple\u list\u item\u 1);
String exerciseCheck=addNewExercise.getText().toString();
试一试{
for(int i=0;i
我现在尝试了这段代码,我的微调器加载为空,当我试图保存新字符串时,强制关闭

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

    }

}
//练习微调器/////////////////////
//使用字符串数组和默认微调器布局创建ArrayAdapter
列表=新的ArrayList();
exerciseAdapter=newarrayadapter(this,R.array.exerciseTypes,list);
//指定显示选项列表时要使用的布局
exerciseAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
//将适配器应用于微调器
workOutChoose.setAdapter(exerciseAdapter);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.setNewExercise:
String exerciseCheck=addNewExercise.getText().toString();
试一试{
if(exerciseAdapter.getPosition(exerciseCheck)>=0)
{
Toast.makeText(getApplicationContext(),“此练习已存在”,Toast.LENGTH_LONG.show();
打破
}
其他的
{
exerciseAdapter.add(exerciseCheck);
exerciseAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),“添加成功”,Toast.LENGTH_LONG.show();
打破
}
}
捕获(例外e){
Toast.makeText(getApplicationContext(),“wtf”,Toast.LENGTH_LONG.show();
}
}
}

您不能修改资源本身。如果您愿意,欢迎将您的
字符串[]
转换为
数组列表
,并添加从其他地方(例如,文件、数据库、
共享引用
)获取的其他字符串


如果
ArrayAdapter
是由
ArrayList
而不是Java数组(
[]
)生成的,则
ArrayAdapter
上的
add()
可以工作,因为Java数组的大小在运行时无法更改。

您不能修改资源本身。如果您愿意,欢迎将您的
字符串[]
转换为
数组列表
,并添加从其他地方(例如,文件、数据库、
共享引用
)获取的其他字符串


如果
ArrayAdapter
是由
ArrayList
而不是Java数组(
[]
)制作的,那么
ArrayAdapter
上的
add()
就可以工作,因为Java数组的大小在运行时无法更改。

@tomer:这本书的免费摘录介绍了
ArrayAdapter
的使用:看起来不错,谢谢@Commonware:我发布了新代码,但仍然出现错误。。。你能看看他吗?@tomer:看看LogCat,检查与你的崩溃相关的Java堆栈跟踪。顺便说一句,您不需要在
add()
之后调用
notifyDataSetChanged()
,因为
add()
会自动为您调用。如果您需要进一步的帮助,请发布一个新的StackOverflow问题,其中包括代码、堆栈跟踪以及该堆栈跟踪中引用的代码行的指示。@tomer:这本书的免费摘录介绍了
ArrayAdapter
:看起来不错,谢谢@Commonware:我发布了新代码,但仍然出现错误。。。你能看看他吗?@tomer:看看LogCat,检查与你的崩溃相关的Java堆栈跟踪。顺便说一句,您不需要在
add()
之后调用
notifyDataSetChanged()
,因为
add()
会自动为您调用。如果您需要进一步的帮助,请发布一个新的StackOverflow问题,其中包含代码、堆栈跟踪以及该堆栈跟踪中引用的代码行的指示。
    /////////////////////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();
        }

    }

}