Right now, I am using this code to add an item to a listView:
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
private void addItem() {
HashMap<String, String> map = new HashMap<String, String>();
//put stuff in the map here.
painItems.add(map);
adapter.notifyDataSetChanged();
}
I was wondering if I would be able to add an item to the list at a certain position rather than at the end of the list. This seems like something that shouldn’t be too hard, but I may be missing something fundamental.
Thanks for your help, and cheers.
You are looking for
painItems.add(index, map);where index is the location you want to add the element at. All elements will be shifted one position to where their new index will be index + 1.
You have to be careful as this method will throw an
IndexOutOfBoundsExceptionif the index is greater than the current size.http://download.oracle.com/javase/6/docs/api/java/util/List.html#add(int,%20E)