如何在共享首选项中存储字符串数组?

时间:2014-08-04 15:17:42

标签: android

如何在SharedPreferences中存储String数组?请给我一个示例代码,用于存储数组的几个名称,并在另一个活动中检索它。提前致谢

3 个答案:

答案 0 :(得分:1)

让我试着帮助你

首先在要使用SharedPreferences的活动中设置这些类变量

public static String MY_PREFS = "MY_PREFS";
private SharedPreferences mySharedPreferences;
int prefMode = Activity.MODE_PRIVATE;

然后存储像这样的字符串值

SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("key1", "value1");
editor.putString("key2", "value2");
editor.putString("key3", "value3");

editor.commit(); // to persist the values between activities

并且最终访问另一个活动中的sharedPreferences使用此

mySharedPreferences = getSharedPreferences(MY_PREFS, prefMode);
String string1 = mySharedPreferences.getString("key1", null);
String string2 = mySharedPreferences.getString("key2", null);

希望这对你有所帮助。

答案 1 :(得分:0)

不支持数组。但是,您可以存储Set。

    SharedPreferences prefs = parent.getContext().getSharedPreferences(
            "com.example.app", Context.MODE_PRIVATE);
    Set<String> values = new HashSet<String>();
    values.add("value 1");
    values.add("value 2");
    prefs.edit().putStringSet("myKey", values).commit();

答案 2 :(得分:0)

第一项活动。

public class MainActivity extends ActionBarActivity {

        private Button button;
        private SharedPreferences preferences;
        private String[] name = {"aa", "bb", "cc"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button=(Button)findViewById(R.id.button1);
            preferences=getSharedPreferences("testarray", MODE_PRIVATE);
            for(int i=0;i<3;i++)
            {
                SharedPreferences.Editor editor=preferences.edit();
                editor.putString("str"+i, name[i]);
                editor.commit();
            }
            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this,Secondact.class));               
                }
            });
        }

    }

第二项活动

public class Secondact extends ActionBarActivity {

    private Button button;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        preferences = getSharedPreferences("testarray", MODE_PRIVATE);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                for(int i=0;i<3;i++)
                {
                    Log.d("aa", preferences.getString("str"+i, " "));
                }

            }
        });
    }
}

使用字符串集

public class MainActivity extends ActionBarActivity {

    private Button button;
    private SharedPreferences preferences;
    private String[] name = {"aa", "bb", "cc"};
    Set<String> values = new HashSet<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button1);
        preferences=getSharedPreferences("testarray", MODE_PRIVATE);
        for(int i=0;i<3;i++)
        {
            values.add(name[i]);
        }

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                for(int i=0;i<3;i++)
                {

                    SharedPreferences.Editor editor=preferences.edit();
                    editor.putStringSet("str", values);
                    editor.commit();
                }
                startActivity(new Intent(MainActivity.this,Secondact.class));               
            }
        });
    }

}


public class Secondact extends ActionBarActivity {

    private Button button;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        preferences = getSharedPreferences("testarray", MODE_PRIVATE);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Set<String> values = preferences.getStringSet("str", null);

                String[] name = values.toArray(new String[values.size()]);
                for (int i = 0; i < values.size(); i++) {
                    Log.d("aa", name[i]);
                }

            }
        });
    }
}