Android 从列表中删除是错误的

编程入门 行业动态 更新时间:2024-10-27 20:29:21
本文介绍了Android 从列表中删除是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我当前从网络服务中检索数据的代码如下:

My current code to retrieve data from webservice is the following:

try{
    for(int i = 0; i <= list.size() - 1; i++){
        SoapObject request = new SoapObject(NAMESPACE + "/", "get_all");    
        request.addProperty("host", host);
        request.addProperty("user", user);
        request.addProperty("pass", pass);
        request.addProperty("bd", bd);              
        request.addProperty("id", list.get(i));

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 60000);
        try {
            androidHttpTransport.call(URL + "/get_all", envelope);
        } catch (Exception e) {
            Log.e("Error HH:", e.toString());
            break;
        }

        SoapObject rep = (SoapObject) envelope.bodyIn;
        JSONArray jr = new JSONArray(rep.getPropertyAsString(0)); 

        JSONObject jb = (JSONObject) jr.get(0); 
        db.update_text(jb.getString("text_priority"), jb.getInt("id"));
        updateLabel("Importing id: " + jb.getString("id"));
        publishProgress((i * 100) / list.size()); 
    }
}catch (Exception e) {
    Log.e("Error H:", e.toString());
}

它在标签中正确显示如下:

And it displays correctly in the label the following:

导入 ID:1"

导入 ID:2"

导入 ID:3"

导入 ID:4"

导入 ID:5"

导入 ID:6"

它工作正常.虽然,我想在调用完成时删除变量list"中的 ID.

It works ok. Although, I would like to delete whenever the call is done perfectly the ID from the variable "list".

try {
    androidHttpTransport.call(URL + "/get_all", envelope);
    list.remove(list.get(i));
} catch (Exception e) {
    Log.e("Error HH:", e.toString());
    break;
}

但是,使用此代码,我的标签输出如下:

But, with this code my label output is the following:

导入 ID:1"

导入 ID:3"

导入 ID:5"

导入 ID:7"

我也试过这个:

try {
    androidHttpTransport.call(URL + "/get_all", envelope);
    list.remove(i);
} catch (Exception e) {
    Log.e("Error HH:", e.toString());
    break;
}

但是我得到了同样的结果,谁能告诉我为什么?谢谢.

But I get the same result, anyone can tell me why? Thanks.

推荐答案

您需要从列表中的最后一个项目开始,如果您想在迭代它们的同时删除项目,则向后工作.因此,将您的 for 循环更改为:

You need to start at the last item in the list and and work backwards if you want to remove items at the same time you're iterating over them. So change your for loop to:

for(int i = list.size()-1; i >= 0; i--)

由于在从数组中删除项目时将所有内容都向左移动,因此您需要确保光标也向同一方向移动.因此,如果我们有一个包含 4 个项目的数组,并且正在查看第 0 个位置的项目:

Since you're shifting everything over to the left when removing an item from an array, you want to make sure your cursor is moving in the same direction as well. So if we have an array of 4 items and are looking at the item in the 0th position:

[itemA itemB itemC itemD] (i=0)
 ^

然后删除项目并增加我们的计数器:

then remove the item and increment our counter:

[itemB itemC itemD] (i=1)
         ^

我们直接跳过了 itemB,直接跳到了 itemC!但是从后到前:

we jumped right over itemB and went straight to itemC! But going from back to front:

[itemA itemB itemC itemD] (i = 3)
                      ^

并删除:

[itemA itemB itemC] (i=2)
               ^

我们没有错过任何东西.

we didnt miss anything.

这篇关于Android 从列表中删除是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 07:25:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/881974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   列表中   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!