开发者

Android, subclass Preference class

I am tryig to write an Android Honeycomb application and I am having trouble subclassing Preference: http://developer.android.com/reference/android/preference/Preference.htm开发者_Go百科l

I want to make a similar layout with title and summary but also a progress bar.

I have created the layout and added the custom preference class but I can't seem to get hold of the instance of it to set the values of the items in it.

It seems that the preference key doesn't work for the custom class.

Here is my preference definition compared to the standard preference class:

               <Preference
                android:key="int_free_storage"
                android:title="Free Space"
                android:summary="free storage value here"/>

            <com.hamid.storageether.SpacePreference
                android:key="int_space_test"
                android:title="Test"
                android:summary="This is my custom preference"/>

My my preference subclass then sets my XML layout as it's layout resource in its constructor

setLayoutResource(R.layout.space_pref_layout);

it also overrides the setTitle and setSummary methods....

In my main PreferenceActivity I try to get hold of my Preference by it's key but no luck it seems, since the preference never gets updated:

// These Two work
Preference intTotal = (Preference)findPreference("int_total_storage");
Preference intFree = (Preference)findPreference("int_free_storage");
           intTotal.setSummary("Standard Preference Summary 1");
           intFree.setSummary("Standard Preference Summary 2");

// My subclass doesn't - It just displays the default text defined in the layout xml.
SpacePreference intTest = (SpacePreference)findPreference("int_test_space");
            intTest.setTitle("Testtttyyy");
            intTest.setSummary("Test Summary");

Could someone please point me towards where I may be going wrong?


Is this code copied straight from the program or retyped? If copied, then your key is "int_space_test" in XML and "int_test_space" in code. It should be throwing a null pointer exception on the next line where you use intTest if that's the case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜