0

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.

2 Answers2

1

You want the items later in the list to be at the top of the ListView? If so, check out this questions: Is it possible to make a ListView populate from the bottom?

Community
  • 1
  • 1
jsimon
  • 577
  • 6
  • 17
  • No I would like the listview to originate at the top... Currently the orientation scrolls to the bottom on create. Is there a way to pin the screen to the top when it creates? http://imgur.com/wGTEy 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. http://imgur.com/6Lg6e... entry 1 is the top of the list and 6 is pushed off to the bottom for the scroll... – Beau Thomsen Dec 10 '12 at 20:51
0

You are completely changing the adapter, so the scroll position is lost in the process... You can use:

ListView listView = getListView();
int position = listView.getFirstVisiblePosition();
if (!(body.trim().equals(""))){
   String bodysplit[] = body.split(",");
   for (int i = 0; i < bodysplit.length; i++) {
      mAdapter.add(bodysplit[i].trim());
   } 
}

listView.setSelection(position);

But this is not perfect as it is, if a row is added before position the index will be off. If your list contains unique values you can use ArrayAdapter#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() and adapter.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 with ArrayAdapter#notifyDataSetChanged() after the loop completes.

Sam
  • 86,580
  • 20
  • 181
  • 179