3

Im trying to create a Spinners within a ListView, but i dont know how to get all the values of each spinner in a List of strings for example. This is the code of the Adapter:

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;
    }
4

1 回答 1

1

If you're looking to present the user with a way to launch a spinner from each row of the list, one approach would be to use a custom view such as the following 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
}

You would initialize this view within the GetView() of your CustomizeColumnsListAdapter above.

One other thing you'll likely want to consider in that method is the ViewHolder pattern recommended by Android: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

于 2012-05-15T14:59:38.947 回答