0

I have a set which contains a list of strings:

public  Set<String> favs = new HashSet<>();

However when I start another activity 'Favorites' I want to pass this list over to my 'Favorties' class, currently I have:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    } else if (id == R.id.exit_the_app) {
        finish();
        return true;
    } else if (id == R.id.favorites) {
        Intent startfavs = (new Intent(Insulter.this, Favorites.class));
        startActivity(startfavs);
        return true;
        }
return super.onOptionsItemSelected(item);
}

I want to pass this set in 'favorites' and eventually display it in a list view after changing it to a list. What would be the best way of going about doing this?

(Note: My second activity is all set up in the manifold and runs fine)

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
James
  • 129
  • 4
  • 18
  • you will have to use intent and intent extra. I recommend you to study more about them [here](http://www.vogella.com/tutorials/AndroidIntent/article.html). – Rahul Tiwari Oct 02 '15 at 11:05
  • Possible duplicate of [How to use putExtra() and getExtra() for string data](http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data) – Rahul Tiwari Oct 02 '15 at 11:09
  • pass it as serializable in a bundle to the activity – Belvi Nosakhare Oct 02 '15 at 12:56

2 Answers2

1

In your First Activity called Insulter,add this,

Intent startfavs = (new Intent(Insulter.this, Favorites.class));
String[] objects = new String[set.size()];
set.toArray(objects);
final ArrayList<String> list = new ArrayList<String>(Arrays.asList(objects));
startfavs.putStringArrayListExtra("favs",list);
startActivity(startfavs);

In your second Activity called Favorites,to get the param,

ArrayList<String> favs = getIntent().getStringArrayListExtra("favs");
starkshang
  • 8,228
  • 6
  • 41
  • 52
  • So I am passing an array list of strings across, right? However when I add the second piece of code in the second activity getIntent goes red and cannot resolve symbol 'getIntent'. Any ideas? – James Oct 02 '15 at 12:05
  • Sorry,not `getIntent`,is `getIntent()` – starkshang Oct 02 '15 at 12:12
0

It is possible to put extra data in intents before using them to start another activity. Intent class provides putExtra() method to put extra data in it.

putExtra() method has several overloads, because you are asking specifically about passing a Set, this one is probably the most relevant:

public Intent putExtra (String name, Serializable value)

For more detail check this out: putExtra(String name, Serializable value)

Since you are using HashSets, passing the favs object as it is should work fine since HashSet implements Serializable.

So your code would look something like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    } else if (id == R.id.exit_the_app) {
        finish();
        return true;
    } else if (id == R.id.favorites) {
        Intent startfavs = (new Intent(Insulter.this, Favorites.class));
        startfavs.putExtra("favs_set", favs);
        startActivity(startfavs);
        return true;
        }
return super.onOptionsItemSelected(item);
}

Don't forget that you must also retrieve this data, therefore in your Favorites activity, on onCreate() method you retrieve this data by using

public Serializable getSerializableExtra (String name)

The relevant piece of code would look something like this:

getExtras().getSerializableExtra("favs_set");

Max
  • 1
  • 1