无法从ArrayAdapter中更新数据

时间:2014-10-02 05:32:10

标签: android android-listview android-arrayadapter notifydatasetchanged

我使用带有a的数组适配器显示线性布局列表。布局有3个文本字段和一个隐藏按钮,单击列表项时会显示该按钮。我想要做的是将onclicklistener附加到按钮,该按钮将从arraylist / adapter中删除该行并更新数据集。我有以下代码:

public class CheckAdapter extends ArrayAdapter<Check> {
    Context context;
    int layoutResourceId;
    ArrayList<Check> data = null;

    public CheckAdapter(Context context, int resource, ArrayList<Check> objects) {
        super(context, resource, objects);
        this.context = context;
        this.layoutResourceId = resource;
        this.data = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if(convertView == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder(convertView.findViewById(R.id.checklistRoot));
            holder.checkName = (TextView) convertView.findViewById(R.id.checklistname);
            holder.checkNumber = (TextView) convertView.findViewById(R.id.checklistnumber);
            holder.checkTotal = (TextView) convertView.findViewById(R.id.checklisttotal);
            holder.deleteButton = (Button) convertView.findViewById(R.id.deletecheck);

            holder.swipeButtons();

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
            holder.swipeButtons();
        }

        String name = data.get(position).getName();
        Double total = data.get(position).getAmmount();
        Long number = data.get(position).getCheckNumber();

        holder.checkName.setText(name);
        holder.checkNumber.setText(number.toString());
        holder.checkTotal.setText(total.toString());

        holder.deleteButton.setTag(position);
        holder.deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer position = (Integer) view.getTag();
                Log.d("Check Adapter", "Button Clicked: " + position);
                data.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;

    }

    static class ViewHolder{
        TextView checkName;
        TextView checkNumber;
        TextView checkTotal;
        Button deleteButton;
        View rootView;


        ViewHolder(View rootView) {
            this.rootView = rootView;
        }

        public void swipeButtons(){
            rootView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    deleteButton.setVisibility(View.VISIBLE);
                    return false;
                }
            });
        }
    }
}

我的日志显示我有正确的位置被发送到data.remove(position);然而,ListView似乎没有更新,数据似乎仍然存在。有什么建议吗?

非常感谢!

1 个答案:

答案 0 :(得分:5)

您还可以尝试使用getView方法本身提供的位置值。 同时从getView方法中删除行holder.deleteButton.setTag(position);

所以你的代码如下:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        // --- Your other code ---//


        holder.deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;

    }

希望这会对你有所帮助。