0

I am facing a weird issue here.

In my app, the user needs to restart the loader after updating some preferences onLoadFinished, all fine here, the thing is that is I set mAdapter.notifyDataSetChanged(); it scrolls all the way to the bottom of the list.

The weird part is that it only happens once, the first time you restart the loader. All the other times the view remains exactly where it is.

I am notifying the change on UI thread...

getLoaderManager().initLoader(EXISTING_EVAL_LOADER, null, ViewActivity.this);
runOnUiThread(new Runnable() {
    public void run() {
        mAdapter.notifyDataSetChanged();
    }
});

If I don't call notifyDataSetChanged(); the app crashes only on list item click and I get:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.

Alin
  • 1,218
  • 5
  • 21
  • 47
  • its better to use async task –  May 07 '18 at 16:13
  • @D.'s Even so, I don't thing that would fix my current issue. – Alin May 07 '18 at 16:15
  • where do you add your item,post more code.there are a lot of answers to this prb have you checked them?https://stackoverflow.com/questions/3132021/android-listview-illegalstateexception-the-content-of-the-adapter-has-changed –  May 07 '18 at 16:17
  • @D.'s Can't post right now. I am adding my items inside onLoadFinished, inside cursor.moveToFirst to be exact since this activity loads only one row. – Alin May 07 '18 at 16:19
  • is the ordering of the items `desc`?is the last item the most recently added? –  May 07 '18 at 16:21
  • @D.'s Yes , my content provider's query has "_ID DESC" – Alin May 07 '18 at 16:37

2 Answers2

0

I would suggest

if (cursor.getCount() > 0) {
    cursor.moveToPosition(-1);
    while (cursor.moveToNext()) {
          <do stuff>
    }
}

cursor.close();
  • Will try it out when I get to my laptop and get back to you – Alin May 07 '18 at 16:47
  • Will get back to you tonight. Busy schedule and I didn't get the chance to test much. – Alin May 09 '18 at 08:17
  • @Eminem experienced users such as the OP in this case do not need help with accepting answers. They will mark good answers as accepted without anyone telling them to do so. – Thomas Flinkow May 28 '18 at 12:24
0

I guess that your listView is CHOICE_MODE_SINGLE and last item is selected in listView and thus listView is scrolling to the selection.

if you do not need selecting items on listView use this:

listView.setChoiceMode(ListView.CHOICE_MODE_NONE)

or in xml: android:choiceMode="none"

otherwise if you need your selection settings use this after setting adapter in listView:

listView.clearChoices();
listView.setAdapter(listView.getAdapter());

if you want first item be selected and scrolling to top you can use following code:

listView.setSelectionAfterHeaderView();

or

listView.setSelection(0);
ygngy
  • 3,630
  • 2
  • 18
  • 29
  • I do need my selection settings and clearing choices + re- setting the adapter didn't change anything. – Alin May 07 '18 at 18:12
  • @Alin what about using `listView.setSelectionAfterHeaderView()` OR `listView.setSelection(0)` did you test it? – ygngy May 07 '18 at 19:26
  • @Alin maybe this one helps `if(listView.getChildAt(0) != null) listView.getChildAt(0).requestFocus()` – ygngy May 07 '18 at 19:33