Java 正在获取所选项目的listPreference。安卓
我正在运行一个服务。在这方面,我想阅读启动它的应用程序的首选项。虽然我能够阅读其他偏好。我无法读取列表首选项中的选定项。请帮忙。欢迎提供代码或相关链接。Java 正在获取所选项目的listPreference。安卓,java,android,android-preferences,android-sharedpreferences,Java,Android,Android Preferences,Android Sharedpreferences,我正在运行一个服务。在这方面,我想阅读启动它的应用程序的首选项。虽然我能够阅读其他偏好。我无法读取列表首选项中的选定项。请帮忙。欢迎提供代码或相关链接。 提前感谢。在文件夹/res/values/ <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="listDisplayWord"> <item>Option 1</item&g
提前感谢。在文件夹/res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listDisplayWord">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
</string-array>
<string-array name="listReturnValue">
<item>1 is selected</item>
<item>2 is selected</item>
<item>3 is selected</item>
</string-array>
</resources>
主代码
package com.exercise.AndroidListPreference;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidListPreference extends Activity {
TextView myListPref;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListPref = (TextView)findViewById(R.id.list_pref);
Button buttonSetPreference = (Button)findViewById(R.id.setpreference);
buttonSetPreference.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(AndroidListPreference.this, SetPreferences.class));
}});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
String myListPreference = myPreference.getString("listPref", "default choice");
myListPref.setText(myListPreference);
}
}
还必须修改AndroidManifest.xml以包含SetPreferences.java
package com.exercise.AndroidListPreference;
import com.exercise.AndroidListPreference.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class SetPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidListPreference"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidListPreference"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SetPreferences" />
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
起初,我认为可以使用PreferenceManager.GetDefaultSharedReferences(this)获取SharedReferences对象,然后调用GetString方法,并将key作为ListPreference的参数。然而,它没有起作用。我尝试获取此首选项的编辑器对象,然后使用它获取选定的值。但是,它只提供,个方法来设置内容。在所有布局文件中,您都没有提到ListPreference的id。但你在findViewById中提到过。键与id相同。另外,我想在我的服务中读取这些首选项,没有此名称的findViewById方法。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidListPreference"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidListPreference"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SetPreferences" />
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>