如何把一个向量成intent.extra? [英] How to put a Vector into an intent.extra?

查看:88
本文介绍了如何把一个向量成intent.extra?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始写了Android的一个小应用程序。它看起来很漂亮,但有一件事我不明白。

我已经创建了活性的新意图,我添加了一个序列化的载体 intent.putExtra(钥匙,向量)。在所谓的b活动我这样做:矢量< ItemModel>项目=(矢量< ItemModel>)。getIntent()getExtras()getSerializable(钥匙)。此行引起 ClassCastException异常。有谁知道为什么吗?请大家帮帮我: - )

下面是两个活动的源头 ItemModel

ItemModel:

 公共类ItemModel实现了java.io.Serializable {
    私有静态最后长的serialVersionUID = -1772392719565658730L;
    公众诠释ID;
    公共字符串名称;
    公共字符串数量;
    公众诠释状态;

}
 

ShoppingListActivity(A):

 公共无效onClickItems(最终​​查看V){
        最终意向意图=新的意图(这一点,ItemListActivity.class);

        矢量< ItemModel> V1 =新矢量< ItemModel>();
        ItemModel项目=新ItemModel();

        item.name =Tomaten;
        item.quantity =400克;
        item.id = 12;
        item.status = 0;
        v1.add(项目);

        项目=新ItemModel();
        item.name =Bohnen;
        item.quantity =150克;
        item.id = 13;
        item.status = 0;
        v1.add(项目);

        项目=新ItemModel();
        item.name =Schokolade;
        item.quantity =5 Tafeln;
        item.id = 17;
        item.status = 0;
        v1.add(项目);

        intent.putExtra(ShoppingList,第一版);

        startActivity(意向);
 

ItemListActivity(B):

 公共类ItemListActivity延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.item);
        ListView控件列表=(ListView控件)findViewById(R.id.ITEMSLIST);
        最终捆绑额外= getIntent()getExtras()。
        矢量< ItemModel>项=(矢量< ItemModel&GT)extras.getSerializable(ShoppingList);
        ArrayList的< HashMap的<字符串,对象>> itemsList =新的ArrayList< HashMap的<字符串,对象>>();
        HashMap的<字符串,对象> itemMap =新的HashMap<字符串,对象>();
}
 

解决方案

任何实现列表和序列化的内部变成了一个ArrayList。因此,它可能会去作为一个载体,但它出来作为ArrayList。它可能会更好的治疗它只是一个List<>在推杆和getter两端

作为一般规则,不要使用向量两种。所有的方法是同步的,这使得它比一个ArrayList效率较低。

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

解决方案

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.

这篇关于如何把一个向量成intent.extra?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆