I currently have a custom listview with two rows of text in each item, one for the current time and one for the text the user inputs. I do this through creating a new hashmap and adding to the <ArrayList<HashMap<String,String>> that the listview uses. I would like to save my data into the sharedPreferences but I only seem to be getting the last input from the user. My question is: is there anyway to extract the data from a listview then add it into the sharedpreferences? Or add the data in the arraylist into the sharedpreferences?
Here is my code below:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_feed);
//create button and implement on click listener
sendButton = this.findViewById(R.id.sendPostButton);
sendButton.setOnClickListener(this);
//create text field and add text change listener
postTextField = (EditText)findViewById(R.id.postTextField);
postTextField.addTextChangedListener(TextEditorWatcher);
//create text views for seeing the posts and character count
currentTimeTextView = (TextView)findViewById(R.id.postTimeTextView);
mainPostTextView = (TextView)findViewById(R.id.postTextView);
characterCountView = (TextView)findViewById(R.id.charsleft);
characterCountView.setText("150 chars left");
//text view for event name and set the text
feedName = (TextView)findViewById(R.id.nameOfFeed);
currentFeedName = CreateFeedActivity.eventFeedName;
feedName.setText(currentFeedName);
list = new ArrayList<HashMap<String,String>>();
//create the adapter for the list view
adapter = new SimpleAdapter(
this,
list,
R.layout.post_layout,
new String[]{"time", "post"},
new int[]{R.id.postTimeTextView, R.id.postTextView});
//set list adapter
setListAdapter(adapter);
//place the current feed number into a variable here
currentFeedCount = CreateFeedActivity.feedCount;
//create the hashmap for the list view
feedPostMap = new HashMap<String, String>();
//place the stored data into the view again if activity has already been created
if (LiveFeedrHomeActivity.feedOccurs == 1){
Log.d(TAG, "in feed occurs is 1");
//get the shared pref
sharedPref = getSharedPreferences(MY_FEED, 0);
Map<String, ?> map = new HashMap<String, String>();
map = sharedPref.getAll();
//convert from the map to the hashmap
feedPostMap = (HashMap<String, String>) map;
//add to the list
list.add(feedPostMap);
//refresh the adapter
adapter.notifyDataSetChanged();
Log.d(TAG, "feedmap get all");
}
//make variable feed = 1 so that you can't create another feed
LiveFeedrHomeActivity.feedOccurs = 1;
}
@Override
public void onClick(View button) {
switch(button.getId()){
case R.id.sendPostButton:
//create date
date = new Date();
//get current time
currentTime = new SimpleDateFormat("h:mm aaa").format(date);
SendToDatabase();
DisplayUserInput();
break;
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "on stopp'd");
//get shared pref settings
sharedPref = getSharedPreferences(MY_FEED, 0);
sharedPrefEditor = sharedPref.edit();
//get the items in the hash map and add it to the
//shared preferences
for (String string : feedPostMap.keySet()){
sharedPrefEditor.putString(string, feedPostMap.get(string));
// }
sharedPrefEditor.commit();
}
I think your problem is, this for loop,
so try,
Try this, for set of string to edit in sharedpreference,
Set a set of String values in the preferences editor, to be written back once commit() is called.
for more info look at Android-SharedPreferences