Search code examples
c#wcfxamarintypes

explicit conversion from List<T> to Android.Widget.ListView


I have been trying to get a List that comes from a WCF and assign its values to a ListView control on Xamarin Android. But, I keep receiving this error,

Cannot implicitly convert type System.Collections.Generic.List<sometexthere> to Android.Widget.ListView.

The App code is

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
}

The WCF code that communicates with this is

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
}

Is there any way that I can make an explicit conversion or any solution? Any help is appreciated. Thanks!


Solution

  • Thanks for the hint Jason!

    Finally it worked in the following way. In the client code,

     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;
     }