user606669
user606669

Reputation: 1696

Can I use loader to update listadapter data

Hi can i use Loader (api support package v4) to update listAdapter data which i will be changing from another thread in background. Is it safe to do so or is it better to use asyncTask instead. What I am trying to do is to Have a listAdapter and when I press a loadMoreData it loads more contents in the list and update the list, I am retreiving contents from the internet so It cant be run on UI Thread, so I am changing it in background.

The content are stored in a Hashmap in a class, that i update using background thread.

I am previously using asynctask for this purpose but i want to update my code to use loader if its better.

Upvotes: 2

Views: 283

Answers (2)

CChi
CChi

Reputation: 3134

If you use loader, you might want to use something like getLoaderManager().getLoader(YOUR_LOADER_ID).onContentChanged() on the main thread after you update the data;

Upvotes: 0

ahodder
ahodder

Reputation: 11439

This doesn't answer your question, but I recommend that you just create a Handler in a your ListView Activity and pass updated to the handler (via a method call to the Activity).

public class StuffActivity extends Activity {
    ListView mLister;
    Handler mHander;
    ArrayList<String> mStuff = new ArrayList<String>();

    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Init widgets and ListView
        mHandler = new Handler() {
            @Override public void handleMessage(Message msg) {
                if (msg != null && msg.obj != null) {
                    if (msg.obj instanceOf String)
                        mStuff.add(msg.obg);
                }
                updateListView();
            }
        }
    }

    public void updateList(String msg) {
        mHandler.dispatchMessage(new Message(0, msg));
    }

    private void updateListView() {
        // ...
    }
}

Upvotes: 1

Related Questions