Java 将带有自定义对象的列表传递给另一个活动
我有一个Java 将带有自定义对象的列表传递给另一个活动,java,android,listview,Java,Android,Listview,我有一个列表new ArrayList()我需要将此列表传递给另一个活动,并从中检索对象以将其分配给字符串值 java public class News { String title; String description; String thumbnail; String newsUrl; String body; String newsBigImage ; String newsComments ; String ne
列表new ArrayList()代码>我需要将此列表传递给另一个活动,并从中检索对象以将其分配给字符串值
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;
}
}
我传递的值如下:-
myNewsList.add(new News(title, description, thumbnail, newsUrl, body, newsBigImage, newsComments, newsViews, publishedDate, articleGuid, newsSourceId, newsId, publisherName, newsSourceTitle));
然后我将此列表传递给ListAdapter以在ListView中显示它
itemsAdapter = new LazyAdapter(myContext, myNewsList);
newsList.setAdapter(itemsAdapter);
现在,当用户单击listview项目时,我想将myNewsList
传递给新活动,从中检索项目并将其分配给该类中的另一个字符串
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);
ewsList.setOnItemClickListener(新的OnItemClickListener()
{
@凌驾
公共链接(AdapterView arg0,
视图arg1,内部位置,长arg3)
{
//TODO自动生成的方法存根
myDialog=newProgressDialog(myContext).show(getActivity(),“获取新闻…”,请稍等);
//News myMap=myNewsList.get(位置);
Intent newsIntent=新的Intent(getActivity(),NewsDetails.class);
startActivity(newsIntent);
如何实现这一点呢?使用Seriazable
或parsable
接口实现来实现这一点。使用Seriazable
或parsable
接口实现来实现这一点。在NewsDetails类中,您只是想显示描述,所以调用模型类的getters方法并在那里显示,或者按照上述解决方案在NewsDetails类中,您只想显示描述,因此调用模型类的getters方法并在那里显示,或者按照上述解决方案实现一个可打包的接口,使您的类看起来像
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];
}
};
用于传递arraylist doIntent.putParcelableArrayListExtra(“新闻列表”,arr);
在接下来的活动中
News news = (News)intent.getParcelableExtra("newsObject");
为了让arraylist做些什么
ArrayList<News> news = (ArrayList<News>)intent.getParcelableArrayListExtra("newsList");
arraylistnews=(ArrayList)intent.getParcelableArrayListExtra(“新闻列表”);
实现了一个可打包的接口,因此您的类看起来像
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];
}
};
用于传递arraylist doIntent.putParcelableArrayListExtra(“新闻列表”,arr);
在接下来的活动中
News news = (News)intent.getParcelableExtra("newsObject");
为了让arraylist做些什么
ArrayList<News> news = (ArrayList<News>)intent.getParcelableArrayListExtra("newsList");
arraylistnews=(ArrayList)intent.getParcelableArrayListExtra(“新闻列表”);
首先,创建一个getter方法以在适配器中检索对象。返回列表,然后在活动中调用getter,然后创建意图。首先,创建一个getter方法以在适配器中检索对象。返回列表,然后在活动中调用getter,然后创建意图。k如果你认为这应该是评论,我会稍后再跟你说如果你认为这应该是评论什么“以上解决方案”?你仍然停留在那里…我的意思是遵循@Mick在这里发布的内容什么“以上解决方案”?你仍然停留在那里…我的意思是遵循@Mick在这里发布的内容。当我检索到其他活动中的值时,不知何故,我在其他活动中得到了一个空值。我不能直接传递列表吗?你通过调用.getIntent()获得了意图吗是的,我做到了。我如何直接传递列表?您将以对象的形式传递列表,但在获取它时,请调用getParcelableArrayListExtra。(听起来是这样的。请参见intellisence中的内容)我在检索其他活动中的值时,不知怎的获得了null。我不能直接传递列表吗?您是否通过调用.getIntent()获得了意图是的,是我做的。我如何直接传递列表?您将以对象的形式传递列表,但在获取它时,请调用getParcelableArrayListExtra。(听起来像这样。请参见intellisence中的)