打开时应用关闭/检索时存储自定义对象的ArrayList

时间:2014-10-08 02:58:19

标签: android arraylist sharedpreferences gson android-preferences

我有一个名为Singleton的{​​{1}}类,其中有几个GlobalData存储我创建的自定义对象。

现在我想检查应用何时关闭(ArrayLists MainActivity's方法)并存储数据,以便在应用重新打开时检索该数据。

我尝试使用谷歌的onStop/onDestroy框架做到这一点无济于事;这是我实施的内容。

MainActivity:

GSON

我的GlobalData类:

    package com.nanospark.cnc;

import java.util.ArrayList;

import com.google.gson.Gson;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
//import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
    public SharedPreferences mPrefs;
    public Editor preferenceEditor;
    GridLayout_Fragment profileGridFragment = new GridLayout_Fragment();
    EventList_Fragment eventListFragment = new EventList_Fragment();
    ContactList_Fragment contactListFragment = new ContactList_Fragment();
    FragmentManager transactionManager;
    FragmentTransaction transaction;
    CustomIOIO customioio;
    GlobalData globaldata = GlobalData.getInstance();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*      mPrefs = getPreferences(MODE_PRIVATE);

        preferenceEditor = mPrefs.edit();
        retrieveGlobalDataFromStorage();*/
        //insert the initial fragment for when the app boots.
         transactionManager = getSupportFragmentManager();
        transaction = transactionManager.beginTransaction();
        transaction.add(R.id.fragment_frame, profileGridFragment);
        transaction.addToBackStack(null);
        transaction.commit();
         customioio = (CustomIOIO) getApplicationContext();
         customioio.create();
         customioio.setTheme(R.style.AppTheme);

    }

     @Override
        protected void onDestroy(){
           super.onDestroy();
           storeGlobalData();
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
        case R.id.action_settings:
            openSettings();
            return true;
        case R.id.action_home:
            openHome();
            return true;
        case R.id.action_events:
            openEvents();
            return true;
        case R.id.action_help:
            openHelp();
            return true;
        case R.id.action_contacts:
            openContacts();
            return true;

        }
        return super.onOptionsItemSelected(item);
    }

    private void openContacts() {
         transactionManager = getSupportFragmentManager();
        transaction = transactionManager.beginTransaction();
        transaction.replace(R.id.fragment_frame, contactListFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    private void openHome() {
         transactionManager = getSupportFragmentManager();
        transaction = transactionManager.beginTransaction();
        transaction.replace(R.id.fragment_frame, profileGridFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    private void openHelp() {

    }

    private void openEvents() {
         transactionManager = getSupportFragmentManager();
        transaction = transactionManager.beginTransaction();
        transaction.replace(R.id.fragment_frame, eventListFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    private void openSettings() {

    }

    public void storeGlobalData(){
        Gson gson = new Gson();
        //Transform the ArrayLists into JSON Data.
        String machineProfileListJSON = gson.toJson(globaldata.getMachineProfileList());
        String contactInfoListJSON = gson.toJson(globaldata.getContactInfoList());
        String eventInfoListJSON = gson.toJson(globaldata.getEventInfoList());
        preferenceEditor.putString("machineProfileListJSONData", machineProfileListJSON);
        preferenceEditor.putString("contactInfoListJSONData", contactInfoListJSON);
        preferenceEditor.putString("eventInfoListJSONData", eventInfoListJSON);
        //Commit the changes.
        preferenceEditor.commit();

    }
    public void retrieveGlobalDataFromStorage(){
        if(mPrefs.contains("machineProfileListJSONData")){
            Gson gson = new Gson();
            String machineProfileListJSON = mPrefs.getString("machineProfileListJSONData", "");
            String contactInfoListJSON = mPrefs.getString("contactInfoListJSONData", "");
            String eventInfoListJSON = mPrefs.getString("eventInfoListJSONData", "");
            globaldata.setMachineProfileList(gson.fromJson(machineProfileListJSON, ArrayList.class));
            globaldata.setContactInfoList(gson.fromJson(contactInfoListJSON, ArrayList.class));
            globaldata.setEventInfoList(gson.fromJson(eventInfoListJSON, ArrayList.class));
        }
    }
}

然而,这种方法似乎不起作用;任何想法?

1 个答案:

答案 0 :(得分:1)

试试这个:

更改:

        globaldata.setMachineProfileList(gson.fromJson(machineProfileListJSON, ArrayList.class));
        globaldata.setContactInfoList(gson.fromJson(contactInfoListJSON, ArrayList.class));
        globaldata.setEventInfoList(gson.fromJson(eventInfoListJSON, ArrayList.class));

        Type  machineProfileListType = new TypeToken<Collection<MachineProfile>>() {}.getType();
        globaldata.setMachineProfileList(gson.fromJson(machineProfileListJSON, machineProfileListType)));

        Type contactInfoListType = new TypeToken<Collection<ContactInfo>>() {}.getType();
        globaldata.setContactInfoList(gson.fromJson(contactInfoListJSON, contactInfoListType ));

        Type eventListType = new TypeToken<Collection<Event>>() {}.getType();
        globaldata.setEventInfoList(gson.fromJson(eventInfoListJSON, eventListType));

onPause调用商店,并在每个活动的onCreate调用检索。