简体   繁体   English

将集合传递给另一个活动的最佳方法是什么? Java Android SDK

[英]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.

相关问题 将 Drawable 从一个活动传递到另一个活动的最佳方法是什么? - What is the best way to pass a Drawable from one activity to another? 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 在Java中的url中设置标头的最佳方法是什么 - what is the best way to set headers in url in Java Android-如何更新其他活动的数据。 在您正在从事的工作之外 - Android- How to update data on another activity. Outside the one you're working on 访问已完成活动的静态变量。(Android) - Accessing static variable of a finished activity.(Android) 将容器和操作栏添加到Android活动。 - Adding containers and action bar to an Android Activity. Android 中的 Fragment 和 Activity 之间通信的最佳方式是什么? - What is the best way of communicating between a Fragment and an Activity in Android? 我想在Android活动中制作一个带有图像的可滚动文本列表。 我应该使用什么? - I want to make a scrollable list of text with images in an Android Activity. What should I use? Java和Android:在活动中创建可动态扩展的EditText列表的最佳方法? - Java & Android: Best way to create a dynamically expandable list of EditTexts in an activity? 将列表传递给另一个类进行迭代的最佳方法是什么 - What is the best way to pass list to another class for iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM