Fill spinner with list

2019-08-24 00:11发布

问题:

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

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?

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?

回答1:

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
}

Example (java): https://dzone.com/articles/populate-spinner-from-json-data



回答2:

"The list has 4 columns: Id, Name, Age, Gender and 28 rows."

A List cannot have 4 columns, so this is confusing. A List is a collection of types. It may be that your List has 28 items of a type that has 4 properties: Id, Name, Age, Gender. Let's call this type Customer, which may look something like:

public class Customer
{
    public int Id {get; set;}
    public string Name {get; set;}
    public int Age {get; set;}
    public string Gender {get; set;}
}

If the above is true, then you have a List<Customer> that has 28 Customer items, lets call it CustomerList, and you will need to do something like this:

var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem);
foreach (Customer customer in CustomerList)
{
    adapter.Add($"{customer.Id} - {customer.Name} - {customer.Age} - {customer.Gender}");
}
CustomerSpinner.Adapter = adapter;

Note that assigning CustomerSpinner.Adapter = adapter; after you add all of the customers to the adapter is best. If you assign CustomerSpinner.Adapter = adapter; before you add all 28 of the customers to the adapter then you will have to call CustomerSpinner.Adapter.NotifyDataSetChanged(); after you added the customers to the adapter so the spinner can be updated with the new values.