Gridview 项目中的按钮

Button in Gridview item

我有一个网格视图,每个网格单元格中都有一个删除按钮。编码如下:

Photos.java

gd_view.invalidate();
                gd_view.setAdapter(new SampleGridViewAdapter(Photos.this, local_links, local_id));
                gd_view.setOnScrollListener(new SampleScrollListener(Photos.this));
                gd_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
                    {
                        Utilities.custom_toast(Photos.this, "View big", ""+position, "short");
                    }
                });
                gd_view.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
                {
                    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3)
                    {
                        //Utilities.custom_toast(Photos.this, "LONG Press", ""+position, "short");
                        return true;
                    }
                });

SampleGridViewAdapter

final class SampleGridViewAdapter extends BaseAdapter
{
    Context context;
    ArrayList<String> links = new ArrayList<String>();
    ArrayList<Integer> id = new ArrayList<Integer>();
    SampleGridViewAdapter adapter = this;

    SampleGridViewAdapter(Context context, ArrayList<String> links, ArrayList<Integer> id)
    {
        this.context = context;
        this.links = links;
        this.id = id;
    }

    @Override
    public int getCount()
    {
        return links.size();
    }

    @Override
    public String getItem(int position)
    {
        return links.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        gridView = inflater.inflate(R.layout.inflated_gridview, null);

        ImageView btn_delete = (ImageView) gridView.findViewById(R.id.btn_delete);
        ImageView img_grid = (ImageView) gridView.findViewById(R.id.img_grid);
        final TextView tv_ref = (TextView) gridView.findViewById(R.id.tv_ref);


        final String url = getItem(position);
        Picasso.with(context).load(url).fit().centerCrop().into(img_grid);
        tv_ref.setText(""+id.get(position));

        btn_delete.setTag(Integer.valueOf(position));
        btn_delete.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                int photo_id = Integer.parseInt(tv_ref.getText().toString());
                Records_Photo n = Select.from(Records_Photo.class).where(Condition.prop("photo_Id").eq(photo_id)).first();
                n.delete();

                links.remove((Integer) v.getTag());
                id.remove((Integer) v.getTag());

                ArrayList<String> newlinks = new ArrayList<>();
                newlinks.addAll(links);

                ArrayList<Integer> newid = new ArrayList<>();
                newid.addAll(id);

                links.clear();
                links.addAll(newlinks);
                id.clear();
                id.addAll(newid);
                adapter.notifyDataSetChanged();
            }
        });

        return gridView;
    }
}

问题:

btn_delete 运行不正常,删除照片后 gridview 不会自行刷新。以上代码有什么问题?

谢谢。

试试这个:

links.remove(position);
adapter.notifyDataSetChanged();