更新android对象存储时出现问题

更新android对象存储时出现问题,android,object,Android,Object,我有一个应用程序,当加载listfragment时,它会加载我的自定义列表适配器,该适配器通常可以正常工作,但我现在正在尝试实现对象存储,这样即使在关闭和重新打开应用程序时,对象也会一直存在,并且只有在用户手动删除它们时才会被删除 我试图在这里实现它,但使用这种方法,当加载listfragment时,屏幕中央有一个“加载图标”,它不会消失。我通过日志输出发现,这是因为从未输入try块,之前的代码是,但是try块中的代码不是,为什么会这样 public class MainListFragment

我有一个应用程序,当加载
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();
    }

}
public类MainListFragment扩展ListFragment{
OnListSelectedListener McCallback;
public ObjectStorage main objectlist=new ObjectStorage();//创建对象列表
共享参考文献;
int mCurrentPosition=-1;
//容器活动必须实现此接口,以便frag可以传递消息
仅限公共接口ListSelectedListener{
/**选择列表项时由ListFragment调用*/
已选择公共无效项(int位置,字符串schedulename,字符串[]ampm,布尔值[]天,int[]次,布尔值振动);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
public void onStart(){
super.onStart();
updateStorage();
ByteArrayInputStream byteArray=新的ByteArrayInputStream(mPrefs.getString(“myobject”,“myobject”).getBytes());
目标输入流;
试一试{
in=新对象输入流(byteArray);
.readObject()中的ObjectStorage updatedStorageList=(ObjectStorage);
CustomListAdapter=新的CustomListAdapter(getActivity(),
R.layout.listview_item_row,updatedStorageList);
//setListAdapter(新的ArrayAdapter(getActivity(),layout,arraylist));
setListAdapter(适配器);
}捕获(StreamCorruptedException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
public void updateStorage()
{
getActivity();//用于模式_PRIVATE
//将对象列表存储到android系统中
mPrefs=getActivity().getPreferences(Context.MODE\u PRIVATE);
SharedReferences.Editor ed=mPrefs.edit();
ByteArrayOutputStream arrayOutputStream=新建ByteArrayOutputStream();
对象输出流输出;
试一试{
out=新对象输出流(arrayOutputStream);
out.writeObject(mainObjectList);
out.close();
arrayOutputStream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
ed.putString(“myobject”,arrayOutputStream.toString());
ed.commit();
}
}

如果setListAdapter位于try-catch块中,并且引发异常,则setListAdapter将永远不会执行绑定到片段。您可以使用空列表在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();
    }


}
@覆盖
public void onStart(){
super.onStart();
ObjectStorage emptyList=new ObjectStorage();//或类似于new ArrayList()的文件;
CustomListAdapter=新的CustomListAdapter(getActivity(),
R.layout.listview_item_row,emptyList);
setListAdapter(适配器);
updateStorage();
ByteArrayInputStream byteArray=新的ByteArrayInputStream(mPrefs.getString(“myobject”,“myobject”).getBytes());
目标输入流;
试一试{
in=新对象输入流(byteArray);
.readObject()中的ObjectStorage updatedStorageList=(ObjectStorage);
//setListAdapter(新的ArrayAdapter(getActivity(),layout,arraylist));
adapter.addAll(updatedStorageList);
adapter.notifyDataSetChanged();
}捕获(StreamCorruptedException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}