Remove item from GridView

I am very new to Android development and it seems to me that it is very simple, but I could not find anyone on Google with the same problem. I have a Gridview that is populated with a TextView (which has an image on top) and an ImageButton (for deleting the current item). I want to remove the element that I click on ImageButton.

Here is my main one:

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 fact is that when I click on one ImageButton, the last element that was deleted is always added, and I can’t understand why and how to fix it.

Thanks.

==================== 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> 
+4
source share
5 answers

Well, that was my own mistake, and now I feel incredibly stupid.

I actually deleted the correct item, but in each view, I replaced each item in the position with what was there first. Therefore, each element was correct, but the image / text was old, because I modified them inside getView ().

Sorry for the loss of time, my bad ones, I want to hit myself now.

+2
source

Do you have an ImageButton in each element? You can set

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

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

 close.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { list.remove((Integer) v.getTag()); adapter.notifyDataSetChanged(); } }); 
+2
source

Use the ViewHolder template, and also add the gridMain_text.xml file otherwise you will continue to get the wrong click index http://www.binpress.com/tutorial/smooth-out-your-listviews-with-a-viewholder/9 This is an example of ListView, but equally applicable for GridView

+1
source

Please, use

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

instead of close.setOnClickListener, I ran your code, now it works fine

+1
source

add Log.debug () to the listener to ensure the correct delete position.

As you know, after a click, the last item is deleted? I mean, if there is a text label in the elements view? You may delete the item you want, but after updating the gridview it looks like the last item has been deleted.

You can add a position label with setText in getView. This can help you.

Ok, I know. Where did you visualize the position? You must do this in getView. The AdapterView cached this view of the element. How did you delete the correct position. Maybe the position is not the right kind. You can run hierarchyviewer to determine what will happen.

0
source

Source: https://habr.com/ru/post/1492962/


All Articles