如何在活动发送非字符串数据 [英] How to send non-string data across activity
问题描述
在twitterfragment I类有
In twitterfragment class I have
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片段发送的雕像。
In twitter_timeline class I want to get the statues I sent from 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
}
下面的意图期望得到字符串数组中的函数getIntent.getStringArrayExtra(...),但我已经发微博状态从我的片段。
Here intent expects to get StringArray in the function getIntent.getStringArrayExtra(...), but I have sent Twitter Status from my fragment.
推荐答案
由于 twitter4j.Status 类实现Serializable,你应该能够创建一个序列化包装类和发送通过意向其他。
Because the twitter4j.Status class implements Serializable, you should be able to create a Serializable wrapper class and send that through the Intent Extras.
在MyStatuses.java创建MyStatuses类:
Create a MyStatuses class in MyStatuses.java:
import java.io.Serializable;
public class MyStatuses implements Serializable {
List<twitter4j.Status> statuses;
}
然后将包装类的一个实例,在意图附加:
Then send an instance of the wrapper class in the 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(),以获得额外的意图:
Then use getSerializable() in order to get the 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;
}
}
这篇关于如何在活动发送非字符串数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!