简体   繁体   English

Android - startActivityForResult 传回对象的 ArrayList

[英]Android - startActivityForResult passing ArrayList of an Object Back

So I have Activity A and Activity B, in "A" i need to go to "B" and then add Food items to an arrayList of them and then pass this array list back to Activity A, how ever i am getting issues with my current set up所以我有活动 A 和活动 B,在“A”中,我需要转到“B”,然后将食品项目添加到它们的数组列表中,然后将此数组列表传递回活动 A,我怎么会遇到问题当前设置

This is the Intent From A to B这是从 A 到 B 的意图

Intent i = new Intent(OrderMain.this , OrderAdd.class);
startActivityForResult(i,1);

This is the Return intent From B to A这是从 B 到 A 的返回意图

Intent returnIntent = new Intent();
Bundle b = new Bundle();
b.putSerializable("results", orderArray);
returnIntent.putExtras(b);
setResult(Activity.RESULT_OK, returnIntent);
finish();

And finally this is my onActivityResults In A最后这是我的 onActivityResults In A

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            Bundle b = getIntent().getExtras();
            ArrayList<Food> test = new ArrayList<>();
            test = (ArrayList<Food>) b.getSerializable("results");
            setOrderArray(test);
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}

setOrderArray is just a function to set the Array to the new returned Array setOrderArray 只是将 Array 设置为新返回的 Array 的函数

And this is the Food Object这是食物对象

import java.io.Serializable;

public class Food implements Serializable{
    private String name;
    private int cost;
    private int quantity;

public Food( String name, int cost, int quantity){
        this.name = name;
        this.cost = cost;
        this.quantity = quantity;
    }
    public void setQuantity(int quantity)
    {
        this.quantity = quantity;
    }
    public void setName(String name)
    {
       this.name = name;
    }
    public void setCost(int cost)
    {
        this.cost = cost;
    }
    public int getTotal()
    {
        return cost * quantity;
    }
    public String getName ()
    {
        return name;
    }
    public int getCost()
    {
        return cost;
    }
    public int getQuantity()
    {
        return quantity;
    }
    public void addQuantity()
    {
        quantity++;
    }
    public void decQuantity()
    {
        quantity--;
    }
}

the current issue i have is at this line of code:我目前遇到的问题是这行代码:

test = (ArrayList<Food>) b.getSerializable("results");

It is saying there is an "Unchecked Cast" and the intended use for this does not work as it should, any help would be appreciated它说有一个“未经检查的演员”,并且它的预期用途不能正常工作,任何帮助将不胜感激

Because you are getting data from wrong intent. 因为您从错误的意图中获取数据。 You should change your code from 您应该从

Bundle b = getIntent().getExtras();
ArrayList<Food> test = new ArrayList<>();
test = (ArrayList<Food>) b.getSerializable("results");

to

ArrayList<Food> test = (ArrayList<Food>) data.getSerializableExtra("results");

Do not use Serializable, use Parcelable instead. 不要使用Serializable,而应使用Parcelable。 Parcelable is speed king and make sure your object class implements Parcelable. Parcelable是速度之王,并确保您的对象类实现Parcelable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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