I'm having issues where I have a button that is essentially a "favorites" button that saves a URL from a webview sesson and adds it to a listview in another class called favorites. It is doing this through sharedpreferences due to a lack of finding a better way to do it. The issue I'm having is when I try to save another URL it just overwrites the URL that is currently there and doesn't add another row. My code for webview that has the button is:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_share) {
shareURL();
Toast.makeText(this, getString(R.string.share),
Toast.LENGTH_SHORT).show();
}
if (id == R.id.action_favorite) {
changeIcon();
Snackbar.make(findViewById(android.R.id.content), "Saved for later", Snackbar.LENGTH_LONG).show();
String url = getIntent().getDataString();
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("URL", url );
edit.apply();
}
return super.onOptionsItemSelected(item);
}
// code for my "favorites" class is below:
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_list);
ListView listView = (ListView) findViewById(R.id.favoritesList);
adapter = new ArrayAdapter<String>(this,
R.layout.custom_lv,
listItems);
listView.setAdapter(adapter);
SharedPreferences bb = getSharedPreferences("my_prefs", 0);
String m = bb.getString("URL", "");
listItems.add(m);
adapter.notifyDataSetChanged();
}
Any help would be greatly appreciated!
--- EDIT --
With FnR's code it's still only adding the latest "saved" url in favorites list view, updated code is below:
--Favorites code--
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_list);
ListView listView = (ListView) findViewById(R.id.favoritesList);
adapter = new ArrayAdapter<String>(this,
R.layout.custom_lv,
listItems);
listView.setAdapter(adapter);
getListFromSharedPreferences();
}
private List<String> getListFromSharedPreferences() {
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
Set<String> set = prefs.getStringSet("URL", null);
List<String> urlList = new ArrayList<>();
if (set != null) {
urlList = new ArrayList<>(set);
listItems.add(String.valueOf(set));
adapter.notifyDataSetChanged();
}
Log.e("size",urlList.size()+"");
return urlList;
}
-- Webview code --
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_share) {
shareURL();
Toast.makeText(this, getString(R.string.share),
Toast.LENGTH_SHORT).show();
}
if (id == R.id.action_favorite) {
changeIcon();
Snackbar.make(findViewById(android.R.id.content), "Saved for later", Snackbar.LENGTH_LONG).show();
String url = getIntent().getDataString();
putSharedPreferences(url);
}
return super.onOptionsItemSelected(item);
}
private void putSharedPreferences(String url) {
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
Set<String> set = prefs.getStringSet("URL", null);
if (set != null) {
set.add(url);
edit.putStringSet("URL", set);
edit.apply();
}
}