【问题标题】:getString from a few SharedPreferences file and create listView从几个 SharedPreferences 文件中获取字符串并创建 listView
【发布时间】:2012-02-15 21:42:17
【问题描述】:

首先 - 对不起我的英语;)我正在为 Android 编写配置文件管理器,我想从几个 SharedPreferences 文件中获取 getString 并创建 listView。这是我的代码的一部分:

private static final String PN = "profile_name";
private EditTextPreference editText;
private SharedPreferences preferences;

public class Profile_Preferences extends PreferenceActivity {

...

private void SavePreferences() {

    String text= editText.getText().toString();
    preferences = getSharedPreferences("Profile_" + text, Activity.MODE_PRIVATE);  //Here we created SharedPreferences file with name "Profile_"+ this what user write in editText

    SharedPreferences.Editor preferencesEditor = preferences.edit();
    preferencesEditor.putString(PN, editText.getText());
    preferencesEditor.commit();

好的。用户正在做一些配置文件,所以我们有文件,例如:Profile_home.xml、Profile_work.xml、Profile_something.xml。现在我不想使用该文件中的配置文件名称创建 listView。这是我的下一个活动:

public class Tab_profiles extends ListActivity {

ArrayList<String> listItems = new ArrayList<String>();  
ArrayAdapter<String> adapter; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, listItems);
    setListAdapter(adapter);
 ...

public void LoadList() {        

 //It's a good idea to getString "profile_name" form every xml file with name "Profile_" + "" ? 
    preferences = getSharedPreferences("Profile_"+ "", Activity.MODE_PRIVATE);

    String take_profile_name = preferences.getString("profile_name", null);
    listItems.add(take_profile_name );
    adapter.notifyDataSetChanged();

}

但这不起作用... logcat 日志:

FATAL EXCEPTION: MAIN 
java.land.Null.PointerException
at android.widget.ArrayAdaper.createViewFromResource(ArrayAdapter.java:355)
...

我现在不知道这是错的......

请帮助我:)感谢您的任何回答,并对我在写作和代码方面的错误表示歉意;)

【问题讨论】:

    标签: android listview sharedpreferences profiles getstring


    【解决方案1】:

    你的代码有几个问题:

    String text = editText.getText().toString();
    preferences = getSharedPreferences("Profile_" + text, Activity.MODE_PRIVATE);
    
    SharedPreferences.Editor preferencesEditor = preferences.edit();
    preferencesEditor.putString(PN, editText.getText());
    

    如果用户在 EditText 中键入“home”,您会创建一个名为“Profile_home”的首选项文件并将其名称保存在同一个文件中吗?您需要将用户生成的文件名保存在另一个您知道名称的文件中,以便您可以在代码中访问它。

    第二个问题:

    preferences = getSharedPreferences("Profile_" + "", Activity.MODE_PRIVATE);
    String take_profile_name = preferences.getString("profile_name", null);
    

    您正在尝试打开“Profile_”设置。该文件不存在(?)。 getString 的第二个参数不能为空,否则您将向字符串列表添加空引用,这将失败。用空字符串“”替换它。

    编辑:这是获取所有自定义命名首选项的小解决方法:

    private void SavePreferences() {
        String text = editText.getText().toString();
        preferences = getSharedPreferences("ProfileNames", Activity.MODE_PRIVATE);
        SharedPreferences.Editor preferencesEditor = preferences.edit();
        // increment index by 1
        preferencesEditor.putInt("profile_count", preferences.getInt("profile_count", 0) + 1);
        // save new name in ProfileNames.xml with key "name[index]"
        preferencesEditor.putString("name" + (preferences.getInt("profile_count", 0) + 1), editText.getText());
        preferencesEditor.commit();
    }
    
    public void LoadList() {        
        preferences = getSharedPreferences("ProfileNames", Activity.MODE_PRIVATE);
        List<String> profileNames = new LinkedList<String>();
        int profileCount = preferences.getInt("profile_count", 0);
        for (int i = 1; i <= profileCount; i++) {
            profileNames.add(preferences.getString(name + i, ""));
        }
        listItems.addAll(profileNames);
        adapter.notifyDataSetChanged();
    }
    

    【讨论】:

    • 是的。你有权利。此代码尚未完成,但首先我不想查看带有配置文件名称的 listView。如果我在第二个参数中将 null 替换为 getString 中的空字符串“”,则会显示 listView 并且 logcat 没有错误但 listView 不显示任何值...
    • 您的 listView 不包含任何值,因为您在 LoadList() 中打开的文件 Profile_.xml 不包含键为“profile_name”的字符串,因此返回“”。例如Profile_USERINPUT.xml,其中包含键“profile_name”的“USERINPUT”。
    • 如果我们创建一些配置文件,例如:“Home”,我们会生成 xml 文件“Profile_home.xml”,并且这里有键“profile_name”。但是如何搜索所有 sharedPreferences xml 文件并在 listView 中查看“profile_name”?如何从“Profile_”开始搜索所有文件? getSharedPreferences("Profile_" + "", Activity.MODE_PRIVATE) 不好? H
    • "Profile_" + "" = "Profile_",您只是添加了一个空字符串,这并不意味着您将获得所有自定义名称。我在答案中添加了一些代码,您如何将所有自定义名称保存在一个 .xml(ProfileNames)中。希望对您有所帮助。
    • 你的代码和解决方案要好得多!非常感谢您的帮助和您的时间!现在一切都很好!你跟我喝啤酒!!!:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多