Currently the list when populated is starting with the view @ the bottom of the list. Is there a way using listAdapters to force it to the top of the list?
Currently the orientation scrolls to the bottom on create. Is there a way to pin the screen to the top when it creates? https://i.stack.imgur.com/TR9S4.jpg in this example you see that entry 1 on create is shoved upwards to make room for six… Instead I want it to populate like this. https://i.stack.imgur.com/Pu7n3.jpg… entry 1 is the top of the list and 6 is pushed off to the bottom for the scroll.
If you look at the picture above you will notice it starts at the bottom of the list instead of at the top. Any Ideas?
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
setListAdapter(mAdapter);
registerForContextMenu(getListView());
populateFields();
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchDaily(mRowId);
startManagingCursor(note);
String body = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_DBODY));
mAdapter.clear();
if (!(body.trim().equals(""))){
String bodysplit[] = body.split(",");
for (int i = 0; i < bodysplit.length; i++) {
mAdapter.add(bodysplit[i].trim());
}
}
}
}
**edited to fix != string error.
You are completely changing the adapter, so the scroll position is lost in the process… You can use:
But this is not perfect as it is, if a row is added before
positionthe index will be off. If your list contains unique values you can useArrayAdapter#getPosition(), to find the new index.While I still recommend using a CursorAdapter, because it handles large table data better, I want to address a point on efficiency with your ArrayAdapter code.
By using
adapter.clear()andadapter.add()you are asking the ListView to redraw itself on every step… potentially dozens or hundreds of times. Instead you should work with the ArrayList directly and then ask the ListView to redraw once itself withArrayAdapter#notifyDataSetChanged()after the loop completes.