我正在开发一个使用 a 的程序,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)