0

나는 GlobalData이라는 Singleton 클래스가 있는데 여기에는 내가 만든 사용자 정의 객체를 저장하는 여러 개의 ArrayLists이 있습니다.열 때 응용 프로그램이 닫히거나 검색 될 때 사용자 정의 객체의 ArrayList 저장

이제 앱을 닫을 때 (MainActivity'sonStop/onDestroy 메쏘드) 데이터를 저장하고 앱이 다시 열릴 때 검색 할 수 있도록 데이터를 저장하고 싶습니다.

Google의 GSON 프레임 워크를 사용하지 않으려 고 시도했습니다. 여기에 제가 구현 한 것이 있습니다.

MainActivity : 또한

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)); 
     } 
    } 
} 

내 GlobalData에 클래스 :

package com.nanospark.cnc; 

import java.util.ArrayList; 

import android.widget.ArrayAdapter; 

public class GlobalData { 
    private static GlobalData instance = null; 

    // ArrayLists of Data. 
    public ArrayList<MachineProfile> machineProfileList = new ArrayList<MachineProfile>(); 
    public ArrayList<ContactInfo> contactInfoList = new ArrayList<ContactInfo>(); 
    public ArrayList<Event> eventInfoList = new ArrayList<Event>(); 

    protected GlobalData() { 
     // Exists only to defeat instantiation. 
    } 

    public static GlobalData getInstance() { 
     if (instance == null) { 
      instance = new GlobalData(); 
     } 
     return instance; 
    } 

    public ArrayList<MachineProfile> getMachineProfileList() { 
     return machineProfileList; 
    } 

    public void setMachineProfileList(
      ArrayList<MachineProfile> machineProfileList) { 
     this.machineProfileList = machineProfileList; 
    } 

    public ArrayList<ContactInfo> getContactInfoList() { 
     return contactInfoList; 
    } 

    public void setContactInfoList(ArrayList<ContactInfo> contactInfoList) { 
     this.contactInfoList = contactInfoList; 
    } 

    public ArrayList<Event> getEventInfoList() { 
     return eventInfoList; 
    } 

    public void setEventInfoList(ArrayList<Event> eventInfoList) { 
     this.eventInfoList = eventInfoList; 
    } 

} 

그러나이 방법이 작동하지 않는 것; 어떤 아이디어?

변경 :

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

onPause에서 전화 매장

 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)); 

에 각 활동의 onCreate에서 검색 전화

+0

onSaveInstance를 사용하고 번들에서 onCreate에서 데이터를 다시 검색하십시오. –

답변

1

이 시도.

+0

시도해 보았습니다. 모든 유형 유형에 대해 작동하지 않습니다. 거기에 네 가지 수입이 있습니다 ... – Tukajo

+0

내 대답을 봐! – mmlooloo

+0

사실 @mmlooloo 내 솔루션이 효과가 있지만 문제를 설명 할 수 있다고 생각합니다. 응용 프로그램을 열 때마다 데이터를 잘 만들 수 있습니다.하지만 Contact 개체를 만들고 ArrayList에 추가하는 것과 같은 두 번째 작업으로 전환하면 MainActivity로 돌아갈 때 retrieveGlobalDataFromStorage(); 방금 만든 데이터를 덮어 씁니다! – Tukajo