【问题标题】:Remove Item From Listview using SQLite使用 SQLite 从 Listview 中删除项目
【发布时间】:2019-02-02 21:52:32
【问题描述】:

我的ListView 中有一个奇怪的问题,我不明白为什么会这样。

在我的checklist 中,当我选择 3 个或更多项目时,只有最后一个项目被删除,第一个项目被删除,第二个项目不会被删除。它只是保持选中状态,我需要再次单击此发布按钮。

SendDataFragment 活动

   btn_DataSend=(Button) getActivity().findViewById(R.id.trn_post_btn);
    btn_DataSend.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            StringBuffer responseText = new StringBuffer();
            responseText.append("The following were selected...\n");

            //get All info like custID, mStatus mTrnNo mCustID mCustName
            List<customerInfo> stateList = adapter.mCategories;

            for(int i=0;i<stateList.size();i++)
            {
                customerInfo state = stateList.get(i);
                if(state.ismStatus())
                {
                    //postData(state.getmTrnNo());
                    int c = i;
                    dbManager.delete_ShipmentDetails(state.getmCustID());
                    stateList.remove(i);
                }
            }
            adapter.notifyDataSetChanged();
           Toast.makeText(getActivity(), responseText, Toast.LENGTH_LONG).sho();
        }
    });

数据库管理器

 public void delete_ShipmentDetails(String id){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        db.execSQL("delete from "+ DatabaseHelper.TBL_OE_SHIPMENT_H +" WHERE " +DatabaseHelper.OE_SHIPMENT_H_CUSTID+"="+id);
}

我的应用截图是:

【问题讨论】:

  • 我建议您调试并检查它是否通过if(state.ismStatus()) 条件。
  • 我在您的代码中看到的第一个问题是您正在遍历列表并从中删除项目。我建议以相反的顺序浏览此列表。
  • 是的,我已经调试过了这个条件返回一个真或假,它实际上定义了我选择了哪个项目,就像它在 i = 0 和 i = 1、2、3 时返回假一样,因为 i是选择我列表中的那些项目

标签: java android sqlite listview arraylist


【解决方案1】:

以相反的顺序使用循环

 for(int i = stateList.size()-1; i >= 0; i--){
        customerInfo state = stateList.get(i);
        if(state.ismStatus())
        {
            //postData(state.getmTrnNo());
            int c = i;
            dbManager.delete_ShipmentDetails(state.getmCustID());
            stateList.remove(i);
            notifyItemRemoved(i);
        }
     }

        adapter.notifyDataSetChanged();

【讨论】:

  • 谢谢我明白了.. 谢谢大家
【解决方案2】:

这是因为您在更改列表长度的同时循环遍历列表。

有几种方法可以解决这个问题,或者向后循环列表,使用迭代器或者保存删除直到结束。为了将循环更改为使用迭代器,以下内容应按预期工作:

Iterator<Integer> it = stateList.iterator();
while (it.hasNext()) {
    customerInfo state = it.next();
    if(state.ismStatus()) {
        dbManager.delete_ShipmentDetails(state.getmCustID());
        it.remove();
    }
}
adapter.notifyDataSetChanged();

【讨论】:

    【解决方案3】:

    现在我明白了,我也在这里做了另一种方式只是改变,比如

           for(int i=0;i<stateList.size();i++)
                        {
                            customerInfo state = stateList.get(i);
                            if(state.ismStatus())
                            {
                                postData(state.getmTrnNo());
                                dbManager.delete_ShipmentDetails(state.getmCustID());
                                stateList.remove(i);
                                i--;
                            }
                        }
                        adapter.notifyDataSetChanged();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多