3

I have a custom list with two two textviews and a delete button. I want to delete the listview item when I click delete button I have tried these answers delete item from custom listview, Remove selected item from ListView, Remove ListView items in Android with no luck.

This is my Adapter class

public class SpecialListAdapter extends BaseAdapter
{
Activity context;
List<String> id = new ArrayList<String>();
List<String> name = new ArrayList<String>();
List<String> newid = new ArrayList<String>();
List<String> newname = new ArrayList<String>();

ViewHolder holder;

public SpecialListAdapter(Activity context, List<String> id, List<String> name) {
    super();
    this.context = context;
    this.id = id;
    this.name = name;
}

public int getCount() {
    // TODO Auto-generated method stub
    return id.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private class ViewHolder {
    TextView txtViewID;
    TextView txtViewName;
    ImageButton btnDelete;

}

public View getView(final int position, View convertView, ViewGroup parent)
{
    // TODO Auto-generated method stub

    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.special_list_item, null);
        holder = new ViewHolder();
        holder.txtViewID = (TextView) convertView.findViewById(R.id.specialId);
        holder.txtViewName = (TextView) convertView.findViewById(R.id.specialName);

        holder.txtViewID.setText(id.get(position));
        holder.txtViewName.setText(name.get(position));

        holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);
        holder.btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                id.remove(position);
                name.remove(position);
                notifyDataSetChanged();

            }
        });


        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }



return convertView;
}
}

Edit

Error Log:

12-18 17:39:46.030: D/AndroidRuntime(17893): Shutting down VM
12-18 17:39:46.030: W/dalvikvm(17893): threadid=1: thread exiting with uncaught exception (group=0x40dc3930)
12-18 17:39:46.053: E/AndroidRuntime(17893): FATAL EXCEPTION: main
12-18 17:39:46.053: E/AndroidRuntime(17893): java.lang.UnsupportedOperationException
12-18 17:39:46.053: E/AndroidRuntime(17893):    at java.util.AbstractList.remove(AbstractList.java:638)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at java.util.AbstractCollection.remove(AbstractCollection.java:229)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at .SpecialListAdapter$1.onClick(SpecialListAdapter.java:83)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at android.view.View.performClick(View.java:4202)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at android.view.View$PerformClick.run(View.java:17340)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at android.os.Handler.handleCallback(Handler.java:725)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at android.os.Looper.loop(Looper.java:137)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at java.lang.reflect.Method.invokeNative(Native Method)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at java.lang.reflect.Method.invoke(Method.java:511)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-18 17:39:46.053: E/AndroidRuntime(17893):    at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
AhulR
  • 51
  • 1
  • 6

5 Answers5

0

you are removing only from "name" List and you have

public int getCount() {
// TODO Auto-generated method stub
    return id.size();
}

so the count of list items isn't changing.

add to onClick line id.remove(position);, then notifyDataSetChanged();

also put this

holder.btnDelete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            id.remove(position);
            name.remove(position);
            notifyDataSetChanged();

        }
    });

outside/below if(convertView == null){...} else {...} and keep ViewHolder inside method ViewHolder holder; above this if

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I've already written id.remove(). Just scroll horizontally and you'll see it. And moving setOnClickListener outside if(convertView == null){...} else {...} gives me java.lang.UnsupportedOperationException at id.remove(position) – AhulR Dec 18 '14 at 11:47
0

Assume that you have a global variable for you String Array in your Activity class...

List<String> globalIDArray = new List<String>();
List<String> globalNamesArray = new List<String>();

You set the adapter to list, like this...

myListView.setAdapter(new SpecialListAdapter(getApplicationContext(), globalIDArray, globalNamesArray);

Now on button click...

button.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v)
       {
              globalIDArray.remove("id to remove");
              globalNamesArray.remove("name to remove");
              // now just invalidate the views and it will do remaining work automatically
              myListView.invalidateViews();
       }
}

Edit:

You can implement the functionality to remove item right inside the getView function of adapter. Something like this...

public View getView(final int position, View convertView, ViewGroup parent)
{
    // TODO Auto-generated method stub

    LayoutInflater inflater =  context.getLayoutInflater();

    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.special_list_item, null);
        holder = new ViewHolder();
        holder.txtViewID = (TextView) convertView.findViewById(R.id.specialId);
        holder.txtViewName = (TextView) convertView.findViewById(R.id.specialName);

        holder.txtViewID.setText(id.get(position));
        holder.txtViewName.setText(name.get(position));

        holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);

        //
        // Set tag to button so we can use ID and Name values at 'onClick'
        //
        holder.btnDelete.setTag(new ID_Name_Set(id.get(position), name.get(position)));

        holder.btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // retrieve tag object and fetch ID and Name from this object

                ID_Name_Set set = (ID_Name_Set)v.getTag();
                id.remove(set.ID);
                name.remove(set.Name);
                notifyDataSetChanged();

            }
        });


        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}

private class ID_Name_Set
{
    public String ID = "";
    public String Name = "";

    public ID_Name_Set(String id, String name)
    {
        this.ID = id;
        this.Name = name;
    }
}

I hope this helps.

:)

Hemendra Sharma
  • 1,063
  • 9
  • 21
0

You should add position of item into your button by following this, and then in onClick method, you should remove it from the actual list also.

holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);
holder.btnDelete.setTag(position);

and in your onClick method please add those new lines :

int pos = (Integer)v.getTag();
//remove item from list and notifydatasetChanged
name.remove(pos);
notifyDataSetChanged();

and one more thing is, you should also add tags to your views, in the case of "convertview != null" too.

I hope this will help you, since it helps me to solve my problem.

Regards.

denizt
  • 713
  • 5
  • 21
0
List<String> id = new ArrayList<String>();
List<String> name = new ArrayList<String>();

should be

ArrayList<String> id;
ArrayList<String> name;

The List will be unmodified, use ArrayList

then use in your constructor

this.id = new ArrayList<String>(id);
this.name = new ArrayList<String>(name);
Carnal
  • 21,744
  • 6
  • 60
  • 75
0
    if (convertView == null)
    {
    convertView = inflater.inflate(R.layout.special_list_item, null);
    holder = new ViewHolder();



    convertView.setTag(holder);
    }
    else
    {
    holder = (ViewHolder) convertView.getTag();
     }
    holder.txtViewID = (TextView) convertView.findViewById(R.id.specialId);
    holder.txtViewName = (TextView) convertView.findViewById(R.id.specialName);

    holder.txtViewID.setText(id.get(position));
    holder.txtViewName.setText(name.get(position));

    holder.btnDelete = (ImageButton) convertView.findViewById(R.id.bDelete);
    holder.btnDelete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            id.remove(position);
            name.remove(position);
            notifyDataSetChanged();

        }
    });
deepak825
  • 432
  • 2
  • 8