6

I have a custom listview item that includes a 'remove' button. I created a custom adapter called LazyListAdapter that extends BaseAdapter. Inside the getView method that I override I set this button's onclick method as follows:

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

     View v = convertView;

     // Some other things...

     ImageButton removeFav = (ImageButton) v.findViewById(R.id.removeFavorites);

     removeFav.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {

          // I delete the object from Parse database here,
          // Therefore I want the view to disappear here
     }
}

How can I delete or somehow hide the corresponding view by using a code inside this onclick method? Or should I change my approach?

Thank you so much in advance.

ecem
  • 3,574
  • 3
  • 27
  • 40

3 Answers3

8

Try this

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

    View v = convertView;

    // Some other things...

    ImageButton removeFav = (ImageButton) v.findViewById(R.id.removeFavorites);

    removeFav.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

       // After you delete the object from Parse database here,
       notifyDataSetChanged();

    }
}
ecem
  • 3,574
  • 3
  • 27
  • 40
Ankit
  • 1,916
  • 2
  • 20
  • 33
  • @ecem: please clarify what edit you want to do here. This is a good practice whenever you do a edit please leave a note over there. – Ankit Apr 20 '13 at 22:01
  • I only changed the spacing so that the code is more readable to future visitors. Nothing more. – ecem Apr 21 '13 at 19:02
  • Is that normal? I mean, I also want to show the typical confirmation message (are you sure, yes/no). Should I put that code inside the adapter? Thanks. @Ankit – Ricardo Jun 12 '15 at 12:32
  • Yes, you can write a `dialog fragment` and start it in `onClick` of each delete button. make sure you are maintaining unique id for each row. – Ankit Jun 15 '15 at 08:54
0

try using parent.removeViewAt(position).

  • 1
    This is the error I get when using your answer, `java.lang.UnsupportedOperationException: removeViewAt(int) is not supported in AdapterView` – ArtiomLK Jun 08 '17 at 07:04
0

Simply you can remove an item inside getview method as in the example

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

    ViewHolder viewHolder;

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_row, null, false);
        viewHolder = new ViewHolder();
        viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
        convertView.setTag(viewHolder);

    } else {
        // we call the view created before to not create a view in each time
        viewHolder = (ViewHolder) convertView.getTag();
    }

    final int imgId = imageId.get(position);

    viewHolder.img.setImageResource(imgId);

    viewHolder.img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "Clicked " + imgId, Toast.LENGTH_SHORT)
                    .show();
        }
    });

    // Create a generic swipe-to-dismiss touch listener.
    viewHolder.img.setOnTouchListener(new SwipeDismissTouchListener(
            viewHolder.img, null,
            new SwipeDismissTouchListener.DismissCallbacks() {
                @Override
                public boolean canDismiss(Object token) {
                    return true;
                }

                @Override
                public void onDismiss(View view, Object token) {

                    Log.d(TAG, "Image ıd" + imgId);


                    imageId.remove(position);
                    remove(position);

                    notifyDataSetChanged();
                }
            }));

    return convertView;
}
huseyin
  • 1,367
  • 16
  • 19