更新ListView

时间:2013-03-06 20:08:29

标签: 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)

2 个答案:

答案 0 :(得分:1)

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

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

我喜欢把它想到ArrayAdapter&lt;&gt;与ArrayList&lt;&gt;基本相同能够为某些AdpaterView父级生成子视图的额外好处。您可以将array[]传递给它开始,它将使用该数组中的值初始化自身,但它不会将数组链接到适配器或ListView。在初始构造函数调用之后,对任何一个的任何更改都不会反映在另一个中。

因此,您应该将代码更改为:

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);
之后我觉得你应该好好去。基本上问题是,因为你传入一个数组,并且由于数组是固定大小(你的长度= 1),它不允许你添加任何东西。如果你愿意的话,我怀疑你可以使用ArrayList<String>代替String[],但老实说,根据你的情况,我认为一起跳过这个参数会更容易,并且每当你将项目直接添加到适配器需要添加新的。

答案 1 :(得分:0)

将新String添加到您的列表后,您只需使用适配器的notifyDataSetChanged(),它告诉适配器更新显示,并使用新字符串<{1}}更新<{1}} / p>

ListView

<强>更新

您应该考虑进行此更改

addButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        EditText editText = (EditText) findViewById(R.id.edit_choice);
        String message = editText.getText().toString();
        choices.add(message);
        choicesArray = choices.toArray(choicesArray);

        adapter.notifyDataSetChanged();

    }
});