用列表填充微调器 [英] Fill spinner with list

查看:61
本文介绍了用列表填充微调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为微调框填充列表时遇到问题.

I am having a problem with filling my spinner with a list.

我从URL链接下载了json并进行了解析,因此可以将其放在列表中.到目前为止很好...但是现在我有了列表,而且在互联网上找不到关于使用列表填充微调框的任何信息.该列表有4列:ID,名称,年龄,性别和28行.现在,我想在微调器中打印行,并用-分隔4列,以分隔单词,例如:"4-John-46-Male".我该怎么办?

I downloaded a json from a url link and parsed it so I can put it in a list. So far so good... But now I have the list and I can't find anything on the internet about filling a spinner with a list. The list has 4 columns: Id, Name, Age, Gender and 28 rows. Now I want to print the rows in a spinner with the 4 columns printed with a - to seperate the words, so for example: "4 - John - 46 - Male". How can I do that?

这是我从URL创建列表的代码的一部分:

Here is the part of the code where I create the list from a url:

Spinner CustomerSpinner = FindViewById<Spinner>(Resource.Id.CustomerSpinner);
//Startup WebClient
WebClient client = new WebClient();

//Define URL to download
string link = @"http://website.com/customers/getcustomers.php";

//Download json website content
string json = new WebClient().DownloadString(link);

//Parse json content
var jObject = JObject.Parse(json);

//Create Array from everything inside Node:"Customers"
var customerPropery = jObject["Customers"] as JArray;

//Create List to save Coin Data
customerList = new List<customer>();

//Find every value in Array: customerPropery 
foreach (var property in customerPropery )
{
    //Convert every value in Array to string
    var propertyList = JsonConvert.DeserializeObject<List<customer>>(property.ToString());

    //Add all strings to List
    customerList.AddRange(propertyList);
}

有人可以进一步帮助我吗?

Can someone help me further?

推荐答案

您应该创建一个ArrayAdapter并将其附加到微调器.像这样:

You should create an ArrayAdapter and attach that to the spinner. Something like this:

var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem);
CustomerSpinner.Adapter = adapter;

foreach (var customer in customerList)
{
    adapter.Add(customer.ToString());  // format your string here
}

示例(java): https://dzone.com/articles/从json数据填充spinner-来自

这篇关于用列表填充微调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆