2

I have a ListView which onResume I am trying to populate with data from a SQL Lite Database. During resume I add a click listener and repopulate the List View. The onResume, populateListView(), and onPause methods are below. The full source code is available on GitHub.

onResume:

@Override
protected void onResume() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(TalkServerIntent.INTENT_NEW_MESSAGES.toString());
    registerReceiver(receiver, filter);

    setContentView(R.layout.main);

    talkListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parentView, View childView,
                            int position, long id) {
            GeoCamTalkMessage msg = adapter.getTalkMessage(position);
            if (msg.hasAudio()) {
                try {
                    UIUtils.playAudio(getApplicationContext(), msg, player,
                            siteAuth);
                } catch (Exception e) {
                    UIUtils.displayException(getApplicationContext(), e,
                            "Cannot retrieve audio");
                }
            }
        }
    });

    populateListView();
    super.onResume();
}

populateListView:

private void populateListView() {
    try {
        talkMessages = messageStore.getAllMessages();
    } catch (Exception e) {
        Log.i("Talk", "Error:" + e.getMessage());
    }

    if (talkMessages != null) {
        adapter.setTalkMessages(talkMessages);
        talkListView.setAdapter(adapter);
    }
}

I'm not doing anything special on the onPause, but since the values are stored in the database didn't think I would have to.

onPause

@Override
protected void onPause() {
    unregisterReceiver(receiver);
    super.onPause();
}

What am I doing wrong that is causing the ListView to not be refreshed onResume?

ahsteele
  • 26,243
  • 28
  • 134
  • 248
  • @ahsteele: Have you tried debugging this or following the logcat? Seems the most likely reason is that talkMessages = messageStore.getAllMessages(); is returning null so the adapter is never set. – Squonk Apr 13 '11 at 00:17
  • @MisterSquonk that's what I thought and before posting ran through the application in the debugger. I've watched LogCat closely and don't see anything that would indicate a problem. But I do know that there are definitely messages returned from `messageStore.getAllMessages()`. – ahsteele Apr 13 '11 at 00:21
  • @ahsteele: Long shot - I notice you tend to call the 'super' methods at the end of your overridden methods (e.g., onResume()). Most code I see tends to have these at the start (with a few exceptions). Is there any chance RoboActivity does something in its onResume() method which could have a negative effect? As I said, it seems unlikely and it's just a long shot. – Squonk Apr 13 '11 at 00:47
  • @MisterSquonk funny that you say that I just moved the `super` for that very reason. Nothing changed though. :( – ahsteele Apr 13 '11 at 00:53
  • 1
    @ahsteele: Oh well, worth a try. The only other thing I can see which might be worth looking at is you could try moving setContentView(R.layout.main); to your onCreate() method. It seems setting it in onResume every time might cause issues??? Again, perhaps another long shot. – Squonk Apr 13 '11 at 00:59
  • 1
    @MisterSquonk that did the trick. You should turn your comment into an answer so I can up vote you and get you some points. ;) – ahsteele Apr 13 '11 at 01:44
  • @ahsteele: Got there in the end. Comment promoted to answer. :) – Squonk Apr 13 '11 at 02:01

1 Answers1

5

You could try moving setContentView(R.layout.main); to your onCreate() method.

Setting it in onResume() every time might cause issues.

Squonk
  • 48,735
  • 19
  • 103
  • 135