gpt4 book ai didi

java - 如何跨 Activity 发送非字符串数据

转载 作者:行者123 更新时间:2023-11-30 02:01:20 25 4
gpt4 key购买 nike

在 twitterfragment 类中我有

List<twitter4j.Status> statuses = twitter.getUserTimeline(user);
Intent intent = new Intent(getActivity(), twitter_timeline.class);
intent.putExtra(twitter_timeline.STATUS_LIST, statuses);// this line giving error if I pass status

在 twitter_timeline 类中,我想获取我从 twitter fragment 发送的雕像。

public class twitter_timeline extends Activity {
public static List<twitter4j.Status> STATUS_LIST;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_timeline);

setTitle("Timeline");

List<twitter4j.Status> statuses = (Status) this.getIntent().getStringArrayExtra(STATUS_LIST); // this line not resolving even if I cast it to status type
}

这里的 Intent 希望在函数 getIntent.getStringArrayExtra(...) 中获取 StringArray,但我已经从我的 fragment 中发送了 Twitter 状态。

最佳答案

因为 twitter4j.Status类实现 Serializable,您应该能够创建一个 Serializable 包装类并通过 Intent Extras 发送它。

在 MyStatuses.java 中创建一个 MyStatuses 类:

import java.io.Serializable;

public class MyStatuses implements Serializable {
List<twitter4j.Status> statuses;
}

然后在 Intent Extras 中发送一个包装类的实例:

    List<twitter4j.Status> statuses = twitter.getUserTimeline(user);
MyStatuses myStatuses = new MyStatuses();
myStatuses.statuses = statuses;
Intent intent = new Intent(getActivity(), twitter_timeline.class);
intent.putExtra("statuses", myStatuses);

然后使用 getSerializable() 来获取 Intent Extra:

public class twitter_timeline extends Activity {
//public static List<twitter4j.Status> STATUS_LIST;
List<twitter4j.Status> statuses;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_timeline);

setTitle("Timeline");
Bundle b = this.getIntent().getExtras();
if (b != null) {
MyStatuses myStatuses = (MyStatuses) b.getSerializable("statuses");
statuses = myStatuses.statuses;
}
}

关于java - 如何跨 Activity 发送非字符串数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com