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?
A
List
cannot have 4 columns, so this is confusing. AList
is a collection of types. It may be that yourList
has 28 items of a type that has 4 properties: Id, Name, Age, Gender. Let's call this typeCustomer
, which may look something like:If the above is true, then you have a
List<Customer>
that has 28Customer
items, lets call itCustomerList
, and you will need to do something like this:Note that assigning
CustomerSpinner.Adapter = adapter;
after you add all of the customers to the adapter is best. If you assignCustomerSpinner.Adapter = adapter;
before you add all 28 of the customers to theadapter
then you will have to callCustomerSpinner.Adapter.NotifyDataSetChanged();
after you added the customers to theadapter
so the spinner can be updated with the new values.You should create an ArrayAdapter and attach that to the spinner. Something like this:
Example (java): https://dzone.com/articles/populate-spinner-from-json-data