-1

我無法創建共享首選項文件我一直在爲此掙扎2天,請幫助,我是新來的。在我的應用程序中,我有不同的屏幕上有15個問題,我想要存儲所選選項的文本,以便將來可以使用它。 我的代碼我無法創建共享首選項文件

公共類QuestionPagerFragment擴展片段{

protected View mView; 

String pageData[]; //Stores the text to swipe. 
String optionData[];//Stores the option data. 

static int rid; 
RadioGroup group; 
static ArrayList<Integer> list = new ArrayList(); 

SharedPreferences prefs; 
public static final String MyPREFERENCES = "MyPrefs" ; 


@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    mView = inflater.inflate(R.layout.page, container, false); 
    return mView; 
} 

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    prefs = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
    pageData=getResources().getStringArray(R.array.desserts); 

    optionData=getResources().getStringArray(R.array.options); 

    group = (RadioGroup)mView.findViewById(R.id.group); 

    group.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      // TODO Auto-generated method stub 
      int radioButtonID = group.getCheckedRadioButtonId(); 
      View radioButton = group.findViewById(radioButtonID); 
      rid = group.indexOfChild(radioButton); 
      list.add(getArguments().getInt("pos"), rid); 
      click(); 

     } 
    }); 
    ((TextView)mView.findViewById(R.id.textMessage)).setText(pageData[getArguments().getInt("pos")]); 
    if(getArguments().getInt("pos") == 14) { 
     ((Button)mView.findViewById(R.id.submitbtn)).setVisibility(View.VISIBLE); 
     ((Button)mView.findViewById(R.id.submitbtn)).setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v){ 

       }     
      }); 
    } 

    for(int i = 0; i < group.getChildCount(); i++){ 
     ((RadioButton) group.getChildAt(i)).setText(optionData[(getArguments().getInt("pos")*4)+i]); 
    }  
} 



public static void save() { 
     if(rid==0){ 
      MainActivity.FLAG_A++;     
     } 
     else if(rid==1){ 
      MainActivity.FLAG_B++;  
     } 
     else if(rid==2){ 
      MainActivity.FLAG_C++;  
     } 
     else if(rid==3){ 
      MainActivity.FLAG_D++;  
     }    
} 
public void click(){ 

     SharedPreferences prefs = getActivity().getSharedPreferences("idValue", 0); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("idValue", list.get(getArguments().getInt("pos")).toString()); 
     editor.commit(); 
} 

}

+0

共享prefrence不是一個文件,這是存儲文本的方式在內部存儲器,如果你想使用它的一些其他活動,那麼你可以訪問它 – Amitsharma 2015-02-06 13:20:59

+0

與文件,我的意思是我無法找到SP在數據文件夾 – user3699550 2015-02-06 13:25:12

+0

共享prefrence不用於讀取數據或從文件中獲取數據這是用來存儲在內部應用中僅在變量的那一側進行評估將永遠存儲,如果你清理它,然後內存將會清除其他內容不會 – Amitsharma 2015-02-06 13:29:42

回答

0

此代碼將幫助您存儲值

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
Editor editor = pref.edit(); 

//存儲數據的鍵/值對

editor.putBoolean("key_name1", true);   // Saving boolean - true/false 
editor.putInt("key_name2", "int value");  // Saving integer 
editor.putFloat("key_name3", "float value"); // Saving float 
editor.putLong("key_name4", "long value");  // Saving long 
editor.putString("key_name5", "string value"); // Saving string 

// Save the changes in SharedPreferences 
editor.commit(); // commit changes 

//獲取SharedPreferences數據

//如果key不存在則返回第二個參數值的方式值 - 在這種情況下,空

pref.getBoolean("key_name1", null);   // getting boolean 
pref.getInt("key_name2", null);    // getting Integer 
pref.getFloat("key_name3", null);   // getting Float 
pref.getLong("key_name4", null);   // getting Long 
pref.getString("key_name5", null);   // getting String 

//從SharedPreferences刪除鍵值

editor.remove("key_name3"); // will delete key key_name3 
editor.remove("key_name4"); // will delete key key_name4 

// Save the changes in SharedPreferences 
editor.commit(); // commit changes 

//清除SharedPreferences中的所有數據

editor.clear(); 
editor.commit(); // commit changes 
+0

做到了這一點幫助你...... – Amitsharma 2015-02-06 13:25:27

+0

鏈接更多詳細信息http://developer.android.com/reference/android/ content/SharedPreferences.html – Amitsharma 2015-02-06 13:27:01

+0

不,這是給getApplicationContext()錯誤(我正在使用片段) – user3699550 2015-02-06 13:28:34

0

嘗試刪除此行: SharedPreferences prefs = getActivity()。getSharedPreferences(「idValue」,0);

+0

它贏得了' t幫助定義SharedPreferences偏好再次絕對是涵蓋全局變量,你正在本地工作,但這不是一個問題 – 2015-02-06 13:37:22

0

試圖改變提交()到申請()是這樣的:

public void click(){ 

    SharedPreferences prefs = getActivity().getSharedPreferences("your.project.package.name", 0); // don't use short id because sharedPreferences is a global file 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putString("idValue", list.get(getArguments().getInt("pos")).toString()); 

    editor.apply(); //Here is the change 
} 

閱讀您的首選項嘗試:

SharedPreferences prefs = this.getSharedPreferences("your.project.package.name", Context.MODE_PRIVATE); 
String idValue= prefs.getString("idValue", null); 
+0

如何在數據/數據/ .. – user3699550 2015-02-06 13:41:07

+0

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME中查看SP文件。xml 但是,只有當你有根深蒂固的手機,我相信你打開它使用私人國防部。嘗試閱讀它,就像我吞下 – 2015-02-06 13:42:32

+0

是的,我使用的是private_Mode,但沒有數據文件夾中的任何內容,但我想這不是保存在文件夾 – user3699550 2015-02-06 13:48:39