繁体   English   中英

Android意向捆绑包传递字符串

[英]Android intent bundle pass string

我将json转换为可序列化的claas,然后用它来填充listview,并且一切正常,但是我想使用可序列化的对象之一在另一个xml页面上填充textview,我读到的最佳方法是捆绑包意图,但我做到了编码错误。

public class FeedItem implements Serializable {

    private String title;
    private String date;
    private String attachmentUrl;
    private String id;
    private String content;
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getAttachmentUrl() {
        return attachmentUrl;
    }

    public void setAttachmentUrl(String attachmentUrl) {
        this.attachmentUrl = attachmentUrl;
    }




    @Override
    public String toString() {
        return "[ title=" + title + ", date=" + date + "]";
    }

    ArrayList<FeedItem> all_thumbs = new ArrayList<FeedItem>();
    all_thumbs.add(new FeedItem(title);
    Intent intent = new Intent(title);

    Bundle extras = new Bundle();

    extras.putSerializable("title",title);
    intent.putExtras(extras);

}

在我想使用它的课堂上

public void updateList() {

    TextView infoz = (TextView) getView().findViewById(R.id.infoz);
    Bundle args;
    getArguments().getSerializable(title);

而是使用:

public void updateList() {

    TextView infoz = (TextView) getView().findViewById(R.id.infoz);
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String title = (String) extras.getSerializable("title");

如果要传递任何原始数据类型的值(String,int,long,float等),则无需使用Bundle.putExtra(KEY,Serialized Object)和Bundle.getSerializable(KEY)。 您可以使用Bundle.putExtra(KEY,primitive data);

如果要将类对象传递给intent / bundle,则需要在该类中实现Serializable / Parsable,例如:

public class Test implements Serializable
{
   private static final long serialVersionUID = 1L;
   int test1;
} 

然后可以将该对象实例传递给意图,例如:

myIntent.putExtra( KEY, new Test() );

有关更多说明,请检查意图示例中的可传递对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM