Android has several option to store persistent data.Your data storage options are the following:

  1. Shared Preferences
  2. Store private primitive data in key-value pairs.

  3. Internal Storage
  4. Store private data on the device memory.

  5. External Storage
  6. Store public data on the shared external storage.

  7. SQLite Databases
  8. Store structured data in a private database.

  9. Network Connection
  10. 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).

  1. getSharedPreferences() – Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
  2. 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:

  1. Call edit() to get a SharedPreferences.Editor.
  2. Editor editsharedPreferences = sharedPreferences.edit();

  3. Add values with methods such as putBoolean() and putString().
  4. editsharedPreferences.putString(“Your Value”,”Your Value”);

  5. Commit the new values with commit()
  6. editsharedPreferences.commit();

Note: sharedPreferences is an object of class SharedPreferences;

To read values, use SharedPreferences methods

  1. getBoolean(): sharedPreferences.getBoolean”yourkey”, “defaultValue to return if not exits”);
  2. 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) {
	    ....
            ....
            ....
	}
	
}

Leave a Reply