Search code examples
androidstringsettext

How would i store a number/string into R.string.xx?


with this code, my program just force close(error)

***public View x = findViewById(R.string.nfoname);***
@Override
public void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);    
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***

//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);

//clear button
clear.setOnClickListener(new View.OnClickListener() {       
    public void onClick(View v) {
        // TODO Auto-generated method stub
        infoname.setText("");
    }
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {     
    public void onClick(View v) {

        ***x=(View) infoname.getText();***
    }
});
}

the one with the * are the source of error

program function: if the user clicks confirm, his name will be set to R.string.nfoname which will then be used in another layout through TextView x = setText(R.string.nfoname);


Solution

  • I am not sure that you can save text to the R.string. This is a generated class that the compiler creates for you. It gets packaged with your apk. Think of the resources as a means of translation and to present text to the screen.

    I think what you would want to do is save the user input as a SharedPreference or in a database.

    See:SharedPreferences on the android docs for an example usage.