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!