0

Say i have an Array list called listitem which updates every time a GridView is clicked. It gets the item of the GridView that was clicked and adds it to the array list, which is also used as the backing array for an adapter of a ListView. That works fine but instead of adding the same rows to the ListView, I want to display a message like item_clicked (2) when it was clicked twice, and if its clicked again display item_clicked (3) and so on.

I tried to approach this with this code:

public void addItems(int position) {
    if (listItems.contains(value.get(position))) {
        int index = listItems.indexOf(value.get(position));
        listItems.set(index,value.get(position)); <- here
    } else {
        listItems.add(value.get(position));
        adapter.notifyDataSetChanged();
    }
}

position is the index of the GridView and values is an array of the values of the GridView.

This is my GridView code:

        int index = 0;
        while (rs.next()) {
            value.add("ID :" + rs.getString(1) + "  Nombre:" + rs.getString(3));
            list2.add(values.get(index));
            index++;
            Log.w("query result: ", rs.getString(1));
        }
        ArrayAdapter adapter = new ArrayAdapter(papeleta_act.this, R.layout.list_item, list2);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                addItems(position);

            }
        });
    } catch (Exception e) {
        Log.w("My error;", e);
    }
}

I would appreciate any idea of how to approach this prob. Thanks in advance!

Community
  • 1
  • 1
ivan
  • 1,177
  • 8
  • 23

1 Answers1

0

For the sake of simplicity, let's assume all items in the GridView are of a single type, and let's call it A. In this case, the ArrayList holds items of type A.

A simple solution would be to have listitem contain objects of a type that wrap A and also have a counter, for the times clicked. Possible implementation is:

public class B {
    public A mA;
    public int times_clicked;
}

How to show the times clicked in the ListView depends on your implementation of the layout. The default behavior of an ArrayAdapter is to check if the root element of the layout it was given is a TextView, and then set its text to the result of the toString() function of the item it is showing, so you could add this method to class B:

public String toString(){
     if(times_clicked == 1 && A != null)
         return A.toString();
     else
         return "item_clicked ("+times_clicked+")";
}
SilverCorvus
  • 2,956
  • 1
  • 15
  • 26
  • okay, ill try it out just one question. wouldnt that trigger for all the elements that are going to be added to the list view? – ivan Oct 21 '15 at 22:24
  • yes, I edited it a bit. But in general this is a general direction of what you should do. you might still want to show `A.toString()` even when an item was clicked more than once. You might also want to move away from the default behavior eventually, which is explained in http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass – SilverCorvus Oct 21 '15 at 22:30
  • well ill try to implement this, not today tho, i have some stuff to do and complete but ill let you know what happened tomorrow – ivan Oct 21 '15 at 22:34
  • not really what i wanted but its a nice workaround and well it works so thanks! – ivan Oct 22 '15 at 19:33