在包

时间:2015-07-03 08:16:30

标签: java android arraylist

之前已经问过这个问题,但我没有找到对我的具体案例有帮助的答案。

我第一次访问该类时,正在填充ArrayList部分来自另一个activity,但是当我在初始创建它时尝试访问它时,我一直在丢失部件实例。每次调用Task时,部件都是空的,我不知道如何让它充满我希望它填充的细节。

public class Task extends ListActivity{
    private PartAdapter partsArrayAdapter;
    ArrayList<Parts> parts ;//= new ArrayList<>()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null){
            parts = (ArrayList<Parts>)savedInstanceState.getSerializable("parts");
        }
        else {
            try {
            // Get the Bundle Object
            Bundle bundleObject = getIntent().getExtras();

            // Get ArrayList Bundle
            parts = (ArrayList<Parts>) bundleObject.getSerializable("parts");


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        setContentView(R.layout.activity_parts_list);

        partsArrayAdapter = new PartAdapter(this,parts);
        setListAdapter(partsArrayAdapter);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putSerializable("parts", parts);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        parts = (ArrayList<Parts>)savedInstanceState.getSerializable("parts");
    }
}

0 个答案:

没有答案