从收集共享preference GETALL()方法的所有字符串? [英] Gather all Strings from SharedPreference getAll() Method?

查看:230
本文介绍了从收集共享preference GETALL()方法的所有字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何设置使用该GETALL()方法,这样我就可以使用这些值来命名填充按钮和分配这些按钮来访问我的应用程序的共享preferences每个字符串和键名共享preferences适当的字符串在一个TextView显示。

问题是我不知道如何,因为我没有以前处理映射到这些值保存到一个字符串或字符串数​​组。

如果有人可以帮助我我的共享preferences键和字符串值存储到字符串数组在我的片段使用,这将是AP preciated。

下面是我到目前为止有:

 的String [] arrayToStoreStrings;公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    共享preferences SP =(共享preferences)MadlibsSaved.this.getActivity();
    地图<字符串,>键= sp.getAll();    为(?Map.Entry的<弦乐,>项:keys.entrySet()){
        Log.d(映射值,entry.getKey()+:+
                               。entry.getValue()的toString());
}

更新了更多的努力,但仍然得到错误:

  BootstrapButton [] loadButtons;公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    共享preferences SP = NULL;
    地图<字符串,>键= sp.MadlibsSaved.this.getActivity()GETALL()。    为(?Map.Entry的<弦乐,>项:keys.entrySet()){
        Log.d(映射值,entry.getKey()+:+
                               。entry.getValue()的toString());        而(!(值[I] .matches(NULL))){
            。值[I] = entry.getValue()的toString();
            我++;
        }
        而(!(关键[I] .matches(NULL))){
            键[I] = entry.getKey()的toString();
            我++;
        } }    loadButtons =新BootstrapButton [20];    loadButtons [0] =新BootstrapButton(getActivity());
    loadButtons [0] .setText(值[I]);


解决方案

如果您只想字符串值:

 的ArrayList<串GT;串=新的ArrayList<串GT;();
为(?Map.Entry的<弦乐,>项:keys.entrySet()){
    如果(entry.getValue()的instanceof字符串){
        strings.add((字符串)entry.getValue());
    }
}
arrayToStoreStrings = strings.toArray(新的String [strings.size()]);

如果你想的每一个值转换为字符串

 的ArrayList<串GT;串=新的ArrayList<串GT;();
为(?Map.Entry的<弦乐,>项:keys.entrySet()){
    strings.add(entry.getValue()的toString());
}
arrayToStoreStrings = strings.toArray(新的String [strings.size()]);

可以做为地图键类似的方法(而不是映射的值)在一个单独的阵列

I'm trying to figure out how to set every String and Key name from my app's SharedPreferences using the getAll() method so I can use those values to name populated buttons and assign those buttons to the access the proper string in sharedpreferences to display in a textview.

The problem is I do not know how to save these values to a String or String Array because I haven't ever dealt with maps before.

If anybody could help me store my SharedPreferences keys and string values to a String array for use in my fragment that would be appreciated.

Here is what I have so far:

String[] arrayToStoreStrings;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    SharedPreferences sp = (SharedPreferences) MadlibsSaved.this.getActivity();
    Map<String,?> keys = sp.getAll();

    for(Map.Entry<String,?> entry : keys.entrySet()){
        Log.d("map values",entry.getKey() + ": " + 
                               entry.getValue().toString());            
}

UPDATED with more effort but still getting errors:

BootstrapButton[] loadButtons;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    SharedPreferences sp = null;
    Map<String,?> keys = sp.MadlibsSaved.this.getActivity().getAll();

    for(Map.Entry<String,?> entry : keys.entrySet()){
        Log.d("map values",entry.getKey() + ": " + 
                               entry.getValue().toString());

        while(!(values[i].matches(null))){
            values[i] = entry.getValue().toString();
            i++;
        }
        while(!(key[i].matches(null))){
            key[i] = entry.getKey().toString();
            i++;
        }

 }

    loadButtons = new BootstrapButton[20];

    loadButtons[0] = new BootstrapButton(getActivity());
    loadButtons[0].setText(values[i]);

解决方案

If you want only the String values:

ArrayList<String> strings = new ArrayList<String>();
for(Map.Entry<String,?> entry : keys.entrySet()){
    if (entry.getValue() instanceof String) {
        strings.add((String) entry.getValue());
    }
}
arrayToStoreStrings = strings.toArray(new String[strings.size()]);

If you want to convert every value to a String:

ArrayList<String> strings = new ArrayList<String>();
for(Map.Entry<String,?> entry : keys.entrySet()){
    strings.add(entry.getValue().toString());
}
arrayToStoreStrings = strings.toArray(new String[strings.size()]);

You can do a similar approach for the map keys (as opposed to map values) in a separate array.

这篇关于从收集共享preference GETALL()方法的所有字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆