Gridview项目中的按钮

时间:2017-11-29 15:07:52

标签: android gridview notifydatasetchanged

我在每个网格单元格中都有一个带有删除按钮的gridview。编码如下:

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不会刷新。上述代码有什么问题?

谢谢。

0 个答案:

没有答案