0

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();
    }
}
Mitch Davis
  • 173
  • 1
  • 1
  • 10

2 Answers2

0

You are using same SharedPreferences key that means when you add a new url you are overriding already existing url.You need to store urls in a list . For this you can use SharedPreferences.Editor.putStringSet. Example is here Save ArrayList to SharedPreferences.

  private void putSharedPreferences(String url) {
    SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
   SharedPreferences.Editor edit = prefs.edit();
    Set<String> set = prefs.getStringSet("key", null);
    if (set != null) {
        set.add(url);
        edit.putStringSet("key", set);
        edit.apply();
    }
}

private List<String> getListFromSharedPreferences() {
    SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
    Set<String> set = prefs.getStringSet("key", null);
    List<String> urlList = new ArrayList<>();
    if (set != null) {
        urlList = new ArrayList<>(set);
    }
    Log.e("size",urlList.size()+"");
    return urlList;
}
FnR
  • 75
  • 1
  • 5
0

What you need to do is add the previous saved URLs to the new Set before adding to SharedPreference.

private void putSharedPreferences(String url) {
  SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
  SharedPreferences.Editor edit = prefs.edit();
  Set<String> previousSet = prefs.getStringSet("key", null);
  Set<String> set = new HashSet<>();
  if (previousSet != null) {
    set.addAll(previousSet);
  }
  set.add(url);
  edit.putStringSet("key", set);
  edit.apply();
 }
}

The reason behind adding all previous saved data to new HashSet is that modifying the returned instance will not guarantee the data consistency or if its been modified at all. You can see documentation here for more explanation.

nitinkumarp
  • 2,120
  • 1
  • 21
  • 30