简体   繁体   中英

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

This is the Intent From A to B

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

This is the Return intent From B to 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

    @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

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. Parcelable is speed king and make sure your object class implements Parcelable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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