之前我有这个工作,但看起来在Xamarin或Android SDK中有些变化。我试图用反序列化的json数据填充一个微调器。然而,微调器适配器不再接受我的列表了?它说我无法从对象转换为int?我在here
之前遵循的教程 Spinner spinner;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
string urlmountains = "http://my.azurewebsites.net/api/Mountains";
JsonValue json1 = FetchMountains(urlmountains);
var list = JsonConvert.DeserializeObject<List<RootObject>>(json1.ToString());
spinner = FindViewById<Spinner>(Resource.Id.spinner1);
//this line ------------------------------------------------->
spinner.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, list); // Error here cannot convert from Object to Int?
}
private JsonValue FetchMountains(string urlmountains)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlmountains));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = request.GetResponse())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc1 = JsonObject.Load(stream);
Console.Out.WriteLine("Response: {0}", jsonDoc1.ToString());
// Return the JSON document:
return jsonDoc1;
}
}
}
public class RootObject
{
public string ID { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public string Name { get; set; }
public double? Height_m { get; set; }
public double? Height_ft { get; set; }
public double? temperature { get; set; }
public double? humidity { get; set; }
public double? snowCover { get; set; }
}
}
答案 0 :(得分:1)
您的list
是List
的{{1}}:
RootObject
但是您的var list = JsonConvert.DeserializeObject<List<RootObject>>(...);
想要ArrayAdapter
List
:
string
您的IDE无法解析相应的构造函数,因为这些类型不匹配。将您的spinner.Adapter = new ArrayAdapter<string>(...);
类型参数更改为ArrayAdapter
:
RootObject
此外,spinner.Adapter = new ArrayAdapter<RootObject>(...);
显示其模型类的ArrayAdapter
方法返回的值,因此您需要覆盖ToString()
类中的值以返回所需的{{1} }}
RootObject