我创建了一个包含 ListView 的布局。我在其中添加了一个按钮,单击该按钮会出现一个带有编辑文本框和两个选项“确定”和“取消”的警告对话框。 我想将用户在编辑文本框中输入的项目添加到 ListView 中。谁能帮我看看怎么添加?
这是向我显示警报对话框的代码,值 必须显示为 ListView 的项目。
if(v.getId() == addbtn.getId())
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Add new location");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
最佳答案
您应该使用 ArrayAdapter 并将其绑定(bind)到您的 ListView。当用户输入新值时,将其添加到 ArrayAdapter 并调用 mListView.notifyDataSetChanged()。
这里有一个很好的设置 ListView 和 ArrayAdapter 的例子 http://anujarosha.wordpress.com/2011/11/17/how-to-create-a-listview-using-arrayadapter-in-android/
关于java - 如何将用户在编辑文本框中输入的文本添加到 ListView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13009864/