简体   繁体   中英

Android intent bundle pass string

i have json to serializable claas then i use it to populate listview, and everything works fine but i want to populate textview on another xml page with one of the serializable object and i read to best way to do it is bundle intent, but i did code something wrong.

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);

}

and in the class where i want to use it

public void updateList() {

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

Instead use:

public void updateList() {

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

IF you want to pass any primitive datatyped values (String,int,long,float etc.) then do not need to use Bundle.putExtra(KEY,Serialized Object) and Bundle.getSerializable(KEY). You can use Bundle.putExtra(KEY,primitive data);

If you want to pass class object to intent/bundle then you need to implement Serializable/Parsable in that class like:

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

and then you can pass that object instance to intent like:

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

For more clarification check pass seriable object in intent example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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