2017-01-06 1 views
-2

를로드하지 않습니다 여기에 저장하고 데이터를로드하는 데 사용됩니다 내 savaData 및 loadData 방법이다된 SharedPreferences 저장하거나 내 데이터 안드로이드

여기
public void saveData(){ 
     SharedPreferences sharedPreferences= getActivity().getSharedPreferences("SubjectTitle", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor=sharedPreferences.edit(); 
     int i=0,n; 
     n=addArray.size(); 
     for(Bunk b: addArray) { 
      editor.putString("Title"+i, b.getTitle()); 
      editor.putInt("No_of_bunk"+i, b.getBunk_remain()); 
      editor.putFloat("pre_of_att"+i, b.getPrecentageAtt()); 
      i++; 
     } 
     editor.putInt("size_of_data",n); 
     editor.apply(); 
     Toast.makeText(getActivity(),"data saved",Toast.LENGTH_SHORT).show(); 

    } 
    public ArrayList<Bunk> loadData(){ 
     SharedPreferences sharedPreferences= getActivity().getSharedPreferences("SubjectTitle", Context.MODE_PRIVATE); 
     int n=sharedPreferences.getInt("size_pf_data",Default); 
     String loadTitle; 
     int loadBunk; 
     float loadAtt; 
     for(int i=0;i<n;i++){ 
      loadTitle=sharedPreferences.getString("Title"+i,DEFAULT); 
      loadBunk=sharedPreferences.getInt("No_of_bunk"+i,Default); 
      loadAtt=sharedPreferences.getFloat("pre_of_att"+i,def_ault); 
      addArray.add(new Bunk(loadTitle,loadBunk,loadAtt)); 
     } 
     return addArray; 
    } 

입니다 loadData 방법에서 데이터를로드하는 데 사용되는 코드

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup perent, Bundle savedInstanceState) { 

    super.onCreateView(inflater, perent, savedInstanceState); 
    View v = inflater.inflate(R.layout.fragment_main, perent, false); 
     show=(ListView)v.findViewById(android.R.id.list); 
    addArray=loadData(); 
    adapter=new BunkAdapter(addArray); 
    show.setAdapter(adapter); 
    return v; 
} 
+0

스택 오버플로에 오신 것을 환영합니다! [둘러보기]를 먼저 듣고 좋은 질문을 배우고 [mcve]를 만들 수 있습니다. 그렇게하면 우리가 당신을 도울 수 있습니다. – Katie

답변

1

먼저 editor.commit()을 사용하십시오.을 넣은 후 데이터를 저장하거나 직접 저장하는 경우 : editor.putString("Title"+i, b.getTitle()).commit(); 둘째로, 내가 잘못 생각한다고 생각합니다. 공유 기본 설정은 설정이나 소량의 데이터와 같은 경우에 더 좋으며, 경우에 따라 SQLite를 사용하여 대용량 데이터를 저장해야합니다.

관련 문제