Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove item from GridView

I am very new to android development and I feel like this is very simple, yet, I haven't managed to find anyone on google with the same problem. I have a Gridview that is filled with a TextView (which has an image on top) and an ImageButton (to delete the current item). What I want to do is to remove the item of which I click the ImageButton.

Here is my Main :

public class ActivityMain extends Activity
{
GridView gridview;
public GridAdapter mainActivityAdapter;
public ArrayList<String> listService = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listService.add("Market");
    listService.add("Recherche");
    listService.add("Quiz");

    gridview = (GridView) findViewById(R.id.mainActivity_grid);
    mainActivityAdapter = new GridAdapter(this.getApplicationContext(), listService);

    gridview.setAdapter(mainActivityAdapter);

}

}

And here is my Adapter :

    public class GridAdapter extends BaseAdapter
    {
    Context context;
    ArrayList<String> list = new ArrayList<String>();
    GridAdapter adapter = this;

    public GridAdapter(Context context, ArrayList<String> list)
    {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int arg0)
    {
        return null;
    }

    @Override
    public long getItemId(int arg0)
    {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
    if(convertView==null)
    {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_gridmain, null);

        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.gridMain_text);
        holder.close = (ImageButton) convertView.findViewById(R.id.mainActivity_delete);

        convertView.setTag(holder);
    }

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

// Doing stuff on views

        holder.close.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View arg1)
            {
                // list.remove(position);
                list.remove(position);              
                adapter.notifyDataSetChanged();
            }
        });
        return convertView;

    }

public static class ViewHolder
{
    TextView textView;
    ImageButton close;
}

}

The thing is, when I click on one ImageButton, it's always the last item that has been added that is being removed and I can't figure out why or how to fix this.

Thank you.

================== EDIT :

Here is my activity_main.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DDDDDD"
android:orientation="vertical" >

<GridView
    android:id="@+id/mainActivity_grid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:layout_marginTop="5sp"
    android:clickable="false"
    android:gravity="center"
    android:horizontalSpacing="15dp"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

</LinearLayout>

And my item_gridmain.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView 
    android:id="@+id/gridMain_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:textAlignment="center"
    android:textColor="@android:color/holo_blue_dark"
    />

 <ImageButton
    android:id="@+id/mainActivity_delete"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/deleteFav"
    android:src="@drawable/boutoncroixfermer" />

</RelativeLayout>
like image 215
P.h. Dvrgne Avatar asked Jul 23 '13 13:07

P.h. Dvrgne


People also ask

How do I delete a row from the grid?

When the Delete Button is clicked, the OnRowDeleting event handler is executed. Inside the OnRowDeleting event handler, the Index of the GridView Row is determined and it is used to delete the Row from the DataTable. Finally the DataTable is saved back to the ViewState and the GridView is again populated.

How can we delete selected row from GridView and database in asp net?

In this article, I will show you how to delete selected rows from a GridView in ASP.NET. We are using a checkbox inside a GridView and a button. The User selects the row from the checkbox that he want to delete and on a button click it is deleted from the GridView and from the database too.

How do I get rid of GridView?

We can delete a record from the database table by clicking on the delete button that is in the Delete column of the GridView. We first of all create a Stored Procedure that gets the Id of an employee as a parameter from the GridView on whichever row the user clicked.


2 Answers

You have ImageButton's in every item right? You can set

holder.close.setTag(Integer.valueOf(position));

And then, you have all positions hidden in the right buttons.Change the OnClickListener like below:

close.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            list.remove((Integer) v.getTag()); 
            adapter.notifyDataSetChanged();
        }
    });
like image 151
tasomaniac Avatar answered Oct 11 '22 09:10

tasomaniac


Ok actually it was my own mistake and I feel incredibly dumb now.

I was actually removing the right item but in each view I was replacing each item at position with what was there in the first place. So each item was the correct one but the picture/text were the old one because I was modifying them inside getView().

Sorry for that lose of time, my bad, I want to punch myself now.

like image 31
P.h. Dvrgne Avatar answered Oct 11 '22 08:10

P.h. Dvrgne