0

I have created two EditText objects in the Java code of My android application, as

final EditText et1=(EditText)findViewById(R.id.editText1);
final EditText et2=(EditText)findViewById(R.id.editText2);

then on the onClick() event of a button, called a method with parameters as-

addStringToXmlFile(et1.getText(),et2.getText());

now in the definition of this method below, i have written-

private void addStringToXmlFile(Editable editable1,Editable editable2){
        String s1=new String();
        s1=editable1.toString();

        String s2=new String();
        s2=editable2.toString();
}

The problem is that, now i want to use these two String objects s1,s2, to make two entries in the res/values/Strings.xml file of the database, & I don't know how to do it.

please guide me further.

1
  • you can just do String s2 = editable2.toString(); you don't need to initialize the string before assigning it Commented Mar 12, 2012 at 21:39

2 Answers 2

1

This is not possible - an app's apk (including it's resources) can not be changed at runtime. I'm not entirely sure of all of the reasons why, but one obvious thing I can think of is that R.java needs to contain a reference to your String in order for you to access it, and this file is generated by the compiler when you create the APK.

If you need to persist a String across sessions, you should look into using one of several data storage mechanisms that Android provides, such as SharedPreferences.

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

Comments

0

Take a look into using SharedPreferences to store your string values instead.

//Saving your strings
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("s1", s1);
editor.putString("s2", s2);
editor.commit();

//retrieving your strings from preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
String s1 = prefs.getString("s1", ""); //empty string is the default value
String s2 = prefs.getString("s2", ""); //empty string is the default value

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.