Android ArrayAdapter.Add method not working

前端 未结 1 1019
轮回少年
轮回少年 2020-12-14 19:29

The ArrayAdapter.add() method is not working for me. I am using Eclipse Helios 3.6 with ADT Plugin, Target Source is a Froyo 2.2 emulator and 2.2 HTC Evo 4g. Here is my java

1条回答
  •  感情败类
    2020-12-14 19:47

    I'm just learning, but if I'm reading the source correctly, ArrayAdapter's constructor doesn't copy references to each of the elements in the array or list. Instead, it directly uses the list that's passed in, or for an array uses asList() to treat the original array as a list. Since the list returned by asList() is still just a representation of the underlying array, you can't do anything (such as resize) that you couldn't do with an array.

    Try passing a list like ArrayList instead of an array.

    ArrayList entries = 
            new ArrayList(Arrays.asList("List Item A", "List Item B"));
    
    ArrayAdapter arrAdapt=
            new ArrayAdapter(this, R.layout.list_item, entries);
    
    arrAdapt.setNotifyOnChange(true);
    arrAdapt.add("List Item C");
    

    0 讨论(0)
提交回复
热议问题