开发者

Does Custom PreferenceActivity Need To Define List

开发者 https://www.devze.com 2023-01-24 23:31 出处:网络
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:

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:

  1. One list that adds selections to the other list
  2. 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:

  1. Copy PreferenceActivity to a new class.

  2. Create you own layout for your Activity (take a look at ListActivity docs). Add a second ListView to it.

  3. Make sure that all works as original PreferenceActivity.

  4. 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消