Preference List only shows first element

Posted by jmunoz on Stack Overflow See other posts from Stack Overflow or by jmunoz
Published on 2012-03-07T15:25:22Z Indexed on 2012/03/27 11:29 UTC
Read the original article Hit count: 491

I am developing a PreferenceActivity with custom Preference views. My problem is that I created a view with a ListView and it only shows the first element. I post my code and an image: http://imageshack.us/photo/my-images/545/sc20120307161530.png/

xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory
        android:key="player_settings"
        android:title="@string/settings_player_config" >
        <EditTextPreference
            android:defaultValue="@string/settings_player_default_name"
            android:dialogMessage="@string/settings_player_summary"
            android:dialogTitle="@string/settings_playersname"
            android:key="player_name"
            android:summary="@string/settings_player_summary"
            android:title="@string/settings_playersname" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="volume"
        android:title="@string/settings_volume" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="volume"
            android:title="@string/settings_volume" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="shine"
        android:title="@string/settings_shine" >
        <com.battleship.preferences.SeekBarPreferences
            android:defaultValue="50"
            android:key="shine"
            android:title="@string/settings_shine" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="themeTitle"
        android:title="@string/settings_group_themes" >
        <com.battleship.preferences.ListPreferences android:key="theme" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="fontsTitle"
        android:title="@string/settings_group_font_size" >
        <com.battleship.preferences.ListPreferences android:key="font" />
    </PreferenceCategory>

</PreferenceScreen>

The Custom ListPreference:

package com.battleship.preferences;

import com.battleship.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class ListPreferences extends Preference implements
        OnCheckedChangeListener {

    public ListPreferences(Context context) {
        super(context);
    }

    public ListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListPreferences(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onClick() {

        super.onClick();
        Toast t = Toast.makeText(getContext(), "HOLA!", 3);
        t.show();
    }

    @Override
    protected View onCreateView(ViewGroup parent) {



        String[] contentString = new String[3];
        if (getKey().equals("theme")) {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_theme_default)),
                    (getContext().getString(R.string.settings_theme_black)),
                    (getContext().getString(R.string.settings_theme_white)) };
        } else {
            contentString = new String[] {
                    (getContext().getString(R.string.settings_font_big)),
                    (getContext().getString(R.string.settings_font_medium)),
                    (getContext().getString(R.string.settings_font_little)) };
        }

        ListView listView = new ListView(getContext());
        ArrayAdapter<String> array = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_single_choice,
                android.R.id.text1, contentString);
        listView.setAdapter(array);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


        return listView;
    }

    private void updatePreference(int intRadio) {
        SharedPreferences.Editor editor = getEditor();
        editor.putInt(getKey(), intRadio);
        editor.commit();
    }


}

© Stack Overflow or respective owner

Related posts about android

Related posts about android-listview