在ArrayAdapter.add()方法不是为我工作。我使用的Eclipse 3.6赫利俄斯与ADT插件,目标源是一个升级Froyo 2.2模拟器和2.2 HTC EVO 4G。这是我的Java类
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.ArrayAdapter;
公共类主要扩展活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
的String []条目= {列表项,列表项目B};
ArrayAdapter<字符串> arrAdapt =新的ArrayAdapter<字符串>(这一点,R.layout.list_item,项);
arrAdapt.setNotifyOnChange(真正的);
arrAdapt.add(列表项C);
}
}
这里是我的布局列表项目(list_item.xml)
< XML版本=1.0编码=UTF-8&GT?;
<的TextView
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
机器人:填充=10dp
机器人:TEXTSIZE =12SP
< / TextView的>
这是给我和错误的LogCat中,说
引起的: java.lang.UnsupportedOperationException 在 java.util.AbstractList.add(AbstractList.java:411) 在 java.util.AbstractList.add(AbstractList.java:432) 在 android.widget.ArrayAdapter.add(ArrayAdapter.java:178)
解决方案我刚学的,但如果我读source正确地,ArrayAdapter的构造不引用复制到每个阵列或列表中的元素。相反,它直接使用该真实通过在列表中,或者对于阵列用途的 asList()治疗原数组列表。因为由asList(返回)名单仍是底层数组的只是一个再presentation,你不能做任何事情(如调整大小),你不能用数组做的。
试着传递一个数组列表,如ArrayList代替。
的ArrayList<字符串>项=
新的ArrayList<字符串>(Arrays.asList(列表项,列表项目B));
ArrayAdapter<字符串> arrAdapt =
新的ArrayAdapter<字符串>(这一点,R.layout.list_item,项);
arrAdapt.setNotifyOnChange(真正的);
arrAdapt.add(列表项C);
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 class
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] entries = {"List Item A", "List Item B"};
ArrayAdapter<String> arrAdapt=new ArrayAdapter<String>(this, R.layout.list_item, entries);
arrAdapt.setNotifyOnChange(true);
arrAdapt.add("List Item C");
}
}
And here is my layout for the list item (list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:padding="10dp"
android:textSize="12sp"
</TextView>
It is giving me and error in the LogCat that says
Caused by: java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:411) at java.util.AbstractList.add(AbstractList.java:432) at android.widget.ArrayAdapter.add(ArrayAdapter.java:178)
解决方案
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<String> entries =
new ArrayList<String>(Arrays.asList("List Item A", "List Item B"));
ArrayAdapter<String> arrAdapt=
new ArrayAdapter<String>(this, R.layout.list_item, entries);
arrAdapt.setNotifyOnChange(true);
arrAdapt.add("List Item C");