Does a custom PreferenceActivity need to define a List?
If I do not define a list as part of my content associated with my custom PreferenceActivity, I get the following RuntimeException:
Your content must have a ListView whose id attribute is 'android.R.id.list'
I am trying to create a custom PreferenceActivity tha开发者_如何学Ct shows two lists:
- One list that adds selections to the other list
- One list that allows the user to move items up or down, or delete them (probably with a context menu)
What is a "preferred" way of doing this?
Thanks, wTs
PreferenceActivity extends ListActivity, so I assume it needs a List.
When I don't understand things my preferred way is to Use the Source® : PreferenceActivity
Edited:
I'd take this approach:
Copy PreferenceActivity to a new class.
Create you own layout for your Activity (take a look at ListActivity docs). Add a second ListView to it.
Make sure that all works as original PreferenceActivity.
Start adding code for the second ListView.
the problem is u probably use settings loaded from an xml file and used
setContentView(layoutId); ...
use: addPreferencesFromResource(layoutId); instead.
and you can skip the steps described by 'Peter Knego' and can directly use the Preferences without adding an listview.
you can add preference to your PreferenceScreen use for statement to add it multiple times
public void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.preferences);
PreferenceScreen preference;
preferenceScreen = getPreferenceScreen();
PreferenceScreen new_preference = new Preference(this);
new_preference.setKey("unique_key");
new_preference.setTitle("new_title");
preferenceSreen.addPreference(new_preference);
}
精彩评论