Android ArrayAdapter.Add 方法不起作用
ArrayAdapter.add() 方法对我不起作用。我使用带有 ADT 插件的 Eclipse Helios 3.6,目标源是 Froyo 2.2 模拟器和 2.2 HTC Evo 4g。这是我的 java 类
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");
}
}
这是我的列表项布局 (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>
它给了我 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是在学习,但是如果我正在阅读 source 正确,ArrayAdapter 的构造函数不会复制对数组或列表中每个元素的引用。相反,它直接使用传入的列表,或者对于数组使用 asList() 将原始数组视为列表。由于 asList() 返回的列表仍然只是底层数组的表示,因此您无法执行任何无法使用数组执行的操作(例如调整大小)。
尝试传递像 ArrayList 这样的列表而不是数组。
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.