0

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;

}
}
Maantje
  • 1,781
  • 2
  • 20
  • 33
  • check this [answer](http://stackoverflow.com/questions/30118595/showdialog-in-button-listview-adapter/30119715#30119715). It shows very similar implementation of yours – hrskrs May 22 '15 at 15:04

1 Answers1

1

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 ;)

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • Thank you for trying to help me! but I get a error "Cannot resolve method 'remove(int)'" – Maantje May 22 '15 at 15:16
  • Oops, my bad, I was too quick in writing my answer :-/ In fact, removing an item from an array is not exactly possible in Java (see [this question](http://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java)), so you instead make result to be a List rather than a String[], so you could call java Collections methods on it, like remove. I'll edit my answer. – 2Dee May 22 '15 at 15:23
  • Thanks for the fast reply! im gonna try and let you know how it went ^^ – Maantje May 22 '15 at 15:27
  • There, I finished editing the question with a bit more advice to get you going. I hope this will help, but don't hesitate to ask if there's something you don't really understand. – 2Dee May 22 '15 at 15:32