c# - 尝试将项目添加到列表时 Foreach 循环中断

标签 c# loops xamarin foreach

<分区>

我是一名 Android 开发人员,并且是 Xamarin 的新手。我想做的是,我从服务器获取 JSON 请求并将其添加到列表(对象)中。然后我想检查列表中的一些值,并根据这些值将这些数据添加到新列表(相同结构)中。我在 Android 中使用 JAVA 完成了此操作,但在 c# 中该方法不起作用。当我尝试向新列表添加值时,循环首先中断。

我的服务...

public async Task<List<FpRankData>> GetFpData(string accessToken)
{
    List<FpRankData> myFpList = null;
    try
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
        var json = await client.GetStringAsync("http://203.115.11.236:10455/MobileAuthWS/api/Agent/GetFP2018Rank");
        myFpList = JsonConvert.DeserializeObject<List<FpRankData>>(json);
    }
    catch (Exception e)
    {
        myFpList = null;
        Debug.WriteLine("fp_error : ", e);
    }
    return myFpList;

}

检查方法...

    public List<FpRankData> fpFullList;
    public List<FpRankData> fpTempList;
    public List<FpRankData> fpRankList

    public async Task getFpDataAsync(string accessToken)
    {
        IsBusy = true;

        fpFullList = await _apiServices.GetFpData(accessToken);

        foreach (FpRankData item in fpFullList) {

            if (item.agentCode1.Equals(Settings.agentCode))
            {

                item.AgentName = ExceptBlanks(item.AgentName);

                fpTempList.Add(item);

                if (item.rank == 1)
                {
                    List<FpRankData> finList1 = new List<FpRankData>(fpFullList.GetRange(1, 1));
                    fpTempList.AddRange(finList1);
                }
                else
                {
                    List<FpRankData> finList1 = new List<FpRankData>(fpFullList.GetRange(0, 1));
                    fpTempList.AddRange(finList1);
                }

                if (item.rank == 110)
                {
                    List<FpRankData> finList110 = new List<FpRankData>(fpFullList.GetRange(108, 1));
                    fpTempList.AddRange(finList110);
                }
                else
                {
                    List<FpRankData> finList110 = new List<FpRankData>(fpFullList.GetRange(109, 1));
                    fpTempList.AddRange(finList110);
                }

                if (item.rank != 30)
                {
                    List<FpRankData> finList30 = new List<FpRankData>(fpFullList.GetRange(29, 1));
                    fpTempList.AddRange(finList30);
                }
                if (item.rank != 60)
                {
                    List<FpRankData> finList60 = new List<FpRankData>(fpFullList.GetRange(59, 1));
                    fpTempList.AddRange(finList60);
                }

                Debug.WriteLine(item.AgentName, " AgentName");
                Debug.WriteLine("");

            }
            else {
                Debug.WriteLine("AgentName ", " AgentName");
            }

        }

        fpRankList = fpTempList;

        Debug.WriteLine("ListCount : ", fpTempList.Count.ToString());

        IsBusy = false;

    }

    public string ExceptBlanks(string str)
    {
        string pattern = "\\s+";
        string replacement = " ";                      
        Regex rx = new Regex(pattern);
        string result = rx.Replace(str, replacement);
        return result;
    }

我已使用“设置”保存我的代理代码,并将其与列表进行比较。

public class FpRankData
{
    public string agentCode1 { get; set; }
    public string agentCode2 { get; set; }
    public string AgentName { get; set; }
    public string Role { get; set; }
    public string branchCode { get; set; }
    public string branchName { get; set; }
    public string region { get; set; }
    public int nop_count { get; set; }
    public double fp { get; set; }
    public string lastPersistencyYear { get; set; }
    public string persistencyValue { get; set; }
    public int rank { get; set; }
    public string acheivement { get; set; }
}

我在 Java android 中做了什么...(这有效)

    ArrayList<UserData> finList = new ArrayList<>();
    finList.clear();

    for (UserData customer : rList) {

        if (customer.getAgentCode().equals(rAgentCode)) {

            finList.add(customer);
            if (customer.getuPosition() == 1) {
                ArrayList<UserData> finList1 = new ArrayList<>(rList.subList(1, 2));
                finList.addAll(finList1);
            } else {
                ArrayList<UserData> finList1 = new ArrayList<>(rList.subList(0, 1));
                finList.addAll(finList1);
            }

            if (customer.getuPosition() == 110) {
                ArrayList<UserData> finList110 = new ArrayList<>(rList.subList(108, 109));
                finList.addAll(finList110);
            } else {
                ArrayList<UserData> finList110 = new ArrayList<>(rList.subList(109, 110));
                finList.addAll(finList110);
            }

            if (customer.getuPosition() != 30) {
                ArrayList<UserData> finList30 = new ArrayList<>(rList.subList(29, 30));
                finList.addAll(finList30);
            }

            if (customer.getuPosition() != 60) {
                ArrayList<UserData> finList60 = new ArrayList<>(rList.subList(59, 60));
                finList.addAll(finList60);
            }

    }

最佳答案

看起来您还没有实例化 fpTempListfpRankList 在尝试向这些列表中添加项目时您得到 NullReferenceException 的可能性。因此,您可以在声明时将它们初始化为以下 lkie,或者您可以检查 null 并在 getFpDataAsync() 方法 [使用它们之前] 中初始化它们:

 public List<FpRankData> fpTempList = new List<FpRankData>();
 public List<FpRankData> fpRankList = new List<FpRankData>();

关于c# - 尝试将项目添加到列表时 Foreach 循环中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53312729/

相关文章:

c# - a-synchronized 并行任务的正确方法

C# 类型转换和 as 之间的区别?

c# - 如何模拟网络浏览器以便网站为我提供正确的 HTML 源代码?

python - 如何从元组列表中创建变量并将其设置为类?

Javascript 在循环内循环以获取 JSON

c# - Xamarin Forms webview 未在设备上显示

c# - 如何在 Java 中执行 GUI 任务

php - 循环遍历 XML,仅在特定 ID 处循环 "look"

Android 应用程序本地化 - 在运行时下载翻译

ios - 后台抓取是否允许上传(PerformFetch)