0

I have the following code that is saving a list of variables named savedlist, to sharedprefs. I have been trying to get the values saved (which should be a list of URLs) to display in a ListView (lv).

public class saveSearch extends Activity {

    private String urlString = "www.google.com";
    private String saveURL;
    private final static String PREFERENCE_FILENAME = "TestSave";
    private EditText savename;
    private ArrayList<String> savelist;
    private ArrayList savelist2;
    List<String> SavedSearches = new ArrayList<String>();
    private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String value = intent.getStringExtra("searchurl");
    urlString = value;
    setContentView(R.layout.save_search);
    final SharedPreferences.Editor editor = getSharedPreferences(PREFERENCE_FILENAME,0).edit();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    savename = (EditText) findViewById(R.id.et_savename);
    lv = (ListView) findViewById(R.id.listView);
    // Get the current list.
    final Set<String> myStrings = prefs.getStringSet("savelist", new HashSet<String>());
    //Convert Your Set to List again
    final ArrayList<String> savelist = new ArrayList<String>(myStrings);
    // populate the listview
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            savelist);
    lv.setAdapter(arrayAdapter);
    final Button browsebutton = (Button) findViewById(R.id.button_Save);
    browsebutton.setOnClickListener(
      new View.OnClickListener() {
        public void onClick(View v) {
        // Perform action on click
        saveURL = (savename.getText() + " - " + urlString);
        // Add the new value.
        myStrings.add(saveURL);
        // Save the list. 
        editor.putStringSet("savelist", myStrings);
        editor.commit();
        arrayAdapter.notifyDataSetChanged();
        lv.setAdapter(arrayAdapter);
      }})
gvlasov
  • 18,638
  • 21
  • 74
  • 110
Faust
  • 97
  • 1
  • 1
  • 4

1 Answers1

0

There are some problems here. Let's go one by one:

  1. Since You check intent at the beginning i assume You launch this activity with properly filled intent. Hard to say, since You dont seem check for null nowhere. Let's assume it's ok.
  2. Next item is shared preferences which You used incorrectly here, see this for nice explanation why. It should hopefully answer Your quesion why this does not work - you used two different shared preferences.
  3. A few other ideas (not quite related to Your question, but perhaps they might help You
    1. What is the reason You use HashSet and convert it back to ArrayList? Why not use ArrayList to begin with? Unless of course You want to remove duplicates, then sure.
    2. Last thing is onClick method. Calling lv.setAdapter(arrayAdapter); is unnecessary since You had already set it in onCreate.
Community
  • 1
  • 1
brainovergrow
  • 458
  • 4
  • 13
  • Thanks, you were right on with the different shared pref calls, that was my issue. As for why I used the HashSet, I'll have to admit I was having trouble and used some examples I found online, could you show me a better way to accomplish this – Faust Feb 18 '15 at 22:38
  • Sure I can. But discussion in comments is rather discouraged on SO, so I'd rather like to avoid that. If You like, You might close this question - if it helped, and post another - preferably with description of Your goals (what do You want to archive?). Send me a msg if You do so, so I could take a look :) (wth with my usage of "so" here :)) – brainovergrow Feb 22 '15 at 23:19