(XAMARIN ANDROID) - 我如何通过 List<T> 进行每个循环并抓取每个项目

这是我从 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


ITMISS
浏览 256回答 2
2回答

手掌心

如果我正确阅读了您的代码,那么您正在ToString()对每个COUNTRESULT_Info从您的 返回的代码进行操作repo.GetCountResult:stringResult&nbsp;=&nbsp;result.Select(x&nbsp;=>&nbsp;x.ToString()).ToList();换句话说,这就是你在做什么:&nbsp;COUNTRESULT_Info.ToString()您已经List<COUNTRESULT_Info>从您的 repo 方法返回了一个,因此您可以获得它的任何属性 - 例如x.CSTSDS或x.STDQTC.ToString()

梵蒂冈之花

好吧,我对你的问题的理解使我想到了这样的事情:&nbsp; if (result != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;stringResult= new List<string>();&nbsp; &nbsp; &nbsp; &nbsp;foreach(var item in result)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string s= item.STDSKU.toString() + item.STDQTC.toString() + item.CSTSDS +item.STDDSC&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringResult.Add(s);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, stringResult);&nbsp; &nbsp; &nbsp; &nbsp; listview_countresult.Adapter = adapter;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP