Mono for Android: Spinner within a listview

一世执手 提交于 2020-01-06 19:39:09

问题


I have a listview which is populated from a webservice using an ArrayAdapter. The webservice provides me with all the data I need. Some are just plain textviews yet other alternate between EditText and spinners. I show them easily, also filling up the values where they're due in EditText fields.

The problem comes with filling up the value of the Spinner. Can I define an adapter within an adapter? Also my data comes from a webservice as an XML passed as a string.

My Spinner code so far inside my Adapter.cs:

if (item.FieldType == "OptionBOX")
    {
        Spinner SpinnerValue = (Spinner) view.FindViewById<Spinner>(Resource.Id.spinnerVal);
        SpinnerValue.Visibility = view.Visibility == ViewStates.Invisible ? ViewStates.Invisible : ViewStates.Visible;

        bool isReadOnly = bool.Parse(item.isReadOnly);

        if (isReadOnly == true)
        {
            SpinnerValue.Enabled = false;
            SpinnerValue.Focusable = false;
            SpinnerValue.FocusableInTouchMode = false;
        }
    }

My data for the spinner is within item.optbox_options.

A table from my XML for easier understanding:

<Table diffgr:id="Table5" msdata:rowOrder="4">
<IdRec>5</IdRec>
<FieldId>1026</FieldId>
<FieldDesc>stanje rezervoarja</FieldDesc>
<FieldType>ComboBOX</FieldType>
<isReadOnly>true</isReadOnly>
<FieldValue>6</FieldValue>
<FieldTextValue>2/4</FieldTextValue>
<OptBox_Options>
<Options><myOPT FieldValue="1" FieldTextValue="0"/><myOPT FieldValue="2" FieldTextValue="1/4"/><myOPT FieldValue="6" FieldTextValue="2/4"/><myOPT FieldValue="7" FieldTextValue="3/4"/><myOPT FieldValue="8" FieldTextValue="4/4"/></Options>
</OptBox_Options>
</Table>

So just to clarify my needs and wants: Can I use an adapter within an adapter and if I can - how? How can I display data from the OptBox_Options row? I need to display the value from the FieldTextValue column inside in my spinners.


回答1:


Why do you think that it is not possible to instantiate an Adapter somewhere inside your custom Adapter for your ListView to populate content inside of it? You can simply make the implementation of the Adapter for your Spinner outside of the Adapter for the ListView.

Then you just need to think out a decoupled way to pass data through the hierarchy of Adapters so that all levels get the data you want.

However I don't think the ListView design pattern is suited for this kind of interaction and you will probably have problems with touch events not being fired. I think an ExpandableListView would be a more sensible choice of design pattern for what you are trying to achieve, and will have a better usability.

You can find an example of an ExpandableListView here: ExpandableListView Mono for Android




回答2:


Resolved using:

List<string> entries = new List<string>();

String rawXML = item.OptBox_Options;

StringReader stream = null;
XmlTextReader reader = null;

DataSet xmlDS = new DataSet();
stream = new StringReader(rawXML);
// Load the XmlTextReader from the stream
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);

DataSet myOPTvalues = new DataSet();
myOPTvalues = xmlDS;

foreach (DataRow row in myOPTvalues.Tables[0].Rows)
{
    var optItem = new PrevzemSpin();
    optItem.FieldValue = row["FieldValue"].ToString();
    if (optItem.FieldValue.Equals("")) optItem.FieldValue = null;

    optItem.FieldTextValue = row["FieldTextValue"].ToString();
    if (optItem.FieldTextValue.Equals("")) optItem.FieldTextValue = null;

    entries.Add(optItem.FieldTextValue);
    SpinnerValue.Tag = optItem.FieldValue;
}


来源:https://stackoverflow.com/questions/14456694/mono-for-android-spinner-within-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!