问题
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
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
}
Here intent expects to get StringArray in the function getIntent.getStringArrayExtra(...), but I have sent Twitter Status from my fragment.
回答1:
Because the twitter4j.Status class implements Serializable, you should be able to create a Serializable wrapper class and send that through the Intent Extras.
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);
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;
}
}
来源:https://stackoverflow.com/questions/31421465/how-to-send-non-string-data-across-activity