如何从ListView自定义适配器中的微调器获取所有值

时间:2012-05-03 16:22:40

标签: listview custom-controls spinner xamarin.android

我试图在ListView中创建一个Spinners,但我不知道如何在一个字符串列表中获取每个微调器的所有值。这是适配器的代码:

public class CustomizeColumnsListAdapter : BaseAdapter
{
    private Activity context;
    private int columnsCount;
    public List<string> fieldsValues;

    public CustomizeColumnsListAdapter(Activity context, int columnsCount)
    {
        this.context = context;
        this.dataObject = dataObject;
        this.columnsCount = columnsCount;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        RelativeLayout view = (convertView
                        ?? context.LayoutInflater.Inflate(
                                Resource.Layout.customize_column_list_item, parent, false)
                    ) as RelativeLayout;

        try
        {
            string[] sortingColumsItems = {"None", "Id", "Customer Name", "Customer Number", "City", "Address", "Credit Limit", "Contact Name", "Phone Number", "Mail"};
            ArrayAdapter<string> spinnerArrayAdapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleSpinnerItem, sortingColumsItems);
            spinnerArrayAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            view.FindViewById<Spinner>(Resource.Id.cboCCD).Adapter = spinnerArrayAdapter;
            view.FindViewById<Spinner>(Resource.Id.cboCCD).Enabled = true;

            if(columnsCount>=position)
            {
                string columnIndex = (position + 1).ToString();
                view.FindViewById<TextView>(Resource.Id.lblColumn).Text = columnIndex;
            }

        }
        catch (System.Exception ex)
        {
            Toast.MakeText(context, ex.ToString(), ToastLength.Short).Show(); 
        }

        return view;            

    }

    public override int Count
    {
        get { return columnsCount; }
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return position;
    }

1 个答案:

答案 0 :(得分:1)

如果您希望向用户提供从列表的每一行启动微调器的方法,一种方法是使用自定义视图,例如以下SpinnerRow:

public class SpinnerRow : RelativeLayout, View.IOnClickListener, IDialogInterfaceOnClickListener
{
    #region Member Variables
    SpinnerItemSelectedDelegate _spinnerItemSelected;
    SpinnerPosSelectedDelegate _spinnerPosSelected;

    private TextView _label;
    private TextView _value;
    private ImageButton _edit;
    private string[] _options = new String[0];
    private int _position;

    private AlertDialog.Builder _builder;
    #endregion

    #region Construction
    public SpinnerRow (Context context) :
        base (context)
    {
        Initialize (context, null);
    }

    public SpinnerRow (Context context, IAttributeSet attrs) :
        base (context, attrs)
    {
        Initialize (context, attrs);
    }

    protected SpinnerRow (IntPtr doNotUse, Android.Runtime.JniHandleOwnership owner) :
        base (doNotUse, owner)
    {
        //Do nothing
    }

    protected void Initialize (Context context, IAttributeSet attrs)
    {
        Inflate (context, Resource.Layout.SpinnerRow, this);

        _label = FindViewById<TextView>(Resource.Id.editscope_addons_spinner_row_lbl);
        _value = FindViewById<TextView>(Resource.Id.editscope_addons_spinner_row_spin_lbl);
        _edit = FindViewById<ImageButton>(Resource.Id.editscope_addons_spinner_row_button);

        _label.Text = "Row label";
        string[] options = {"None", "Id", "Customer Name", "Customer Number", "City", "Address", "Credit Limit", "Contact Name", "Phone Number", "Mail"};
        _options = options;

        _edit.SetOnClickListener(this);

        _builder = new AlertDialog.Builder(context);
        _builder.SetTitle(_label.Text);
    }
    #endregion

    public virtual void OnClick(View v)
    {
        _builder.SetSingleChoiceItems(_options, _position, this);
        _builder.Create().Show();
    }

    public virtual void OnClick(IDialogInterface dialog, int position)
    {
        Position = position;
        _value.Text = _options[_position];

        dialog.Dismiss();
    }

    public int IndexOf(string option)
    {
        int numOptions = _options.Length;
        for (int i = 0; i < numOptions; i++)
        {
            if (_options[i].ToLower() == option.ToLower())
                return i;
        }

        return IntentConstants.INVALID_POSITION;
    }

    public bool Contains(string option)
    {
        return IndexOf(option) != IntentConstants.INVALID_POSITION;
    }

    #region Properties
    public string Text
    {
        get { return _label.Text; }
        set { _label.Text = value; }
    }

    public int Position
    {
        get { return _position; }
        set
        {
            _position = value;
            if (_position == IntentConstants.INVALID_POSITION)
            {
                _value.Text = NotSelectedText;
                if (OnSelected != null)
                    OnSelected("");
            }
            else if (_options != null && _options.Length > 0)
            {
                _value.Text = _options[_position];
                if (OnSelected != null)
                    OnSelected(_options[_position]);
            }

            if (OnPosSelected != null)
                OnPosSelected(_position);
        }
    }

    public string ValueText
    {
        get { return _value.Text; }
    }

    public String[] Options
    {
        get { return _options; }    
        set 
        { 
            _options = value;

            //Reset the current position so the text is updated
            int curPos = Position;
            _position = IntentConstants.INVALID_POSITION;
            Position = curPos;
        }
    }

    public SpinnerItemSelectedDelegate OnSelected
    {
        get { return _spinnerItemSelected; }
        set { _spinnerItemSelected = value; }
    }

    public SpinnerPosSelectedDelegate OnPosSelected
    {
        get { return _spinnerPosSelected; }
        set { _spinnerPosSelected = value; }
    }
    #endregion
}

您可以在上面的CustomizeColumnsListAdapter的GetView()中初始化此视图。

您可能希望在该方法中考虑的另一件事是Android推荐的ViewHolder模式:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html