In my app, I need the values to be saved in to Sharedpreferences file RKs_Data without overwriting the existing data. Every time, I click \'Yes\' in my app, I require all the va
Finally my code looks like this: Sharing the final code as it can be useful to others who are newbies like me :)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rks_contactslist_main);
ListView listview = getListView();
sp = getSharedPreferences("PACKAGE", Context.MODE_PRIVATE);
String str = sp.getString("FAV_CONTACS",
"NO fav contacts are saved as of now");
---------
protected void onListItemClick(ListView listview, View view, int position,
long id) {
// TODO Auto-generated method stub
super.onListItemClick(listview, view, position, id);
ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
showCallDialog(bean.getName(), bean.getPhoneNo());
}
---------
public void onClick(DialogInterface dialog, int which) {
Fav_Contacts_file = getFilesDir();
if (count <5) {
SharedPreferences.Editor editor = sp.edit();
String new_contact = name + " " + phoneNo;
String existing_contact = sp.getString("CONTACTS", "");
/*String existing_phone = sp.getString("phoneNo", "");
String existing_contact = existing_name + " " +existing_phone ;*/
String latestfavContacts = append(existing_contact, new_contact);
editor.putString("CONTACTS", latestfavContacts);
editor.commit();
count++;
Toast.makeText(
getApplicationContext(),
"The data saved successfully to ........ : "
+ Fav_Contacts_file + "/PACKAGE",
Toast.LENGTH_SHORT).show();
Toast.makeText(
getApplicationContext(),
"Name : " + name + " and Phone : "
+ phoneNo, Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(
getApplicationContext(),
"More than 5 Fav Contacts are NOT allowed",
Toast.LENGTH_SHORT).show();
}
}
});
alert.show();
}
protected String append(String existing_contact, String new_contact) {
String latestfavContacts = existing_contact + " | "+ new_contact ;
return latestfavContacts;
}
and the data stored in SharedPreference file 'PACAKAGE' looks like this:
-
I'm yet to work on the formatting and present it to the UI friendly mode as per my application needs.