更新android对象存储的问题

时间:2013-03-02 05:41:36

标签: android object

我有一个应用程序,当加载listfragment时,它会加载我的自定义列表适配器,这通常有效,但我现在正在尝试实现对象存储

我正在尝试在这里实现它但是使用这种方法,当加载listfragment时,屏幕中央有一个“加载图标”,它不会消失。我通过日志输出发现这是因为从未输入try块,代码就在它之前,但try块中的代码不是,为什么会这样?

public class MainListFragment extends ListFragment{ 
    OnListSelectedListener mCallback;
    public ObjectStorage mainObjectList = new ObjectStorage();  //creates the list of objects
    SharedPreferences mPrefs;
    int mCurrentPosition = -1;


    // The container Activity must implement this interface so the frag can deliver messages
    public interface OnListSelectedListener {
        /** Called by ListFragment when a list item is selected */
        public void onItemSelected(int position, String schedulename, String[] ampm, boolean[] days, int[] times, boolean vibrate);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    public void onStart() {
        super.onStart();

        updateStorage();
        ByteArrayInputStream byteArray = new ByteArrayInputStream(mPrefs.getString("myobject", "").getBytes());
        ObjectInputStream in;
        try {
            in = new ObjectInputStream(byteArray);
            ObjectStorage updatedStorageList = (ObjectStorage) in.readObject();
            CustomListAdapter adapter = new CustomListAdapter(getActivity(), 
                    R.layout.listview_item_row, updatedStorageList);
            //setListAdapter(new ArrayAdapter<String>(getActivity(), layout, arraylist));
            setListAdapter(adapter);
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    public void updateStorage()
    {
        getActivity();//used for MODE_PRIVATE

        //store object list into android system
        mPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = mPrefs.edit();
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); 
        ObjectOutputStream out;
        try {
            out = new ObjectOutputStream(arrayOutputStream);
            out.writeObject(mainObjectList);
            out.close();
            arrayOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ed.putString("myobject", arrayOutputStream.toString());
        ed.commit();
    }

}

1 个答案:

答案 0 :(得分:0)

如果setListAdapter在try catch块中,并且引发异常,则setListAdapter将永远不会执行对fragment的绑定。您可以使用空列表在try catch块之外调用setListAdapter,然后可以在try catch方法中手动添加对象。

@Override
    public void onStart() {
        super.onStart();
        ObjectStorage emptyList = new ObjectStorage(); // or some like new ArrayList<Object>();
        CustomListAdapter adapter = new CustomListAdapter(getActivity(), 
            R.layout.listview_item_row, emptyList);
setListAdapter(adapter);
    updateStorage();
    ByteArrayInputStream byteArray = new ByteArrayInputStream(mPrefs.getString("myobject", "").getBytes());
    ObjectInputStream in;
    try {
        in = new ObjectInputStream(byteArray);
        ObjectStorage updatedStorageList = (ObjectStorage) in.readObject();

        //setListAdapter(new ArrayAdapter<String>(getActivity(), layout, arraylist));
        adapter.addAll(updatedStorageList);
        adapter.notifyDataSetChanged();
    } catch (StreamCorruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}