Android Limited Multi Select Preference,为什么没有复选标记?

时间:2010-09-28 07:17:35

标签: java android listview android-preferences

我正在尝试为Android应用程序创建自定义首选项,以限制用户可以选择的项目数。达到限制后,应禁用未选择的项目,并且仅启用当前选定的项目。如果选择的项目少于限制,则应启用所有项目。

这是我拼凑在一起的两个班级。代码运行正常,但没有检查CheckedTextView。结果没有维持状态。

首先是视图类......

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;

public class LimitedMultiChoiceView extends ListView 
    implements AdapterView.OnItemClickListener
{
    private static final String SEPARATOR = "OV=I=XseparatorX=I=VO";
    private static final String LIMITEDSELECTION="limitedSelection";
    private static final String SUPERSTATE="superState";

    private String mSelection = "";

    public LimitedMultiChoiceView(Context context, CharSequence[] items, int limit)
    {
        this(context, null, 0, items, limit);

    }       // LimitedMultiChoiceView(Context context)


    public LimitedMultiChoiceView(Context context, AttributeSet attrs, CharSequence[] items, int limit)
    {
        this(context, attrs, 0, items, limit);

    }       // LimitedMultiChoiceView(Context context, AttributeSet attrs)


    public LimitedMultiChoiceView(Context context, AttributeSet attrs
            , int defStyle, CharSequence[] items, int limit) 
    {
        super(context, attrs, defStyle);

        setAdapter(new ItemAdapter(context, android.R.layout.simple_list_item_multiple_choice, items, limit));

    }       // LimitedMultiChoiceView(Context context, AttributeSet attrs, int defStyle)


    public String getSelection()
    {
        return mSelection;

    }       // String getSelection()


    public void onItemClick(AdapterView<?> parent, View child, int position, long id) 
    {
        // For now we don't need anything...
        // we think the ItemAdapter manages what we need for each item.

    }       // void onItemClick(AdapterView<?> parent, View child, int position, long id) 


    @Override
    public void onRestoreInstanceState(Parcelable toParce) 
    {
      Bundle state=(Bundle)toParce;
      super.onRestoreInstanceState(state.getParcelable(SUPERSTATE));
      mSelection = state.getString(LIMITEDSELECTION);

    }       // void onRestoreInstanceState(Parcelable toParce) 


    @Override
    public Parcelable onSaveInstanceState() 
    {
      Bundle state=new Bundle();
      state.putParcelable(SUPERSTATE, super.onSaveInstanceState());
      state.putString(LIMITEDSELECTION, mSelection);

      return(state);

    }       // Parcelable onSaveInstanceState() 


    public void setSelection(String value)
    {
        mSelection = value;

    }       // void setSelection(String value)

    class ItemAdapter extends ArrayAdapter<CharSequence> 
    {
        CharSequence[] mItems = null;
        int mLimit = 1;

        public ItemAdapter(Context context, int viewResId, CharSequence[] items, int limit) 
        {
            super(context, viewResId, items);
            mItems = items;
            mLimit = limit;

        }       // ItemAdapter(Context context, int viewResId, CharSequence[] strings, int limit) 


        public boolean areAllItemsEnabled() 
        {
            // Since we are in a limited selection list not all items can be
            // selected so this always returns false, there is no calculating
            // to do.
            return false;

        }       // boolean areAllItemsEnabled() 


        public boolean isEnabled(int position) 
        {
            boolean[] clickedIndexes = new boolean[this.getCount()];
            boolean result = false;
            int selectedCount = 0;

            mSelection = "";
            for (int item=0; item < clickedIndexes.length; item++ )
            {
                View itemView = this.getView(item, null, LimitedMultiChoiceView.this);
                if (itemView instanceof CheckedTextView)
                {
                    CheckedTextView check = (CheckedTextView)itemView;
                    // First we turn the check mark on or off...
                    if (item == position)
                    {
                        check.setChecked(!check.isChecked());

                    }       // (item == position)

                    // Now we count how many are checked and mark our tracking array
                    if (check.isChecked())
                    {
                        clickedIndexes[item] = true;
                        mSelection += check.getText() + SEPARATOR;

                    }
                    else
                    {
                        clickedIndexes[item] = false;

                    }       // (check.isChecked())

                }       // (itemView instanceof CheckedTextView)

                if (clickedIndexes[item])
                    selectedCount++;

            }       // (int item=0; item< clickedIndexes.length; item++ )

            if (selectedCount >= mLimit)
            {
                if (clickedIndexes[position])
                {
                    result = true;

                }
                else
                {
                    result = false;

                }       // (clickedIndexes[position])

            }
            else
            {
                result = true;

            }       // (selectedCount >= mLimit)

            return result;

        }       // boolean isEnabled(int position) 

    }       // class ItemAdapter extends ArrayAdapter<CharSequence>


}       // class LimitedMultiChoiceView extends ListView 

这是偏好类......

import android.content.Context;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.View;


public class ListPreferenceLimitedMultiSelect extends ListPreference
{
    int mLimit = 3;
    LimitedMultiChoiceView mList = null;
    String mSelection = "";

    public ListPreferenceLimitedMultiSelect(Context context) 
    {
        this(context, null);

    }       // ListPreferenceLimitedMultiSelect(Context context)


    public ListPreferenceLimitedMultiSelect(Context context, AttributeSet attr)
    {
        super(context, attr);

    }       // ListPreferenceLimitedMultiSelect(Context context, AttributeSet attr) 


     @Override
     protected void onBindDialogView(View v) 
     {
        super.onBindDialogView(v);

        mList.setSelection(mSelection);

     }      // void onBindDialogView(View v) 


    @Override
    protected View onCreateDialogView() 
    {
        mList = new LimitedMultiChoiceView(getContext()
                , this.getEntries(), mLimit);
        return(mList);

    }       // View onCreateDialogView()


    @Override
    protected void onDialogClosed(boolean positiveResult) 
    {
        super.onDialogClosed(positiveResult);

        if (positiveResult) 
        {
            if (callChangeListener(mList.getSelection())) 
            {
                mSelection = mList.getSelection();
                persistString(mSelection);

            }       // (callChangeListener(mList.getSelection())) 

        }       // (positiveResult) 

    }       // void onDialogClosed(boolean positiveResult) 



    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) 
    {
        mSelection =(restoreValue ? getPersistedString(mSelection) : (String)"");

    }       // void onSetInitialValue(boolean restoreValue, Object defaultValue) 


}       // class ListPreferenceLimitedMultiSelect extends ListPreference

谢谢,

\ ^ /我l

0 个答案:

没有答案