Android has several option to store persistent data.Your data storage options are the following:
-
- Shared Preferences
Store private primitive data in key-value pairs.
-
- Internal Storage
Store private data on the device memory.
-
- External Storage
Store public data on the shared external storage.
-
- SQLite Databases
Store structured data in a private database.
-
- Network Connection
Store data on the web with your own network server.
It depends on developer which one is suitable for uer. We only look to shared preferences storage information.
If you want to use any other option then goto
http://developer.android.com/guide/topics/data/data-storage.html
Using Shared Preferences
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
- getSharedPreferences() – Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
- getPreferences() – Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don’t supply a name.
To write values perform the following steps:
-
- Call edit() to get a SharedPreferences.Editor.
Editor editsharedPreferences = sharedPreferences.edit();
-
- Add values with methods such as putBoolean() and putString().
editsharedPreferences.putString(“Your Value”,”Your Value”);
-
- Commit the new values with commit()
editsharedPreferences.commit();
Note: sharedPreferences is an object of class SharedPreferences;
To read values, use SharedPreferences methods
- getBoolean(): sharedPreferences.getBoolean”yourkey”, “defaultValue to return if not exits”);
- getString(): sharedPreferences.getString(“yourkey”, “defaultValue to return if not exits”);
public class MyCustomListView extends ActionBarActivity {
ArrayList array_list = new ArrayList();
MyCustomAdapter myCustomAdapter;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mycustomlistview);
sharedPreferences = getSharedPreferences("anyName", Context.MODE_PRIVATE);
// check if shared preferences exists
if(sharedPreferences.contains("Book_Name") && sharedPreferences.contains("Book_Author")) {
Bundle dataBundle = new Bundle();
dataBundle.putString("book_Name",sharedPreferences.getString("Book_Name", "") );
dataBundle.putString("book_Author", sharedPreferences.getString("Book_Author", ""));
Intent intent = new Intent(MyCustomListView.this,Test_activity.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
....
....
....
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View view, int arg2,long arg3) {
// select your item and store in shared preferences
Editor editsharedPreferences = sharedPreferences.edit();
// find text value of selected item
String book_name = (String) ((TextView) view.findViewById(R.id.bookName)).getText();
String book_author =(String) ((TextView) view.findViewById(R.id.authorName)).getText();
// edit shared preferences
editsharedPreferences.putString("Book_Name",book_name);
editsharedPreferences.putString("Book_Author",book_author);
//commit shared preferences
editsharedPreferences.commit();
....
....
....
}
});
listview.setAdapter(myCustomAdapter);
registerForContextMenu(listview);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
....
....
....
}
@Override
public boolean onContextItemSelected(MenuItem item) {
....
....
....
}
}
Best Open Source Business Intelligence Software Helical Insight is Here