Friday, August 9, 2013

How to save data in Android app

This post will be about saving data to text file in Android development. Android uses a class called SharedPreferences. Within your activity create an instance of the class

SharedPreferences sp;
TextView tv;
Button b1;
EditText et1;

Then name a string that will be the name of the file.

final static String filename = "MyData";

Within the onCreate bundle method make your declaration.

sp = getSharedPreferences(filename,0);

tv = (TextView)findViewById(R.id.textView1);

b1 = (Button)findViewById(R.id.button1);

et1 = (EditText)findViewById(R.id.edit1);

tv.setText(sp.getString("key","No data yet");


public void onClick() {

String test = et1.getText().toString();

SharedPreferences.Editor editor = sp.edit();

editor.putString("mykey",test);

sp.commit();


}


Check out my new movie, live music and book app for iPhone

No comments:

Post a Comment