5

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>
P.h. Dvrgne
  • 131
  • 1
  • 1
  • 7

5 Answers5

2

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();
        }
    });
tasomaniac
  • 10,234
  • 6
  • 52
  • 84
  • position and v.getTag() have the same value according to my Logs but strangly, doing list.remove((Integer) arg1.getTag()); doesn't do anything (I am also displaying what is in "list" and nothing change) whereas list.remove(position) (or even list.remove(0)) actually do something. That seems strange to me :/ – P.h. Dvrgne Jul 23 '13 at 15:31
  • Does list.remove(0) remove a item from the list? – tasomaniac Jul 24 '13 at 06:53
  • Yes, it removes the first item on the list but the last on the grid – P.h. Dvrgne Jul 24 '13 at 07:21
2

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.

P.h. Dvrgne
  • 131
  • 1
  • 1
  • 7
  • How exactly did you fix it? – user1810737 Jul 27 '13 at 13:02
  • I changed the way I set the text and image of each item. I was actually "hard coding" (don't know if that's correct english) each item (like position 0 will be this and that, position 1 will be this and that). That's obviously a bad idea since it will show you that you remove one item but you won't know for sure which you removed (I don't know if that's clear, tell me if you don't understand). – P.h. Dvrgne Jul 29 '13 at 07:47
1

Please use ViewHolder pattern, and also add your gridMain_text.xml else you will keep receiving wrong index of click http://www.binpress.com/tutorial/smooth-out-your-listviews-with-a-viewholder/9 This is example of ListView but equally applicable for GridView

Yahya Arshad
  • 1,626
  • 2
  • 19
  • 34
1

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 this code an now it work fine

Yahya Arshad
  • 1,626
  • 2
  • 19
  • 34
  • Sorry but that's already what I have, I forgot to edit this part when I added the ViewHolder. – P.h. Dvrgne Jul 24 '13 at 07:26
  • i ran your code in simulator , and it works fine,,, please try to uninstall your old application from your mobile/simulator and run clean and re-install – Yahya Arshad Jul 24 '13 at 14:48
  • Well, I don't know. I removed the app from my phone, clean then reinstall and nothing changed. I also tried on another phone and the same problem is still here : if you want to remove the 1st item on the grid, the first item will be removed from the list but we will see the last item disappear from the gridview, instead of the first one. Actually I'm wondering if the issue isn't coming from what I'm doing on each item inside getView() (changing textView specs). I'm gonna try that first thing tomorrow and I'll keep you updated. – P.h. Dvrgne Jul 24 '13 at 15:03
0

add Log.debug() in listener to ensure the remove position is right.

How do you know after click,the last item is removed?I mean,if there has text label in item view?Maybe you remove the right item,but after gridview refresh,it looks like the last item is removed.

You can add position label with setText in getView.That may helps you.

Ok,I know.Where did you do render item view?You must do it in getView.The AdapterView cached it's item view.As you removed the right position.May be the position is not the right view.You can run hierarchyviewer to detect what happen.

user890973
  • 947
  • 2
  • 8
  • 12
  • I am using Log to display "position", I just removed them before posting. Whenever I click on the first item's ImageButton, I can see that "position = 0" (and =1 if I click on the 2nd item, which is normal) and in everycase, it's always the last item to be removed. Actually I have 3 items to start, and no matter which one I want to remove, it's always the 3rd, then the 2nd then the first to be removed (each item has a different text/image). – P.h. Dvrgne Jul 23 '13 at 15:03