Search code examples
androidarraylistandroid-arrayadapter

Not able to remove item at particular index from array adapter


i am not able to remove item from arraylist at particular index.

When i am trying to remove item then it is remove from bottom only.

private ArrayList<myproduct> values;



public View getView(final int position, final View convertView,
        ViewGroup parent) {
    final ViewHolder holder;
    View v = convertView;

    if (v == null) {
        final LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        v = vi.inflate(R.layout.row_favourite, parent, false);
        holder = new ViewHolder();

        holder.btnaddcollection = (Button) v
                .findViewById(R.id.btnaddcollection);

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



    homeproduct mhomeproduct  = values.get(position);


    holder.btnaddcollection.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            values.remove(position);

        }
    });
    return v;
}

static class ViewHolder {

    public Button btnaddcollection;

}

From above code when i am trying to remove then it is start from bottom to remove .

How can i start to remove item from top with particular position.?


Solution

  • Call notifyDataSetChanged() after you remove the item.

    This 'will say' to adapter: "Eh!! data was changed!"