列表中的preference自定义行? [英] custom row in a listPreference?

查看:113
本文介绍了列表中的preference自定义行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个列表preference但不知何故取消的项目之一。有点像灰色出来什么,而不是有选择的能力。这将是即将到来的特点,我想这是在列表中只是不可选。

我创建了一个自定义列表preference类,并在该类自定义适配器,希望使用适配器创建了我想要的。

在code工作,并设置了接口,但没有适配器的功能调用。我设置的方法,如getCount将(断点),但他们从来没有被调用。

下面是我的code。自定义列表preference取自 http://blog.350nice.com/wp/archives/240

 进口android.content.Context;
进口android.content.DialogInterface;
进口android.graphics.Color;
进口的Andr​​oid preference.List preference。
进口android.util.AttributeSet;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.app.AlertDialog.Builder;

公共类CustomList preference扩展列表preference {

    私人布尔[] mClickedDialogEntryIndi​​ces;
    CustomList preferenceAdapter customList preferenceAdapter = NULL;
    语境mContext;

    公共CustomList preference(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        mContext =背景;
        mClickedDialogEntryIndi​​ces =新的布尔[get​​Entries()的长度。]。
    }

    @覆盖
    在prepareDialogBu​​ilder(生成器生成器)保护无效{
        CharSequence的[]项= getEntries();
        为CharSequence [] entryValues​​ = getEntryValues​​();
        如果(项目== NULL || entryValues​​ == NULL
                || entries.length!= entryValues​​.length){
            抛出新IllegalStateException异常(
                    目录preference需要输入数组
                    +和一个entryValues​​阵列这两者都是相同的长度);
        }
        builder.setMultiChoiceItems(条目,mClickedDialogEntryIndi​​ces,
                新DialogInterface.OnMultiChoiceClickListener(){

                    公共无效的onClick(DialogInterface对话,诠释它,
                            布尔VAL){
                        mClickedDialogEntryIndi​​ces [这] = VAL;
                    }
                });
        //设置我的自定义列表适配器
        customList preferenceAdapter =新CustomList preferenceAdapter(mContext);
        builder.setAdapter(customList preferenceAdapter,NULL);
    }

    私有类CustomList preferenceAdapter延伸BaseAdapter {

        公共CustomList preferenceAdapter(上下文的背景下){}

        公众诠释getCount将(){
            返回1;
        }

        公共对象的getItem(INT位置){
            返回的位置;
        }

        众长getItemId(INT位置){
            返回的位置;
        }

        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            convertView.setBackgroundColor(Color.BLUE);
            返回convertView;
        }
    }
}
 

解决方案

确定我得到这个工作,大多是。我不得不使用扩展列表preference自定义定义的类。然后那里面我不得不创建一个自定义的适配器类,就像你的一个ListView并将其设置为使用builder.setAdapter()建造。我也有过定义监听器为单选按钮,并且办理了取消选中的单选按钮,并在这样的ListView行。唯一的问题,我仍然有是,我的自定义列表preference同时具有确定和取消按钮,其中一个列表preference只有取消按钮。我不知道如何删除OK按钮。此外,我不能让行突出,当我点击他们就像他们在常规列表preference。

在Java code自定义列表preference类。一定要想到像你的包名,preference名称(键),您的输入和值表preference,和你的XML项目的名称。

 包your.package.here;

进口的java.util.ArrayList;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Shared preferences;
进口android.graphics.Color;
进口的Andr​​oid preference.List preference。
。进口的Andr​​oid preference preferenceManager。
进口android.util.AttributeSet;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.CompoundButton;
进口android.widget.RadioButton;
进口android.widget.TextView;
进口android.app.Dialog;
进口android.app.AlertDialog.Builder;

公共类CustomList preference扩展列表preference
{
    CustomList preferenceAdapter customList preferenceAdapter = NULL;
    语境mContext;
    私人LayoutInflater mInflater;
    CharSequence的[]项;
    为CharSequence [] entryValues​​;
    ArrayList的<单选> rButtonList;
    共享preferences preFS;
    共享preferences.Editor编辑;

    公共CustomList preference(上下文的背景下,ATTRS的AttributeSet)
    {
        超(背景下,ATTRS);
        mContext =背景;
        mInflater = LayoutInflater.from(上下文);
        rButtonList =新的ArrayList<单选>();
        preFS = preferenceManager.getDefaultShared preferences(mContext);
        编辑= prefs.edit();
    }

    @覆盖
    在prepareDialogBu​​ilder保护无效(生成器生成器)
    {
        条目= getEntries();
        entryValues​​ = getEntryValues​​();

        如果(项目== NULL || entryValues​​ == NULL || entries.length!= entryValues​​.length)
        {
            抛出新IllegalStateException异常(
                    列出preference需要一个条目阵列和entryValues​​阵列是相同的长度都);
        }

        customList preferenceAdapter =新CustomList preferenceAdapter(mContext);

        builder.setAdapter(customList preferenceAdapter,新DialogInterface.OnClickListener()
        {
            公共无效的onClick(DialogInterface对话,诠释其)
            {

            }
        });
    }

    私有类CustomList preferenceAdapter延伸BaseAdapter
    {
        公共CustomList preferenceAdapter(上下文的背景下)
        {

        }

        公众诠释getCount将()
        {
            返回entries.length;
        }

        公共对象的getItem(INT位置)
        {
            返回的位置;
        }

        众长getItemId(INT位置)
        {
            返回的位置;
        }

        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
        {
            查看排= convertView;
            CustomHolder支架=无效;

            如果(行== NULL)
            {
                行= mInflater.inflate(R.layout.custom_list_ preference_row,父母,假);
                持有人=新CustomHolder(行,位置);
                row.setTag(保持器);

                //做任何你所需要的,对我来说,我想最后一个项目被灰色,无法点击
                如果(位置!= 3)
                {
                    row.setClickable(真正的);
                    row.setOnClickListener(新View.OnClickListener()
                    {
                        公共无效的onClick(视图v)
                        {
                            为(单选RB:rButtonList)
                            {
                                如果(rb.getId()!=位置)
                                    rb.setChecked(假);
                            }

                            INT指数=位置;
                            int值= Integer.valueOf((字符串)entryValues​​ [指数]);
                            editor.putInt(你的preF,值);

                            对话框mDialog = getDialog();
                            mDialog.dismiss();
                        }
                    });
                }
            }

            返回行;
        }

        类CustomHolder
        {
            私人TextView的文本= NULL;
            私人单选rButton = NULL;

            CustomHolder(查看行,INT位置)
            {
                文=(TextView的)row.findViewById(R.id.custom_list_view_row_text_view);
                text.setText(项[位置]);
                rButton =(单选)row.findViewById(R.id.custom_list_view_row_radio_button);
                rButton.setId(位置);

                //再次做任何你需要的话,对我来说,我想这个项目显示为灰色和无法点击
                如果(位置== 3)
                {
                    text.setTextColor(Color.LTGRAY);
                    rButton.setClickable(假);
                }

                //还需要做些什么来检查你的preference并设置正确的按钮,选中

                rButtonList.add(rButton);
                rButton.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener()
                {
                    公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked)
                    {
                        如果(器isChecked)
                        {
                            为(单选RB:rButtonList)
                            {
                                如果(RB!= buttonView)
                                    rb.setChecked(假);
                            }

                            INT指数= buttonView.getId();
                            int值= Integer.valueOf((字符串)entryValues​​ [指数]);
                            editor.putInt(你的preF,值);

                            对话框mDialog = getDialog();
                            mDialog.dismiss();
                        }
                    }
                });
            }
        }
    }
}
 

在XML我的preferenceActivity。这不是我完整的XML,拿出了人我的其他preference项目为简单起见。再次,一定要想到包名称,自定义列表preference类必须由包名称引用。此外介意的preference和名称持有的条目和值的数组名。

 < XML版本=1.0编码=UTF-8&GT?;

< preferenceScreen
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

        < preferenceCategory
                机器人:标题=你的标题>

                < your.package.here.CustomList preference
                    机器人:关键=你的preF
                    机器人:标题=你的标题
                    机器人:dialogTitle =你的标题
                    机器人:总结=你的摘要
                    安卓:设置defaultValue =1
                    机器人:项=@阵列/ yourArray
                    机器人:entryValues​​ =@阵列/ yourValues​​/>

        < / preferenceCategory>
< / preferenceScreen>
 

我的XML的对话框的列表视图行。在getView方法,一定要使用的名称此XML文件在这一膨胀行。

 < XML版本=1.0编码=UTF-8&GT?;

<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:paddingBottom会=8DIP
    机器人:paddingTop =8DIP
    机器人:以下属性来=10dip
    机器人:paddingRight =10dip>

    < TableLayout
        机器人:ID =@ + ID / custom_list_view_row_table_layout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:stretchColumns =0>

        <的TableRow
            机器人:ID =@ + ID / custom_list_view_row_table_row
            机器人:重力=center_vertical
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT>

            <的TextView
                机器人:ID =@ + ID / custom_list_view_row_text_view
                机器人:TEXTSIZE =22sp
                机器人:文字颜色=#000000
                机器人:重力=center_vertical
                机器人:layout_width =160dip
                机器人:layout_height =40dip/>

            <单选按钮
                机器人:检查=假
                机器人:ID =@ + ID / custom_list_view_row_radio_button/>
        < /的TableRow>
    < / TableLayout>

< / LinearLayout中>
 

最后,在RES /值这里是我的array.xml包含条目名称和值表preference。此外,缩短了矿井的简易性。

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <字符串数组名=yourArray>
        <项目>项目1< /项目>
        <项目>项目2'; /项目>
        <项目>项目第3版; /项目>
        <项目>项目4℃/项目>
    < /字符串数组>

    <字符串数组名=yourValues​​>
        <项目> 0℃/项目>
        <项目> 1< /项目>
        <项目> 2'; /项目>
        <项目>第3版; /项目>
    < /字符串数组>
< /资源>
 

I am trying to create a ListPreference but somehow disable one of the items. Sort of like gray it out or something and not have the ability to choose it. It will be an upcoming feature and I want it to be in the list just not selectable.

I have created a custom ListPreference class and in that class a custom adapter, hoping to use the adapter to create what I want.

The code works, and it sets the adapter, but none of the adapter functions get called. I set breakpoints on the methods, such as getCount() but they never get called.

Here's my code. Custom ListPreference taken from http://blog.350nice.com/wp/archives/240

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.app.AlertDialog.Builder;

public class CustomListPreference extends ListPreference {

    private boolean[] mClickedDialogEntryIndices;
    CustomListPreferenceAdapter customListPreferenceAdapter = null;
    Context mContext;

    public CustomListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mClickedDialogEntryIndices = new boolean[getEntries().length];
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        CharSequence[] entries = getEntries();
        CharSequence[] entryValues = getEntryValues();
        if (entries == null || entryValues == null
                || entries.length != entryValues.length) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array "
                    +"and an entryValues array which are both the same length");
        }
        builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices,
                new DialogInterface.OnMultiChoiceClickListener() {

                    public void onClick(DialogInterface dialog, int which,
                            boolean val) {
                        mClickedDialogEntryIndices[which] = val;
                    }
                });
        // setting my custom list adapter
        customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);
        builder.setAdapter(customListPreferenceAdapter, null);
    }

    private class CustomListPreferenceAdapter extends BaseAdapter {

        public CustomListPreferenceAdapter(Context context) {}

        public int getCount() {
            return 1;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            convertView.setBackgroundColor(Color.BLUE);
            return convertView;
        }
    }
}

解决方案

OK I got this to work, mostly. I had to use a custom defined class that extends ListPreference. Then inside of that I had to create a custom adapter class just like you would for a ListView and set it to the builder using builder.setAdapter(). I also had to define listeners for both the radio buttons and the ListView rows that handled unchecking of the radio buttons and such. The only issues I still have are, my custom ListPreference has both an OK and a Cancel button where a ListPreference only has the cancel button. I don't know how to remove the OK button. Also, I can't get the rows to highlight when I click on them like they do in a regular ListPreference.

The java code for the custom ListPreference class. Be sure to mind things like your package name, the preference name (key), your entries and values for the ListPreference, and the names of your xml items.

package your.package.here;

import java.util.ArrayList;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.app.Dialog;
import android.app.AlertDialog.Builder;

public class CustomListPreference extends ListPreference
{   
    CustomListPreferenceAdapter customListPreferenceAdapter = null;
    Context mContext;
    private LayoutInflater mInflater;
    CharSequence[] entries;
    CharSequence[] entryValues;
    ArrayList<RadioButton> rButtonList;
    SharedPreferences prefs;
    SharedPreferences.Editor editor;

    public CustomListPreference(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        rButtonList = new ArrayList<RadioButton>();
        prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        editor = prefs.edit();
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder)
    {
        entries = getEntries();
        entryValues = getEntryValues();

        if (entries == null || entryValues == null || entries.length != entryValues.length )
        {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array which are both the same length");
        }

        customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);

        builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });
    }

    private class CustomListPreferenceAdapter extends BaseAdapter
    {        
        public CustomListPreferenceAdapter(Context context)
        {

        }

        public int getCount()
        {
            return entries.length;
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent)
        {  
            View row = convertView;
            CustomHolder holder = null;

            if(row == null)
            {                                                                   
                row = mInflater.inflate(R.layout.custom_list_preference_row, parent, false);
                holder = new CustomHolder(row, position);
                row.setTag(holder);

                // do whatever you need here, for me I wanted the last item to be greyed out and unclickable
                if(position != 3)
                {
                    row.setClickable(true);
                    row.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v)
                        {
                            for(RadioButton rb : rButtonList)
                            {
                                if(rb.getId() != position)
                                    rb.setChecked(false);
                            }

                            int index = position;
                            int value = Integer.valueOf((String) entryValues[index]);
                            editor.putInt("yourPref", value);

                            Dialog mDialog = getDialog();
                            mDialog.dismiss();
                        }
                    });
                }
            }

            return row;
        }

        class CustomHolder
        {
            private TextView text = null;
            private RadioButton rButton = null;

            CustomHolder(View row, int position)
            {    
                text = (TextView)row.findViewById(R.id.custom_list_view_row_text_view);
                text.setText(entries[position]);
                rButton = (RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button);
                rButton.setId(position);

                // again do whatever you need to, for me I wanted this item to be greyed out and unclickable
                if(position == 3)
                {
                    text.setTextColor(Color.LTGRAY);
                    rButton.setClickable(false);
                }

                // also need to do something to check your preference and set the right button as checked

                rButtonList.add(rButton);
                rButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
                {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                    {
                        if(isChecked)
                        {
                            for(RadioButton rb : rButtonList)
                            {
                                if(rb != buttonView)
                                    rb.setChecked(false);
                            }

                            int index = buttonView.getId();
                            int value = Integer.valueOf((String) entryValues[index]);
                            editor.putInt("yourPref", value);

                            Dialog mDialog = getDialog();
                            mDialog.dismiss();
                        }
                    }
                });
            }
        }
    }
}

The xml for my PreferenceActivity. This is not my full xml, took out al my other preference items for simplicity. Again, be sure to mind the package name, the custom ListPreference class must be referenced by the package name. Also mind the names of the preference and the array names that hold the entries and values.

<?xml version="1.0" encoding="utf-8"?>

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

        <PreferenceCategory
                android:title="Your Title">

                <your.package.here.CustomListPreference
                    android:key="yourPref"
                    android:title="Your Title"
                    android:dialogTitle="Your Title"
                    android:summary="Your Summary"
                    android:defaultValue="1"
                    android:entries="@array/yourArray"
                    android:entryValues="@array/yourValues"/>

        </PreferenceCategory>
</PreferenceScreen>

My xml for the dialog's list view row. In the getView method be sure to use the name of this xml file in the line that inflates this.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="8dip"
    android:paddingTop="8dip"
    android:paddingLeft="10dip"
    android:paddingRight="10dip">

    <TableLayout
        android:id="@+id/custom_list_view_row_table_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">

        <TableRow
            android:id="@+id/custom_list_view_row_table_row"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/custom_list_view_row_text_view"
                android:textSize="22sp"
                android:textColor="#000000"  
                android:gravity="center_vertical"
                android:layout_width="160dip" 
                android:layout_height="40dip" />

            <RadioButton
                android:checked="false"
                android:id="@+id/custom_list_view_row_radio_button"/>
        </TableRow>
    </TableLayout>

</LinearLayout>

Finally, under res/values here is my array.xml that contains the entry names and values for the ListPreference. Again, shortened mine for simplicity.

<?xml version="1.0" encoding="utf-8"?>
<resources> 
    <string-array name="yourArray">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
        <item>Item 4</item>
    </string-array>

    <string-array name="yourValues">
        <item>0</item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>
</resources>

这篇关于列表中的preference自定义行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆