从List <t>到Android.Widget.ListView的显式转换

时间:2018-02-03 17:52:28

标签: c# wcf xamarin types

我一直在尝试获取来自WCF的List并将其值分配给Xamarin Android上的ListView控件。但是,我一直收到这个错误,

  

无法隐式转换类型   System.Collections.Generic.List<sometexthere>来   Android.Widget.ListView

应用代码

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.ACategory);

    btnBack = FindViewById<Button>(Resource.Id.btnTest);
    txtInform = FindViewById<TextView>(Resource.Id.textView1);
    lstSubCat = FindViewById<ListView>(Resource.Id.lstSubCats);

    btnBack.Text = "Back";
    string mainCat = Intent.GetStringExtra("ButtonClicked") ?? "Item not available";
    txtInform.Text = "You are viewing the " + mainCat + " Category. Choises in here will be populated when connected to database";
    this.Title = mainCat;

    IntSuk.SukMain sukachin = new IntSuk.SukMain();//This is a web reference

    sukachin.GetSubCategoryCompleted += Sukachin_GetSubCategoryCompleted;
    sukachin.GetSubCategoryAsync(mainCat);
}

private void Sukachin_GetSubCategoryCompleted(object sender, IntSuk.GetSubCategoryCompletedEventArgs e)
{
    lstSubCat = e.Result.ToList();//Here is where the error occurs. The result is a List<T> type but lstSubCat is an Android ListView
}

与此通信的WCF代码是

public List<string> GetSubCategory(string cat)
{
    SqlCommand cmd = new SqlCommand("select SubCategoryNameEnglish from SubCategory where Category='"+ cat + "'",Connection);
    DataTable dt = new DataTable();
    dt = DataManage.ExecuteDT(cmd);//Datamanage is a new class file and no problem with it.

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

    int counts = dt.Rows.Count;
    int i;
    for (i = 0; i <= counts - 1; i++)
    {
        retList.Add(dt.Rows[i]["SubCategoryNameEnglish"].ToString());
    }
    return retList; //This is what is returned to the App
}

有什么方法可以进行显式转换或任何解决方案吗?任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

感谢Jason提示!

最后它以下列方式工作。在客户端代码中,

 private void Sukachin_GetSubCategoryCompleted(object sender, IntSuk.GetSubCategoryCompletedEventArgs e)
 {
    List<string> listItems = new List<string>();
    listItems = e.Result.ToList();
    ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, listItems);
   lstSubCat.Adapter = adapter;
 }