将arraylists保存到内部存储

时间:2016-10-08 22:41:22

标签: arraylist

我正在编写一个计算并显示已安装程序数据使用情况的程序。 我想保存我的变量并重新加载它们。 我已经检查了其他有关此问题,但我没有找到答案。 我的代码中有5个Arraylists来保存这些项目: “应用程序名称”,“应用程序图标”,“已接收数据”,“已发送数据”和“总数据”。 我得到这些信息并将Arraylists转换为带有“for循环”的String数组,并使用我的自定义Adapter和listview显示它们。 我的问题是,当我在手机上运行时,我的应用程序停止了。 我使用SharedPreferences保存了一些变量,并将Arraylists保存到内部存储中。 我也试图用SharedPreferences保存Arraylists,我可以保存它们。但是当我想重新加载并将它们设置为arraylists时,我会收到错误。 这是我的“onCreate”方法代码:

public class MainActivity extends AppCompatActivity  {

private static final String MyPREFERENCES="MyPrefs";
private SharedPreferences myPrefs;
int first;
List<String> list=new ArrayList<>();
List<String> s=new ArrayList<>();
List<String> r=new ArrayList<>();
List<String> t=new ArrayList<>();
List<Drawable> ic=new ArrayList<>();
List<Double> StartRx=new ArrayList<>();
List<Double> StartTx=new ArrayList<>();
private long StartTotal;
private long TotalDataUsage;
private long prevData;

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

    setContentView(R.layout.activity_main);

    ListView listView1=(ListView)findViewById(R.id.mainListView1);
    registerForContextMenu(listView1);

    myPrefs=getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    first=myPrefs.getInt("FirstCall",0);
    StartTotal=myPrefs.getLong("StartData",0);
    TotalDataUsage=myPrefs.getLong("TotalData",0);
    prevData=myPrefs.getLong("prevData",0);
    try {
        list=(ArrayList<String>) InternalStorage.readObject(this,"NamesList");
        r=(ArrayList<String>) InternalStorage.readObject(this,"ReceivedData");
        s=(ArrayList<String>) InternalStorage.readObject(this,"SentData");
        t=(ArrayList<String>) InternalStorage.readObject(this,"TotalData");
        ic=(ArrayList<Drawable>) InternalStorage.readObject(this,"IconsList");
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();***
    }
    DataUsage();
    int l=list.size();
    if (l>0) {
        String[] Names = new String[l];
        String[] DReceived = new String[l];
        String[] DSent = new String[l];
        String[] DTotal = new String[l];
        Drawable[] icons = new Drawable[l];
        for (int i = 0; i < l; i++) {
            Names[i] = list.get(i);
            DReceived[i] = r.get(i);
            DSent[i] = s.get(i);
            DTotal[i] = t.get(i);
            icons[i] = ic.get(i);
        }
        assert listView1 != null;
        listView1.setAdapter(new CustomAdapter(this, Names, DReceived, DSent, DTotal, icons));
    }
}

和“onResume”方法代码是:

protected void onPause() {
    super.onPause();
    try {
        InternalStorage.writeObject(this,"NamesList",list);
        InternalStorage.writeObject(this,"ReceivedData",r);
        InternalStorage.writeObject(this,"SentData",s);
        InternalStorage.writeObject(this,"TotalData",t);
        InternalStorage.writeObject(this,"IconsList",ic);
    } catch (IOException e) {
        e.printStackTrace();
    }
    SharedPreferences.Editor edit = myPrefs.edit();
    edit.putInt("FirstCall", first);
    edit.putLong("TotalData",TotalDataUsage);
    edit.putLong("StartData", StartTotal);
    edit.putLong("prevData", prevData);
    edit.commit();
}

和“InternalStorage”类是:

public final class InternalStorage {
private InternalStorage() {}
public static void writeObject(Context context,String fileName,Object object) throws IOException {
    FileOutputStream fos=context.openFileOutput(fileName,Context.MODE_PRIVATE);
    ObjectOutputStream oos=new ObjectOutputStream(fos);
    oos.writeObject(object);
    oos.close();
    fos.close();
}
public static Object readObject(Context context,String fileName) throws IOException, ClassNotFoundException {
    FileInputStream fis=context.openFileInput(fileName);
    ObjectInputStream ois=new ObjectInputStream(fis);
    return ois.readObject();
}

}

我没有从android studio收到任何错误。

我知道问题出在以下代码中,因为当我删除它们时,它会起作用!

try {
        list=(ArrayList<String>) InternalStorage.readObject(this,"NamesList");
        r=(ArrayList<String>) InternalStorage.readObject(this,"ReceivedData");
        s=(ArrayList<String>) InternalStorage.readObject(this,"SentData");
        t=(ArrayList<String>) InternalStorage.readObject(this,"TotalData");
        ic=(ArrayList<Drawable>) InternalStorage.readObject(this,"IconsList");
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

和这段代码:

try {
        InternalStorage.writeObject(this,"NamesList",list);
        InternalStorage.writeObject(this,"ReceivedData",r);
        InternalStorage.writeObject(this,"SentData",s);
        InternalStorage.writeObject(this,"TotalData",t);
        InternalStorage.writeObject(this,"IconsList",ic);
    } catch (IOException e) {
        e.printStackTrace();
    }

任何人都可以帮我吗?!!

0 个答案:

没有答案