(XAMARIN ANDROID)-如何为通过List <t>的每个循环获取每个项目,然后传递给数组适配器,然后传递给ListView

时间:2018-07-31 03:05:01

标签: c# android json xamarin.android repository

这里有我的示例代码,用于从API获取值:

我的班级信息(型号)

 public class COUNTRESULT_Info
{

public  decimal STDSKU { get; set; }

public  decimal STDQTC { get; set; }

public  string CSTSDS { get; set; }

public  string STDDSC { get; set; }


}

我的API存储库:

 public async Task<List<T>> GetCountResult(string trfnumber, string usertype) {

        var httpClient = new HttpClient();
        var json = await httpClient.GetStringAsync("http://hoscbdd1:8888/api/result/GETCOUNTRESULT/"+trfnumber+"/"+usertype);

        return JsonConvert.DeserializeObject<List<T>>(json);

    }

}

ANDROID活动(通过列表视图):

   private async void GetCountResult(string trfnumber, string usertype) {

        ResultRepository<COUNTRESULT_Info> repo = new ResultRepository<COUNTRESULT_Info>();

        var result = await repo.GetCountResult(trfnumber, usertype);

        if (result != null)
        {
            stringResult = result.Select(x => x.ToString()).ToList();
            adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, stringResult);
            listview_countresult.Adapter = adapter;
        }
    }

我的列表视图中的输出是计算找到的对象个数:

SCRS.MODEL.COUNTRESULT_Info

SCRS.MODEL.COUNTRESULT_Info

SCRS.MODEL.COUNTRESULT_Info

SCRS.MODEL.COUNTRESULT_Info

SCRS.MODEL.COUNTRESULT_Info

2 个答案:

答案 0 :(得分:1)

如果我正确阅读了您的代码,则说明您对ToString()返回的每个COUNTRESULT_Info执行了repo.GetCountResult

stringResult = result.Select(x => x.ToString()).ToList();

换句话说,这就是您正在做的:COUNTRESULT_Info.ToString()

已经已经从您的repo方法中返回了List<COUNTRESULT_Info>,因此您可以获取其任何属性-例如x.CSTSDSx.STDQTC.ToString()

Hth ..

答案 1 :(得分:1)

好吧,我对您的问题的理解使我想到了这样的东西:

  if (result != null)
    {
       stringResult= new List<string>();
       foreach(var item in result)
       {
          string s= item.STDSKU.toString() + item.STDQTC.toString() + item.CSTSDS +item.STDDSC
          stringResult.Add(s);
       }            
        adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, stringResult);
        listview_countresult.Adapter = adapter;
    }