2011-11-16 45 views
3

誰能告訴我哪裏錯了,因爲它只是一個簡單的過程,但它怎麼不檢索值,我不能明白,做了字符串變量條件檢查...另一個動作檢索共享prefrences值

活動A : -

EditText e = (EditText) findViewById(R.id.editText1); 
     EditText e1 = (EditText) findViewById(R.id.editText2); 
     EditText e2 = (EditText) findViewById(R.id.editText3); 
     EditText e3 = (EditText) findViewById(R.id.editText4); 
     EditText e4 = (EditText) findViewById(R.id.editText5); 
     EditText e5 = (EditText) findViewById(R.id.editText6); 
     EditText e6 = (EditText) findViewById(R.id.editText7); 

     SharedPreferences myPrefs = getSharedPreferences("myPrefs", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor = myPrefs.edit(); 
     editor.putString("text", e.getText().toString()); 
     SharedPreferences myPrefs1 = getSharedPreferences("myPrefs1", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor1 = myPrefs1.edit(); 
     editor1.putString("text1", e1.getText().toString()); 

    SharedPreferences myPrefs2 = getSharedPreferences("myPrefs2", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor2 = myPrefs.edit(); 
     editor2.putString("text2", e2.getText().toString()); 

SharedPreferences myPrefs3 = getSharedPreferences("myPrefs3", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor3 = myPrefs3.edit(); 
     editor3.putString("text3", e3.getText().toString()); 

SharedPreferences myPrefs4 = getSharedPreferences("myPrefs4", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor4 = myPrefs4.edit(); 
     editor4.putString("text4", e4.getText().toString()); 

     SharedPreferences myPrefs5 = getSharedPreferences("myPrefs5", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor5 = myPrefs5.edit(); 
     editor5.putString("text5", e5.getText().toString()); 

     SharedPreferences myPrefs6 = getSharedPreferences("myPrefs6", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor6 = myPrefs6.edit(); 
     editor6.putString("text6", e6.getText().toString()); 

活動B: - 在這個活動我正在訪問的值,做條件檢查,但在這兩種情況下是越來越只執行else條件

public class CheckActivity extends Activity{ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     SharedPreferences myPrefs = getSharedPreferences("myPrefs",Context.MODE_PRIVATE); 
     String restoredText = myPrefs.getString("text", ""); 
     SharedPreferences myPrefs1 = getSharedPreferences("myPrefs1",Context.MODE_PRIVATE); 
     String restoredText1 = myPrefs1.getString("text1", ""); 
     SharedPreferences myPrefs2 = getSharedPreferences("myPrefs2",Context.MODE_PRIVATE); 
     String restoredText2 =myPrefs2.getString("text2", ""); 
     SharedPreferences myPrefs3 = getSharedPreferences("myPrefs3",Context.MODE_PRIVATE); 
     String restoredText3 =myPrefs3.getString("text3", ""); 
     SharedPreferences myPrefs4 = getSharedPreferences("myPrefs4",Context.MODE_PRIVATE); 
     String restoredText4 = myPrefs4.getString("text4", ""); 
     SharedPreferences myPrefs5 = getSharedPreferences("myPrefs5",Context.MODE_PRIVATE); 
     String restoredText5 = myPrefs5.getString("text5", ""); 
     SharedPreferences myPrefs6 = getSharedPreferences("myPrefs6",Context.MODE_PRIVATE); 
     String restoredText6 = myPrefs6.getString("text6", ""); 
    Intent i1 = new Intent(); 
    if((restoredText.length()>1)&&(restoredText1.length()>1)&&(restoredText2.length()>1)&&(restoredText3.length()>1)&&(restoredText4.length()>1)&&(restoredText5.length()>1)&&(restoredText6.length()>1)) 
     { 
      i1.setClass(this,ShpoonkleActivity.class); 
     } 
     //if((restoredText.length()==0)||(restoredText1.length()==0)||(restoredText2.length()==0)||(restoredText3.length()==0)||(restoredText4.length()==0)||(restoredText5.length()==0)||(restoredText6.length()==0)) 
    else  
    { 
      i1.setClass(this,Test.class); 
    } 
     startActivity(i1); 
     finish(); 
    } 
} 

回答

0

實際保存的EditText的文本是模棱兩可,因此沒有執行好,所以我這樣做,如下圖所示: -

活動B: -

protected void onPause() { 
     EditText ed = (EditText)findViewById(R.id.editText1); 
     EditText ed1 = (EditText)findViewById(R.id.editText2); 
     EditText ed2 = (EditText)findViewById(R.id.editText3); 
     EditText ed3 = (EditText)findViewById(R.id.editText4); 
     EditText ed4 = (EditText)findViewById(R.id.editText5); 
     EditText ed5 = (EditText)findViewById(R.id.editText6); 
     EditText ed6 = (EditText)findViewById(R.id.editText7); 

     super.onPause(); 
     SharedPreferences myPrefs = getSharedPreferences("myPrefs", Context.MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor = myPrefs.edit(); 
     editor.putString("text", ed.getText().toString()); 
     editor.putString("text1", ed1.getText().toString()); 
     editor.putString("text2", ed2.getText().toString()); 
     editor.putString("text3", ed3.getText().toString()); 
     editor.putString("text4", ed4.getText().toString()); 
     editor.putString("text5", ed5.getText().toString()); 
     editor.putString("text6", ed6.getText().toString()); 
     editor.commit(); 
    } 

活動答: -

public class CheckActivity extends Activity{ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     SharedPreferences myPrefs = getSharedPreferences("myPrefs",Context.MODE_PRIVATE); 
     String restoredText = myPrefs.getString("text", ""); 
     String restoredText1 = myPrefs.getString("text1", ""); 
     String restoredText2 =myPrefs.getString("text2", ""); 
     String restoredText3 =myPrefs.getString("text3", ""); 
     String restoredText4 = myPrefs.getString("text4", ""); 
     String restoredText5 = myPrefs.getString("text5", ""); 
     String restoredText6 = myPrefs.getString("text6", ""); 
    Intent i1 = new Intent(); 
    if((restoredText.length()>0)&&(restoredText1.length()>0)&&(restoredText2.length()>0)&&(restoredText3.length()>0)&&(restoredText4.length()>0)&&(restoredText5.length()>0)&&(restoredText6.length()>0)) 
     { 
      i1.setClass(this,ShpoonkleActivity.class); 
     } 
     //if((restoredText.length()==0)||(restoredText1.length()==0)||(restoredText2.length()==0)||(restoredText3.length()==0)||(restoredText4.length()==0)||(restoredText5.length()==0)||(restoredText6.length()==0)) 
    else  
    { 
      i1.setClass(this,Test.class); 
    } 
     startActivity(i1); 
     finish(); 
    } 
} 

和它的作品真棒... ..日Thnx

2

您應該使用

editor.commit(); 

您的更改將被保留。而且,您不需要使用不同的文件來存儲各種字符串,您可以將所有字符串存儲在相同的共享首選項文件中。

+0

日Thnx爲我糾正這個錯誤不是也仍得到執行我的其他條件.. –

+0

你當然,您提供了每個editText的有效輸入,即每個editText中至少有2個字符? ,我想,你應該檢查字符串長度是否大於0,而不是1,但我也可能是錯的。 –

4

你應該editor.commit();

NB提交您的數據:在你的情況,也沒有必要使用大量的SharedPreferences情況下,你只需要一個實例,然後把你所有的字符串進去,並承諾

+0

thnx因爲我糾正了這個錯誤,也還是我的其他條件正在執行.. –

+0

+1(對於「很多實例」)我認爲他根本不需要SharedPreferences,但他不知道如何使用額外在意圖:) ...... – Selvin

+0

@Selvin:我不知道他是否需要重新啓動應用程序後能夠得到這些字符串,或者只是將它傳遞給另一個活動,在這種情況下,正如你所說,他只需要使用附加的意圖:) – Houcine

相關問題