问题
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