【问题标题】:System.InvalidCastException: 'Specified cast is not valid.' Xamarin Spinner Array from Json APISystem.InvalidCastException:“指定的演员表无效。”来自 Json API 的 Xamarin Spinner 数组
【发布时间】:2023-04-10 05:04:01
【问题描述】:

我在填充微调器数组时遇到问题,它说无效的演员表。 你能帮我解决这个问题吗?

private async void getplant()
    {
        string url = "http://localhost/VMSAPINEW2/MasterData/getplantlist";
        JsonValue json = await FetchWeatherAsync(url);
        string temp = "";
        ArrayList items = new ArrayList();

        for (int i = 0; i < json.Count; i++)
        {
            temp = json[i]["plantName"].ToString();
            items.Add(temp);
        }

        var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, (IList<string>)items);
        spinnerPlantNormal.Adapter = adapter;
    }

item的结果是

item[0] "\"SBM - BIKE\""
item[1] "\"SBM - ROD\""

和错误 System.InvalidCastException: 'Specified cast is not valid.' 当用数组填充适配器时。

【问题讨论】:

  • 而不是(IList&lt;string&gt;)items 尝试做items.Cast&lt;string&gt;().ToList()

标签: arrays json api xamarin spinner


【解决方案1】:

尝试改变

ArrayList items = new ArrayList();

List<string> items = new List<string>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2021-09-30
    • 2021-09-27
    相关资源
    最近更新 更多