【问题标题】:ArrayList(XmlValueModel) move from one activity to another ActivityArrayList(XmlValueModel) 从一个活动移动到另一个活动
【发布时间】:2015-09-15 12:08:41
【问题描述】:

我将 Arraylist 一个活动发送到另一个活动,但第二个活动显示空指针和空适配器。如何解决?谢谢。

第一个活动:

        Bundle b = new Bundle();
        b.putSerializable("array_list", listdata);
        Intent movedata = new Intent(getApplicationContext(),Shopping.class);
        movedata.putExtras(movedata);
        startActivity(movedata);

第二个活动:

Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();

        ArrayList<XmlValueModel> listdata = (ArrayList<XmlValueModel>) bundle.getSerializable("array_list");

  //     ArrayAdapter<XmlValueModel> array = new ArrayAdapter<XmlValueModel>(getApplicationContext(), android.R.layout.simple_list_item_1, list);

       listview = (ListView) findViewById(R.id.ListOne);
       adapter = new CustomListAdapter(this, listdata);
       listview.setAdapter(adapter);
   }catch (Exception e){
        Log.e("Error:", e.getMessage());
   }

【问题讨论】:

    标签: show


    【解决方案1】:

    您的 XmlValueModel 是否可序列化?您应该使用 Serializable 实现 XmlValueModel!

    public class XmlValueModel implements Serializable {...}
    

    如果 XmlValueModel 来自库,则从 XmlValueModel 派生一个新类并实现 Serializable。

    【讨论】:

    • 您的列表在放入捆绑包之前是否为非空?
    【解决方案2】:

    试试这个方法可能对你有帮助

    首先要制作

    XmlValueModel 类实现 Serializable

    public class XmlValueModel implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    }
    

    在您的 FirstActivity 中 这样做

    ArrayList<XmlValueModel> XmlValueModelArrayList = new ArrayList<XmlValueModel>();
    
    Intent intent = new Intent(this,secondActivity.class);
    intent.putExtra("XmlValueModelArrayList", XmlValueModelArrayList);
    

    在您的 SecondActivity 中 这样做

    ArrayList<XmlValueModel> XmlValueModelList;
    
    XmlValueModelList = (ArrayList<XmlValueModel>) getIntent().getSerializableExtra(
                "XmlValueModelArrayList");
    

    【讨论】:

    • 未经检查的演员表:'java.io.Serializable'到'java.util.ArrayList......哪种类型的警告......?
    • 按照我的代码 ArrayList XmlValueModelList;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2013-07-05
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2012-08-27
    相关资源
    最近更新 更多