Removing an item from ListView inside a custom adapter
Asked Answered
B

3

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.

Bort answered 17/4, 2013 at 15:1 Comment(4)
call notifyDataSetChanged() in onClick();Duntson
How many items are in your list? You might want to consider reusing viewsFleshy
On the average, I will not have more than 20-30 views though it will change from one user to another. I don't know how to reuse the views.Bort
notifyDataSetChanged() actually worked pretty flawless. Thank you very much @SankarVBort
C
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();

    }
}
Cutlor answered 18/4, 2013 at 6:38 Comment(4)
@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.Cutlor
I only changed the spacing so that the code is more readable to future visitors. Nothing more.Bort
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. @CutlorTuscarora
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.Cutlor
W
0

try using parent.removeViewAt(position).

Waxy answered 17/4, 2013 at 15:6 Comment(1)
This is the error I get when using your answer, java.lang.UnsupportedOperationException: removeViewAt(int) is not supported in AdapterViewEiten
K
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;
}
Kibitka answered 24/11, 2014 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.