Android - Remove GridView item from inside getView()?

前端 未结 2 1978
逝去的感伤
逝去的感伤 2021-01-17 05:22

I need a way to remove an Item from the GridView but this must be done from within the getView() method inside my custom Adapter. Here is my GridView:

Activi

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-17 06:17

    What you are asking for is very possible. You didn't give enough code for it to be clear exactly what's happening, but the code above you posted won't work unless the remove button is an entire view returned by getView in the adapter.

    Your getView in your adapter will need to look something like this:

    public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.grid_view_item, null);
            }
    
            Button removeButton = (Button) convertView.findViewById(R.id.remove_button);
            removeButton.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick(View v) {
                    adapter.mThumbIdsList.remove(position);
                    adapter.notifyDataSetChanged();
                }
            });
    
            return convertView;
        }
    

    Hope this helps. Good luck!

提交回复
热议问题