[英]What is the best way to pass a set to another activity. Java Android SDK
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) (注意:我的第二项活动都在歧管中设置并且运行良好)
In your First Activity called Insulter,add this, 在您的第一个活动Insulter中,添加此内容,
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");
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.
Intent类提供了putExtra()方法以在其中添加额外的数据。
putExtra() method has several overloads, because you are asking specifically about passing a Set, this one is probably the most relevant: putExtra()方法有多个重载,因为您专门询问传递Set的问题,所以这个可能是最相关的:
public Intent putExtra (String name, Serializable value)
For more detail check this out: putExtra(String name, Serializable value) 有关更多详细信息,请查看: putExtra(字符串名称,可序列化的值)
Since you are using HashSets, passing the favs
object as it is should work fine since HashSet implements Serializable. 由于您使用的是HashSet ,因此,由于HashSet实现了Serializable,因此按原样传递
favs
对象应该可以正常工作。
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 不要忘记,您还必须检索此数据,因此,在“
Favorites
活动中的onCreate()
方法中,您可以使用
public Serializable getSerializableExtra (String name)
The relevant piece of code would look something like this: 相关的代码如下所示:
getExtras().getSerializableExtra("favs_set");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.