Maantje
Maantje

Reputation: 1801

How to delete item from listView with custom baseadapter

I am running into a problem i have a list view and i want to remove an item from it on button click. I can find examples on the internet but i can't get them to work. How can i achieve this?

Here is my code:

public class GroupRequestCustomAdapter extends BaseAdapter {
    String [] result;
    Context context;
    String [] imageId;
    String [] groupId;

private static LayoutInflater inflater=null;

public GroupRequestCustomAdapter(GroupRequests ListActivity, String[] prgmNameList, String[] prgmImages, String [] GroupId) {

    // TODO Auto-generated constructor stub

    result=prgmNameList;
    context=ListActivity;
    imageId=prgmImages;
    groupId = GroupId;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;

}

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

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

public class Holder
{
    TextView tv;
    ImageView img;
    Button accept;
    Button decline;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    //Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageId[position]);
    //Bitmap bitmap = getBitmapFromURL(imageId[position]);
    rowView = inflater.inflate(R.layout.group_request_list, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(result[position]);
    holder.accept=(Button) rowView.findViewById(R.id.buttonAccept);
    holder.decline=(Button) rowView.findViewById(R.id.buttonDecline);

    if (holder.img != null) {
        new GetImageFromUrl(holder.img,context).execute(imageId[position]);
    }

    holder.accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("Button click", "accept");
        }
    });
    holder.decline.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("Button click", "Decline");

        }
    });
    return rowView;

}
}

Upvotes: 0

Views: 3219

Answers (1)

2Dee
2Dee

Reputation: 8629

You could add methods in your adapter to do this for you :

public void deleteItem (int position) {
    result.remove(position);
    // remove(int) does not exist for arrays, you would have to write that method yourself or use a List instead of an array
    notifyDataSetChanged();
}

public void deleteItem (String itemToDelete) {
    result.remove(itemToDelete);
    // remove(int) does not exist for arrays, you would have to write that method yourself or use a List instead of an array
    notifyDataSetChanged();
}

By the way, your getItem(int position) implementation is wrong, it should return result.get(position). getItemId should also return a meaningful id; in your case, I think returning position is fine, though.

EDIT :

I noted your constructor takes 3 different lists. As a consequence, it becomes very hard for you to handle changes in your data set, since if you delete an item from results, you would have to delete corresponding items from the other lists as well. I would strongly recommend redefining your adapter as follows :

public GroupRequestCustomAdapter(GroupRequests ListActivity, List<MyObject>)

MyObject would be a very simple class, containing the data for one item in your ListView, for example :

public class MyObject {
    private String prgmName;
    private String prgmImage;
    // other required fields.

    // required getters/setters
}

That way, you only have one List to manage inside your adapter instead of managing result, imageId and groupId as you are doing now. And calling remove(int position) on a List is possible.

Finally, you could also consider extending ArrayAdapter instead of BaseAdapter, it has a few useful methods that BaseAdapter doesn't, including ... you guessed right : the remove method ;)

Upvotes: 1

Related Questions