Alex Rivera | Logout

ListViews - how to use ArrayAdapter.addAll() function before API 11?

Asked 2012-03-13T01:29:02.160
17

I'm trying update a ListView with an entirely new ArrayList. For API 11 it works great using the addAll(...) method, but this is not available for earlier APIs. I can't figure out how to go about updating an entire list like this for older versions.

ArrayAdapter<String> textList = new ArrayAdapter<String>(listener, R.layout.list_item, stringList);
listener.setListAdapter(textList);

Then later...

textList.clear();
textList.addAll(stringList); <--- works fine for Android 3.0 (API Level 11) and beyond, but not before. 

How did you go about doing this before addAll() was introduced in API 11? Thanks.

Edit
Report

1 Answer

4

I combined barbeau and Villarey's answers into what I think is a good solution:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void setData(List<String> data) {
    clear();
    if (data != null) {
        addAll(data);
    }
}


@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void addAll(String... items) {
    //If the platform supports it, use addAll, otherwise add in loop
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        super.addAll(items);
    }else{
        for(String item: items){
            super.add(item);
        }
    }
}
answered 2012-12-13T00:43:20.693

Your Answer