检索另一个活动中的sharedpreferences值

时间:2011-11-16 17:57:56

标签: android android-activity sharedpreferences if-statement

任何人都可以告诉我哪里错了,因为它只是一个简单的过程,但它如何检索我无法理解的值并对字符串变量进行条件检查......

活动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: - 在此活动中,我正在访问这些值并进行条件检查,但只有在两种情况下都会执行条件

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

3 个答案:

答案 0 :(得分:4)

您应该使用editor.commit();

提交数据

注意:在你的情况下,不需要使用很多SharedPreferences的实例,只需要一个实例,然后将所有的字符串放入其中,并提交

答案 1 :(得分:2)

你应该使用

editor.commit();

要保留您的更改。而且,您不需要使用不同的文件来存储各种字符串,您可以将所有字符串存储在同一个共享首选项文件中。

答案 2 :(得分: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();
    }

活动A: -

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 ..