1

I want to know what's the best way to save data in android.

I am developing an application whose basic operation is simple, the user can fill in fields of a form, once you have done this data is saved on the device (smartphone or tablet).

When the application is closed and then reopened, the user can view and edit what has been entered in the previous form.

(I would like to avoid saving the data on the network.)

I would like to save the data directly to the memory of the device, the data structure that would make me more comfortable is a table (database style), how can I do?

  • have you tried [the Android Developer website?](http://developer.android.com/training/basics/data-storage/index.html) – panini Jul 29 '14 at 22:39
  • If you don't want to use SharedPreferences, see http://stackoverflow.com/questions/2729438/how-do-i-create-a-database-in-android to learn on how to create a DataBase in Android – joao2fast4u Jul 29 '14 at 22:39

2 Answers2

2

You are looking for SavedPreferences which is easy to use and easy to get the data, it is like HashMap with keys and value.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
1

If you just want to save the text that user types in an edit text, then SharedPreferences is the best option.

private Button bsave;
private EditText detail;
private SharedPreferences preferences;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enteractivity);
        preferences = getSharedPreferences("name", MODE_PRIVATE);
        detail = (EditText) findViewById(R.id.etdetails);
        bsave = (Button) findViewById(R.id.buttonsave);
        detail.setText(preferences.getString("key", "defValue"));
        bsave.setOnClickListener(new View.OnClickListener() {



            @Override
                public void onClick(View v) {
     SharedPreferences.Editor editor = preferences.edit();
     editor.putString("key", detail.getText().toString());
     editor.commit();
        }
});


}

I don't know why you need a table structure, if you need it you will have to create a sqlite database. There are lot of tutorials available for it. http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Jossy Paul
  • 1,267
  • 14
  • 26
  • Could i save different "SharedPreferences" ? For example, if i want create a simple address book, using this method can i save more contacts or i must use sql ? –  Jul 30 '14 at 08:31
  • 1
    If u are trying to create an address book you should use a database. – Jossy Paul Jul 30 '14 at 08:35
  • I have a little concerns regarding sqlite, at the time of the creation of the database, it will be saved in the smartphone's internal or external memory? –  Jul 30 '14 at 20:51
  • if you are creating database using SQLiteOpenHelper, then database will be stored in your internal memory. [link](http://stackoverflow.com/questions/9925492/sqlite-database-storage) – Jossy Paul Jul 31 '14 at 04:39