xamarin.forms - Populating list view using a JSON subarray -
i have following json url. trying populate list view using subarrays hair[] , math[] after getting them json url.
here url http://pastebin.com/raw.php?i=2fzqlyxj
inside array "students" there subarray called "properties" , sub array inside "properties" called "grades". "hair" inside "properties" , "math" inside "grades".
here webservice class
public class webservice { public webservice () { } public async task<rootobject> getstudentinfoasync (string apiurl) { var client = new httpclient (); var response = await client.getstringasync(string.format(apiurl)); return jsonconvert.deserializeobject<rootobject>(response.tostring()); } }
and here view model
public class property { public int iq { get; set; } public string hair { get; set; } public string glasses { get; set; } public grade[] grades { get; set; } } public class studentinfo { public string name { get; set; } public string lastname { get; set; } public property[] properties { get; set; } public string studentname { get{ return string.format ("{0}", name); } } //i accessing json sub arrays following "hair" , "math" properties. public string[] haircolors { { return (properties ?? enumerable.empty<property>()).select(p => p.hair).toarray(); } } public string[] mathgrades { { return (properties ?? enumerable.empty<property>()).selectmany(p => p.grades ?? enumerable.empty<grade>()).select(g => g.math).toarray(); } } } public class rootobject { public studentinfo[] students { get; set; } } }
and here how populate list view student names works. notice "studentname" not in subarray.
var sv = new webservice(); var es = await sv.getstudentinfoasync(apiurl); xamarin.forms.device.begininvokeonmainthread( () => { listview.itemssource = es.students; }); var cell = new datatemplate (typeof(textcell)); listview.itemtemplate = cell; listview.itemtemplate.setbinding(textcell.textproperty, "studentname");
but need populate list view haircolors , mathgrades follows following code not work.
var sv = new webservice(); var es = await sv.getstudentinfoasync(apiurl); xamarin.forms.device.begininvokeonmainthread( () => { listview.itemssource = es.students; }); var cell = new datatemplate (typeof(textcell)); listview.itemtemplate = cell; listview.itemtemplate.setbinding(textcell.textproperty, "haircolors"); listview.itemtemplate.setbinding (textcell.detailproperty, "mathgrades");
what doesn't code work? how make work?
make custom converter this:
public class haircolorsconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { var haircolors = (string[])value; return string.join(", ", haircolors); } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
then bind like:
setbinding(textcell.textproperty, "haircolors", bindingmode.default, new haircolorsconverter());
Comments
Post a Comment