How to put a Vector into an intent.extra?

2019-02-18 03:56发布

I've started to write a small app for android. It looks very nice, but there is one thing I don't understand.

I've created a new intent in the activity A and I've added a serialized vector intent.putExtra("key", vector). On the called activity B I'm doing this: Vector<ItemModel> items = (Vector<ItemModel>) getIntent().getExtras().getSerializable("key"). This line causes a ClassCastException. Does anyone know why? Please help me :-)

Here is the source of the two activities and of the ItemModel:

ItemModel:

public class ItemModel implements java.io.Serializable {
    private static final long serialVersionUID = -1772392719565658730L;
    public int id;
    public String name;
    public String quantity;
    public int status;

}

ShoppingListActivity (A):

   public void onClickItems(final View v){
        final Intent intent = new Intent(this,ItemListActivity.class);

        Vector<ItemModel> v1 = new Vector<ItemModel>();
        ItemModel item = new ItemModel();

        item.name = "Tomaten";
        item.quantity = "400g";
        item.id = 12;
        item.status = 0;
        v1.add(item);

        item = new ItemModel();
        item.name = "Bohnen";
        item.quantity = "150g";
        item.id = 13;
        item.status = 0;
        v1.add(item);

        item = new ItemModel();
        item.name = "Schokolade";
        item.quantity = "5 Tafeln";
        item.id = 17;
        item.status = 0;
        v1.add(item);

        intent.putExtra("ShoppingList", v1);

        startActivity(intent);

ItemListActivity (B):

   public class ItemListActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);
        ListView list = (ListView) findViewById(R.id.ITEMSLIST);
        final Bundle extras = getIntent().getExtras();
        Vector<ItemModel> items = (Vector<ItemModel>) extras.getSerializable("ShoppingList");
        ArrayList<HashMap<String, Object>> itemsList = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> itemMap = new HashMap<String, Object>();
}

3条回答
干净又极端
2楼-- · 2019-02-18 04:08

bit confused... but sure you didn't need Vector<ItemModel> instead of Vector for your class cast?

Which type is returned by getIntent().getExtras().getSerializable("key")?

查看更多
The star\"
3楼-- · 2019-02-18 04:10

Anything that implements List and Serializable is internally turned into an ArrayList. So it might go in as a Vector but it comes out as an ArrayList. It's probably better to treat it as just a List<> at both ends in the putter and getter.

As a general rule, don't use Vector either. All the methods are synchronized which makes it less efficient than an ArrayList.

查看更多
Rolldiameter
4楼-- · 2019-02-18 04:33

Give this a try: replace

Vector<ItemModel> v1 = new Vector<ItemModel>();

with

List<ItemModel> v1 = new ArrayList<ItemModel>();

and

Vector<ItemModel> items = (Vector<ItemModel>) extras.getSerializable("ShoppingList");

with

List<ItemModel> items = (List<ItemModel>) extras.getSerializable("ShoppingList");

Declaring v1 and items as List objects should be enough, the change to ÁrrayList would just show if there was some strange issue with the Vector classes.

According to the doc I see no reason why it shouldn't work with the Vector class too. Maybe, by chance, you imported different Vector classes, so you could double check your import statements if all classes import java.lang.Vector only.

查看更多
登录 后发表回答