Android中三个片段之间的传递包
我有三个片段和一个对象,我需要从第一个片段传递到第二个片段,然后传递到第三个片段 第一:Android中三个片段之间的传递包,android,android-fragments,bundle,Android,Android Fragments,Bundle,我有三个片段和一个对象,我需要从第一个片段传递到第二个片段,然后传递到第三个片段 第一: ArrayList<Article> list = app.getDb().getArticles("userId", app.getUser(getActivity()).getId(), "id", item.getId()); FragmentTransaction t = getActivity().getSupportFragmentM
ArrayList<Article> list = app.getDb().getArticles("userId", app.getUser(getActivity()).getId(),
"id", item.getId());
FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
ArticleFragment mFrag = new ArticleFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("article", list.get(0));
mFrag.setArguments(bundle);
t.replace(R.id.fragment_container, mFrag, ArticleFragment.TAG);
t.addToBackStack(ArticleFragment.TAG);
t.commit();
第三:
article = getArguments().getParcelable("article_edit");
//some actions with article
当我完成第三个片段并返回第二个片段时,问题是文章发生了变化。第二个包含我在第三个片段中编辑的相同对象。如何解决此问题?在您的
第一个
片段中,您已将捆绑包中的值作为文章列表
传递,而在第二个片段中,您将作为文章
获得它,它应该是文章列表
。因此,在您的第二个片段中将其更改如下:
更改以下行:
article = getArguments().getParcelable("article");
到
改变
第二段
article = new Article(getArguments().getParcelable("article"));
第三段
article = new Article(getArguments().getParcelable("articles_list"));
在您的文章pojo中创建一个构造函数
Article(Article A1)
{
value1=A1.value1;
value2=A1.value2;
value3=A1.value3;
...
}
希望这能有所帮助我的解决办法是在文章类中创建自己的“克隆”方法
incomeArticle = getArguments().getParcelable("article_edit");
article = incomeArticle.cloneArticle();
/**
* Clone all field of current article to new
*
* @return new article
*/
public Article cloneArticle() {
Article article = new Article();
article.setName(this.name);
article.setId(this.id);
article.setUserId(this.userId);
article.setTypeId(this.typeId);
article.setPictureId(this.pictureId);
article.setAccess(this.access);
if (this.pictures != null) {
article.setPictures((ArrayList<String>) this.pictures.clone());
} else {
article.setPictures(new ArrayList<String>());
}
article.setComments(this.comments);
if (this.tags != null) {
article.setTags((ArrayList<String>) this.tags.clone());
} else {
article.setTags(new ArrayList<String>());
}
if (this.care != null) {
article.setCare((ArrayList<String>) this.care.clone());
} else {
article.setCare(new ArrayList<String>());
}
article.setFitLevel(this.fitLevel);
article.setRating(this.rating);
if (this.materials != null) {
article.setMaterials((ArrayList<ArticleMaterial>) this.materials.clone());
} else {
article.setMaterials(new ArrayList<ArticleMaterial>());
}
article.setBuyingDate(this.buyingDate);
article.setSize(this.size);
article.setArticul(this.articul);
article.setPrice(this.price);
article.setMadeInCountryId(this.madeInCountryId);
article.setCurrencyId(this.currencyId);
article.setCommentsCount(this.commentsCount);
article.setToWeight(this.toWeight);
article.setBrandId(this.brandId);
article.setChanged(this.changed);
article.setShowWeight(this.showWeight);
article.setCreated(this.created);
article.setDescription(this.description);
article.setIsLiked(this.isLiked);
article.setLikeRating(this.likeRating);
article.setFromWeight(this.fromWeight);
article.setGuid(this.guid);
return article;
}
incomeArticle=getArguments().getParcelable(“文章编辑”);
第条=国际贸易术语解释通则第()条;
/**
*将当前文章的所有字段克隆为新字段
*
*@返回新文章
*/
公共物品条款(){
第条=新条款();
article.setName(本名称);
第.setId条(this.id);
article.setUserId(this.userId);
article.setTypeId(本.typeId);
article.setPictureId(本.pictureId);
第.setAccess条(本.access);
如果(this.pictures!=null){
article.setPictures((ArrayList)this.pictures.clone());
}否则{
article.setPictures(新ArrayList());
}
第.setComments条(本.comments);
如果(this.tags!=null){
article.setTags((ArrayList)this.tags.clone());
}否则{
article.setTags(新ArrayList());
}
if(this.care!=null){
article.setCare((ArrayList)this.care.clone();
}否则{
article.setCare(新ArrayList());
}
第.setFitLevel(本.fitLevel)条;
第条设定评级(本评级);
如果(this.materials!=null){
article.setMaterials((ArrayList)this.materials.clone());
}否则{
article.setMaterials(新ArrayList());
}
第条购买日期(本购买日期);
article.setSize(此尺寸);
第条(本条);
第条设定价格(本价格);
article.setMadeInCountryId(此.madeInCountryId);
article.setCurrencyId(本.currencyId);
article.SetCommentScont(此.CommentScont);
第条.设置光照(本.光照);
第条.setBrandId(本条.brandId);
第条变更(本条变更);
第条.设置显示重量(本.显示重量);
第.setCreated条(本.created条);
第条设置说明(本说明);
第.setIsLiked条(本.isLiked条);
第条.设定(本.设定);
物品。设置重量(此。重量);
article.setGuid(this.guid);
退货物品;
}
您遇到了什么错误?请确认您的问题中的错误。我在应用程序中没有错误消息。问题在于文章对象。当我在第三个片段中更改它并返回到第二个片段时,第二个片段中的文章也是changet。我想避免这种情况,只改变第三个片段中的文章。你只需在你的第三个片段中用不同的名称传递包,然后在你的第三个片段中用不同的值将其更改,然后在另一个可包裹实例中将其放入第二个片段。但是如何传递这篇文章的克隆而不是原始对象?ArrayList没有这样的构造函数。请继续首先签入网络…这里有一个构造函数ArrayList(集合c)…我只针对我的案例说话。如果我只在ArrayList中放置Article对象,如何从参数中获取ArrayList?但一般来说,这个构造函数是存在的。
Article(Article A1)
{
value1=A1.value1;
value2=A1.value2;
value3=A1.value3;
...
}
incomeArticle = getArguments().getParcelable("article_edit");
article = incomeArticle.cloneArticle();
/**
* Clone all field of current article to new
*
* @return new article
*/
public Article cloneArticle() {
Article article = new Article();
article.setName(this.name);
article.setId(this.id);
article.setUserId(this.userId);
article.setTypeId(this.typeId);
article.setPictureId(this.pictureId);
article.setAccess(this.access);
if (this.pictures != null) {
article.setPictures((ArrayList<String>) this.pictures.clone());
} else {
article.setPictures(new ArrayList<String>());
}
article.setComments(this.comments);
if (this.tags != null) {
article.setTags((ArrayList<String>) this.tags.clone());
} else {
article.setTags(new ArrayList<String>());
}
if (this.care != null) {
article.setCare((ArrayList<String>) this.care.clone());
} else {
article.setCare(new ArrayList<String>());
}
article.setFitLevel(this.fitLevel);
article.setRating(this.rating);
if (this.materials != null) {
article.setMaterials((ArrayList<ArticleMaterial>) this.materials.clone());
} else {
article.setMaterials(new ArrayList<ArticleMaterial>());
}
article.setBuyingDate(this.buyingDate);
article.setSize(this.size);
article.setArticul(this.articul);
article.setPrice(this.price);
article.setMadeInCountryId(this.madeInCountryId);
article.setCurrencyId(this.currencyId);
article.setCommentsCount(this.commentsCount);
article.setToWeight(this.toWeight);
article.setBrandId(this.brandId);
article.setChanged(this.changed);
article.setShowWeight(this.showWeight);
article.setCreated(this.created);
article.setDescription(this.description);
article.setIsLiked(this.isLiked);
article.setLikeRating(this.likeRating);
article.setFromWeight(this.fromWeight);
article.setGuid(this.guid);
return article;
}