android - 更新 ListView

标签 android android-listview android-adapter

我正在开发一个使用 ListView 的程序,其中包含用户输入的字符串。我希望它在用户单击按钮后列出每个字符串。此外,我希望 ListView 中显示的每个字符串值也位于一个数组中,以便我稍后可以操作该列表。这是我现在拥有的:

public ArrayList<String> choices = new ArrayList<String>();
public String[] choicesArray;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView listView = (ListView) findViewById(R.id.listView1);
    choicesArray = new String[] { "You have not entered a choice yet" };
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1,
            choicesArray);
    listView.setAdapter(adapter);

    final Button addButton = (Button) findViewById(R.id.Button1);
    addButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            EditText editText = (EditText) findViewById(R.id.edit_choice);
            String message = editText.getText().toString();
            adapter.add(message);

            //choices.add(message);
            //choicesArray = choices.toArray(choicesArray);

            //listView.setAdapter(adapter);
        }
    });
}

这段代码产生了一个错误:

FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
at MainActivity$1.onClick(MainActivity.java:38)

最佳答案

您的列表未更新的原因是您从未将数据项添加到适配器。您只是将它们添加到 choicesArray这是您传递给 new ArrayAdapter() 构造函数的数组。

该构造函数不会将适配器绑定(bind)到数组,而是基本上将数组复制到适配器(在您调用构造函数时)之后对数组所做的更改不会反射(reflect)在 ArrayAdapter 中。

我认为 ArrayAdapter<> 基本上与 ArrayList<> 相同,只是能够为某些 AdpaterView 父级生成 subview 。你可以传递一个 array[]首先,它将使用该数组中的值初始化自身,但它不会将数组链接到适配器或 ListView。在初始构造函数调用之后,对任何一个的任何更改都不会反射(reflect)在另一个中。

因此您应该将代码更改为如下所示:

addButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        EditText editText = (EditText) findViewById(R.id.edit_choice);
        String message = editText.getText().toString();
        adapter.add(message);

        //adapter.notifyDataSetChanged(); //<-- you might need to call this, but I think it might work without if you do it this way.
        //listView.setAdapter(adapter); //<-- you shouldn't need to call this each time. Once at the begining is enough.
    }
});

编辑:

更改您的 ArrayAdapter 构造函数并删除 choiceArray 参数,这样您应该留下这个:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1);

在那之后我认为你应该可以开始了。本质上问题在于,由于您传入的是一个数组,并且由于数组的大小是固定的(您的长度是 length = 1),所以它不允许您添加任何内容。我怀疑你可能会使用 ArrayList<String>而不是 String[]如果您愿意,但老实说,对于您的情况,我认为只要跳过该参数并在需要添加新项时直接将项添加到您的适配器会更容易。

关于android - 更新 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15256981/

相关文章:

android - 将图像拖动到屏幕上的特定位置并提醒其位置是否正确 -Android-

android - 丢失应用程序的 keystore 文件(但启用了 Google Play 应用程序签名)

android - 无法在 getItem 中转换值

android - RecyclerView.Adapter notifyDataSetChanged() 使用 ContentProvider

java - 创建自定义 RecyclerView 项目分隔符时出现空类

java - android - 启动模式为 'singleInstance' 的两个 Activity

java - 在 Android 中执行数学方程式

java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用

android - 从 SQLite 数据库中检索数据

android - ViewHolder 模式中的 setTag 和 getTag 的作用是什么?