سؤال

I have implemented a custom ListPreference and managed to load a list of items along with checkboxes for each without an issue. However, I need to add a “Select All” checkbox on top in order to select all list of items. How would I achieve this with the following source I have implemented?

The layout:

<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:title="@string/Title_LOCATIONS">       

        <com.gm.settings.LocationsListPreference         
            android:defaultValue="null"
            android:key="list_locations"
            android:title="@string/LocationsListPreference_title" 
            android:dialogTitle="@string/LocationsListPreference_title"
            android:summary="@string/LocationsListPreference_summary"
        />

The class:

public class LocationsListPreference extends ListPreference {
}

I have implemented the class by following a tutorial and it works fine. But it uses a default layout i think and if I were to add this addition checkbox, how would I achieve this?

Update:

I want to know as to how i can add the "Select All" checkbox to the layout? Or should i create a custom layout? Please provide a sample code. (Because i feel the way it is right now, i dont have the control over this checkbox)

هل كانت مفيدة؟

المحلول 2

Found a stackoverflow post which might help others if they come across this kind of implementation:

You can build your custom ListPreference layout.

Cheers!

نصائح أخرى

What you could do is add a CheckBoxPreference in your PreferenceCategory and attach to it a OnPreferenceChangedListener that sets all of the values to being checked.

An example could probably look a little something like this:

    <CheckBoxPreference
        android:key="select_all"
        android:defaultValue="false"
        android:title="Select All"
    />


    <com.gm.settings.LocationsListPreference         
        android:defaultValue="null"
        android:key="list_locations"
        android:title="@string/LocationsListPreference_title" 
        android:dialogTitle="@string/LocationsListPreference_title"
        android:summary="@string/LocationsListPreference_summary"
    />

And then in your PreferenceFragment (or PreferenceActivity), you would have the following:

SharedPreferences shareprefs = getPreferenceManager().getSharedPreferences();
LocationsListPreference listPreference = getPreference("list_locations");
CheckBoxPreference selectAll = getPreference("select_all");

selectAll.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
{
    public boolean onPreferenceChanged(Preference preference, Object newValue) 
    {
        //Do something with your listPreference and/or your sharedPrefs
    }
}

Hope this helps, and if you get to a road block, I think this post does a slightly better job at explaining some of the concepts. Good luck!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top