将自定义对象的列表传递给另一个活动android [英] Pass List with custom object to another activity android

查看:103
本文介绍了将自定义对象的列表传递给另一个活动android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有列表< News> new ArrayList< News>(); 我需要将此列表传递给另一个活动并从中检索对象以将其分配给String值。

I have a List<News> new ArrayList<News>(); I need to pass this list to another activity and retrieve object from it to assign it to String values.

News.java

News.java

 public class News 
{


    String title;
    String description;
    String thumbnail;
    String newsUrl;
    String body;
    String newsBigImage ;
    String newsComments ;
    String newsViews;
    String publishedDate;
    String articleGuid;
    String newsSourceId;
    String newsId ;
    String publisherName;
    String newsSourceTitle;
    String color;

    News(String title, String description, String thumbnail, String newsUrl, String body, String newsBigImage,  String newsComments,  String newsViews,
    String publishedDate,
    String articleGuid,
    String newsSourceId,
    String newsId ,
    String publisherName,
    String newsSourceTitle )
    {

        this.title = title;
        this.description = description;
        this.articleGuid =articleGuid;
        this.thumbnail = thumbnail;
        this.newsUrl = newsUrl;
        this.body = body;
        this.newsBigImage = newsBigImage;
        this.newsComments = newsComments;
        this.newsViews = newsViews;
        this.publishedDate = publishedDate;
        this.newsId = newsId;
        this.newsSourceId = newsSourceId;
        this.publisherName = publisherName;
        //this.color = color;
        this.newsSourceTitle  =newsSourceTitle;
    }

    public String getTitle() {
        return title;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    public String getNewsUrl() {
        return newsUrl;
    }

    public void setNewsUrl(String newsUrl) {
        this.newsUrl = newsUrl;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getNewsBigImage() {
        return newsBigImage;
    }

    public void setNewsBigImage(String newsBigImage) {
        this.newsBigImage = newsBigImage;
    }

    public String getNewsComments() {
        return newsComments;
    }

    public void setNewsComments(String newsComments) {
        this.newsComments = newsComments;
    }

    public String getNewsViews() {
        return newsViews;
    }

    public void setNewsViews(String newsViews) {
        this.newsViews = newsViews;
    }

    public String getPublishedDate() {
        return publishedDate;
    }

    public void setPublishedDate(String publishedDate) {
        this.publishedDate = publishedDate;
    }

    public String getArticleGuid() {
        return articleGuid;
    }

    public void setArticleGuid(String articleGuid) {
        this.articleGuid = articleGuid;
    }

    public String getNewsSourceId() {
        return newsSourceId;
    }

    public void setNewsSourceId(String newsSourceId) {
        this.newsSourceId = newsSourceId;
    }

    public String getNewsId() {
        return newsId;
    }

    public void setNewsId(String newsId) {
        this.newsId = newsId;
    }

    public String getPublisherName() {
        return publisherName;
    }

    public void setPublisherName(String publisherName) {
        this.publisherName = publisherName;
    }

    public String getNewsSourceTitle() {
        return newsSourceTitle;
    }

    public void setNewsSourceTitle(String newsSourceTitle) {
        this.newsSourceTitle = newsSourceTitle;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }


}

我传递值喜欢: -

I pass values like:-

myNewsList.add(new News(title, description, thumbnail, newsUrl, body, newsBigImage, newsComments, newsViews, publishedDate, articleGuid, newsSourceId, newsId, publisherName, newsSourceTitle));

然后我将此列表传递给ListAdapter以在ListView中显示它。

Then I pass this list to an ListAdapter to show it in a ListView.

 itemsAdapter = new LazyAdapter(myContext, myNewsList);

        newsList.setAdapter(itemsAdapter);

现在,当用户点击listview项目时,我想传递 myNewsList 到新活动并从中检索项目并将其分配给该类中的另一个字符串。

Now, When the user clicks a listview item, I want to pass the myNewsList to the new activity and retrieve items from it and assign it to another Strings in that class.

ewsList.setOnItemClickListener(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> arg0,
                    View arg1, int position, long arg3) 
            {
                // TODO Auto-generated method stub
                myDialog = new ProgressDialog(myContext).show(getActivity(), "Fetching news..", "Just a moment");



                    //News myMap = myNewsList.get(position);
                    Intent newsIntent = new Intent(getActivity(),NewsDetails.class);

                    startActivity(newsIntent);

我该怎么办?

推荐答案

实现了一个parcelable接口,所以你的类看起来像

implements a parcelable interface so your class will look like

public class News implements Parcelable {


String title;
String description;
String thumbnail;
String newsUrl;
String body;
String newsBigImage ;
String newsComments ;
String newsViews;
String publishedDate;
String articleGuid;
String newsSourceId;
String newsId ;
String publisherName;
String newsSourceTitle;
String color;


protected News(Parcel in) {
    title = in.readString();
    description = in.readString();
    thumbnail = in.readString();
    newsUrl = in.readString();
    body = in.readString();
    newsBigImage = in.readString();
    newsComments = in.readString();
    newsViews = in.readString();
    publishedDate = in.readString();
    articleGuid = in.readString();
    newsSourceId = in.readString();
    newsId = in.readString();
    publisherName = in.readString();
    newsSourceTitle = in.readString();
    color = in.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(title);
    dest.writeString(description);
    dest.writeString(thumbnail);
    dest.writeString(newsUrl);
    dest.writeString(body);
    dest.writeString(newsBigImage);
    dest.writeString(newsComments);
    dest.writeString(newsViews);
    dest.writeString(publishedDate);
    dest.writeString(articleGuid);
    dest.writeString(newsSourceId);
    dest.writeString(newsId);
    dest.writeString(publisherName);
    dest.writeString(newsSourceTitle);
    dest.writeString(color);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<News> CREATOR = new Parcelable.Creator<News>() {
    @Override
    public News createFromParcel(Parcel in) {
        return new News(in);
    }

    @Override
    public News[] newArray(int size) {
        return new News[size];
    }
};

}

你现在可以通过这个意图更像是

and you can pass now this in intent extra like

intent.putExtra("newsObject", obj);

并传递arraylist do Intent.putParcelableArrayListExtra(newsList,arr) ;

and for passing arraylist do Intent.putParcelableArrayListExtra("newsList", arr);

并在下一个活动中获得

News news = (News)intent.getParcelableExtra("newsObject");

以及获取arraylist吗

and for getting arraylist do

ArrayList<News> news = (ArrayList<News>)intent.getParcelableArrayListExtra("newsList"); 

这篇关于将自定义对象的列表传递给另一个活动android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆