奇怪的行为ArrayList.remove();

时间:2015-05-11 17:35:25

标签: android listview indexing adapter

我遇到了一个问题,我似乎无法自己解决这个问题。

我正在尝试使用.remove(postion)从listView中删除项目;和.notifyDataSetChanged(position);但它正在消除错误的立场。

这是我的代码

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try {
            ViewHolder holder;
            final String[] parts;
            final String uuidbeacon;
            uuidbeacon = arrayL.get(position).getProximityUuid().toString();
            minorbeacon = arrayL.get(position).getMinor();
            rangebeacon = arrayL.get(position).getAccuracy();
            parts = fetchTums(uuidbeacon).split("::");

            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            }
            else{

                if(parts[0].equals("1")) {
                    holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
                    holder.beacon_image.setImageDrawable(getTumImage("http://127.0.0.1/uploads/face/" + parts[4]));
                    holder.beacon_uuid.setText(parts[1]);
                    holder.beacon_txpower.setText(String.valueOf(position));
                }
                if(parts[0].equals("0"))
                {
                    holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
                    holder.beacon_txpower.setText(String.valueOf(position));
                    toRemoveString = toRemoveString + String.valueOf(position) + "::";
                }
                if(parts[2].equals("1"))
                {
                    holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
                    holder.beacon_image.setImageDrawable(getTumImage("http://127.0.0.1/uploads/face/" + parts[4]));
                    holder.beacon_uuid.setText(parts[1]);
                    holder.beacon_txpower.setText("Hier misschien wat text?");
                    holder.beacon_row.setBackgroundColor(Color.parseColor("#B6B6B6"));
                    holder.image_lock.setVisibility(View.VISIBLE);
                }

            }
            if (arrayL.get(position).getProximityUuid() != null)


            list.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id )
                {
                    //list.setItemChecked(position, true);
                    //view.setBackgroundColor(Color.BLUE);

                   final String[] parts;
                   parts = fetchTums(arrayL.get(position).getProximityUuid().toString()).split("::");

                   if(parts[2].equals("1")){

                       pin(parts[3], parts[1]);
                   }
                   else{
                        if(parts[3] != null){
                            openPage(parts[3], "", parts[1]);
                        }
                   }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


        return convertView;
    }

上面的部分工作正常我得到一个列表视图,其中2个项目位置0和1,我知道项目具有正确的位置我在文本视图中写入位置。

删除项目的功能是:

public void toRemoveFunc()
{
    arrayL.remove(0);
    adapter.notifyDataSetChanged();
}

点击按钮即可调用它。

但现在发生的事情很奇怪,当我点击按钮时它将删除位置为1的项目,如果我再次点击它将删除位置为0的项目。如果我更改arrayL.remove(0); to arrayL.remove(1);它将删除位置为1的项目,如果我再次单击它会按预期崩溃。但我真正想要的是删除位置0并保持位置1.

1 个答案:

答案 0 :(得分:0)

您的代码正常运行,但在转换“convertView”后,您没有更新其值。

ViewHolder holder;
        final String[] parts;
        final String uuidbeacon;
        uuidbeacon = arrayL.get(position).getProximityUuid().toString();
        minorbeacon = arrayL.get(position).getMinor();
        rangebeacon = arrayL.get(position).getAccuracy();
        parts = fetchTums(uuidbeacon).split("::");

        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        }
        else{
holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
}
            if(parts[0].equals("1")) {

            holder.beacon_image.setImageDrawable(getTumImage("http://127.0.0.1/uploads/face/" + parts[4]));
                holder.beacon_uuid.setText(parts[1]);
                holder.beacon_txpower.setText(String.valueOf(position));
            }
            if(parts[0].equals("0"))
            {

                holder.beacon_txpower.setText(String.valueOf(position));
                toRemoveString = toRemoveString + String.valueOf(position) + "::";
            }
            if(parts[2].equals("1"))
            {

                holder.beacon_image.setImageDrawable(getTumImage("http://127.0.0.1/uploads/face/" + parts[4]));
                holder.beacon_uuid.setText(parts[1]);
                holder.beacon_txpower.setText("Hier misschien wat text?");
                holder.beacon_row.setBackgroundColor(Color.parseColor("#B6B6B6"));
                holder.image_lock.setVisibility(View.VISIBLE);
            }