且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过对象从一个活动到另一个***的方法

更新时间:2023-11-27 12:58:22

 意向意图=新的意图(getBaseContext(),NextActivity.class);
intent.putExtra(数组列表,新的ArrayList<字符串>());
 

如果您ArrayList中包含您自己创建,例如Friend.class另一个对象,你可以实现Friend.class与序列化,然后:

 意向意图=新的意图(getBaseContext(),NextActivity.class);
intent.putExtra(好友列表,新的ArrayList&其中;朋友>());
 

和接受它NextActivity.class:

 捆绑额外= getIntent()getExtras()。
    如果(临时演员!= NULL){
        ArrayList的<朋友>朋友= extras.getSerializable(好友列表);
    }
 

好,而不是传递一个空的ArrayList,你必须把值到ArrayList中,然后通过它,但你的想法。

I have read about global static technique in which we create a class with static fields which can be accessed in any activity. Is there any other way to pass large data sets like ArrayList or HashMaps ? I have also read about Serializable but have no idea how to use it. Any example code is welcome...

Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("arraylist", new ArrayList<String>());

If your ArrayList contains another Object that you have created yourself, for instance Friend.class, you can implement the Friend.class with Serializable and then:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("friendlist", new ArrayList<Friend>());

And for receiving it on NextActivity.class:

Bundle extras = getIntent().getExtras();
    if(extras != null){
        ArrayList<Friend> friends = extras.getSerializable("friendlist");
    }

Well, instead of passing an empty ArrayList, you'll have to put values into the ArrayList and then pass it, but you get the idea.