c#-获取选定微调器项的特定值或位置
我有个问题 我与一些客户创建了一个纺纱机。微调器是从一个包含4列的列表中构建的:Id、Name、Age、Gender。在微调器中,我创建了如下项目:c#-获取选定微调器项的特定值或位置,c#,spinner,C#,Spinner,我有个问题 我与一些客户创建了一个纺纱机。微调器是从一个包含4列的列表中构建的:Id、Name、Age、Gender。在微调器中,我创建了如下项目:Id:1-姓名:John-年龄:46-性别:男性 Id:2-姓名:Micheal-年龄:32-性别:男性 等等 现在我想要的是获取所选项目的Id,但我无法确定它,因为我创建了一个自定义项目字符串。所以当我需要输入时,我当然会得到整个字符串。我如何才能只获取字符串的Id并切断:“Id:=>-姓名:…-年龄:…-性别:…”所以剩下的唯一一件事就是Id作为
Id:1-姓名:John-年龄:46-性别:男性
Id:2-姓名:Micheal-年龄:32-性别:男性 等等 现在我想要的是获取所选项目的Id,但我无法确定它,因为我创建了一个自定义项目字符串。所以当我需要输入时,我当然会得到整个字符串。我如何才能只获取字符串的Id并切断:“Id:=>-姓名:…-年龄:…-性别:…”所以剩下的唯一一件事就是Id作为Int
private void CustomerSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
SelectedSpinnerCustomer = spinner.GetItemAtPosition(e.Position).ToString();
LoadCustomerInfo(SelectedSpinnerCustomer);
}
因此,为了明确起见,我希望SelectedSpinnerCustomer是一个int。如何才能做到这一点?最好的方法是创建一个自定义Spinner类,该类继承常规Spinner类并包含所有四个属性
public class MySpinner : Spinner
{
public int Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
那么
private void CustomerSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
MySpinner spinner = sender as MySpinner;
int SelectedSpinnerCustomer = spinner.Id;
int age = spinner.Age;
string name = spinner.Name;
string gender = spinner.Gender;
}
string
类中有几种不同的方法可用于此操作。这里有一个稍微简化的方法,它使用了多种字符串方法
//Save the string in a local variable with a short name for better readability
string str = spinner.GetItemAtPosition(e.Position).ToString();
//Split the string by the underscores ('-')
string[] splitted = str.Split("-");
//Now you have a string array with all the values by themselves
//We know that ID is the first element in the array since your string starts with the ID
//So save it in a new string
string idStr = splitted[0];
//idStr is now "id: x "
//Remove the spaces by replacing them with nothing
idStr = idStr.Replace(" ", "");
//idStr is now "id:x"
//To get only 'x' we need to remove "id:" which can be done in multiple ways
//In this example I will use String.Remove() method
//Start at index 0 and remove 3 characters. This will remove "id:" from "id:x"
idStr = idStr.Remove(0, 3);
//Now idStr is "x" which is an integer, so we can just parse it
int id = int.Parse(idStr);
//Now you have the id as an integer!
我需要在
私有无效客户pinner\u ItemSelected(对象发送者,AdapterView.ItemSelectedEventArgs e)
中执行什么操作?感谢您的帮助!谢谢你的帮助!