简体   繁体   English

Android Slider-我们可以从Item XML检索属性吗?

[英]Android Slider - can we retrieve attributes from the Item XML?

I am currently using Xamarin to create an Android app, so you're about to see C# and not Java, but if you know how to solve the same issue in Java, I would still appreciate your answer... 我目前正在使用Xamarin创建一个Android应用程序,因此您将看到C#而不是Java,但是如果您知道如何用Java解决同一问题,我仍然希望您的回答...

I have a Slider and I've set up a string array like below: 我有一个Slider,并设置了一个如下所示的字符串数组:

<string-array name="house_style_array">
  <item id="124">Item 1</item>
  <item id="565">Item 2</item>
  <item id="356">Item 3</item>
  <item id="537">Item 4</item>
</string-array>

I have created a ItemSelected Event and I am able to get the selected string, eg "Item 1" but I cannot find a way of getting the id - the following code returns an id based on the position in the string array (zero based) which is no use: 我创建了一个ItemSelected事件,并且能够获取选定的字符串,例如“ Item 1”,但是我找不到获取ID的方法-以下代码根据字符串数组中的位置返回ID(从零开始)这没有用:

private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;                        
        string toast = string.Format("The selection is {0}\r\nThe ID is {1}", spinner.GetItemAtPosition(e.Position), spinner.GetItemIdAtPosition(e.Position));
        Toast.MakeText(this, toast, ToastLength.Long).Show();
    }

Is there any way to do what I am trying to do. 有什么办法可以做我想做的事。 I have seen examples of creating objects but it seems overly complicated for a simple ID/String scenario. 我已经看到了创建对象的示例,但是对于简单的ID / String场景,它似乎过于复杂。 I don't have any objections to altering the XML. 我对更改XML没有任何异议。

Thanks 谢谢

Try this code: 试试这个代码:

string fileName = "yourfilePath";
string nodeList = "//string-array/item";
string itemInnerText = "Item 1";
string attributeName = "Id";


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList xmlNodeList = xmlDoc.SelectNodes(nodeList);
foreach(XmlNode node in xmlNodeList)
{
     if (node.Item(0).InnerText == itemInnerText)
         return node.Attributes[attributeName].Value;
}

In the end I decided to populate the spinner using objects as below as it gives me far more flexibility: 最后,我决定使用以下对象填充微调器,因为它为我提供了更大的灵活性:

Spinner spinner = FindViewById<Spinner>(Resource.Id.mySpinner);
        spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

        ArrayAdapter adapter = new ArrayAdapter(this,
                                                Resource.Layout.multiline_spinner_dropdown_item,
                                                new Taxonomy[] { new Taxonomy( 123, "option1"), 
                                                                 new Taxonomy( 124, "option2"), 
                                                                 new Taxonomy( 125, "option3"), 
                                                                 new Taxonomy( 126, "option4") });
        adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
        spinner.Adapter = adapter;

This allows me to add any number of properties to the object and have them easily available when the user selects an option: 这使我可以向对象添加任意数量的属性,并在用户选择一个选项时使它们易于使用:

private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
        Taxonomy selectedObject = (Taxonomy)spinner.Adapter.GetItem(e.Position);

        //rest of code using selected object.
    }

The last this to do is override the ToString() function in the Taxonomy object to display the required data in the spinner, eg: 最后要做的是重写Taxonomy对象中的ToString()函数,以在微调器中显示所需的数据,例如:

 public override string ToString()
    {
        return TaxonomyName;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM