2014-04-28 160 views
2

我有一個List<News> new ArrayList<News>();我需要將此列表傳遞給另一個活動,並從中檢索對象以將其分配給字符串值。將自定義對象傳遞給另一個活動android

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


} 

我傳遞值,如: -

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

現在,當用戶點擊列表視圖的項目,我想了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); 

我該怎麼做?

回答

0

使用SeriazableParceable接口實現爲此。

+2

這應該是評論!!! – Piyush

+0

如果你認爲這應該是評論 – Mick

0

在NewsDetails類只是要顯示這樣的模型類的調用的getter方法的描述,並顯示在那裏,或是按照上述方案

+0

什麼「以上解決方案」,我會再談你? – user3534519

+0

仍然你卡在那裏...我的意思是按照什麼@米克在這裏發佈 – GvSharma

3

實現parcelable接口,以便類看起來像

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]; 
    } 
}; 

}

,您可以在意向額外現在這個傳似

intent.putExtra("newsObject", obj); 

和傳遞的ArrayList做Intent.putParcelableArrayListExtra("newsList", arr);

,並在接下來的活動得到像

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

,併爲獲得ArrayList的做

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

我以某種方式獲得我的其他活動中的值爲空時,我檢索它們。 – user3534519

+0

不能直接傳遞列表嗎? – user3534519

+0

是的..你也可以直接通過列表 –

0

第一,創建一個getter方法中檢索您的對象適配器。 。返回你的列表然後你在你的活動中調用你的getter,然後你創建你的意圖。

相關問題