I have created an layout which contains the listview. I have added a button to it, which on clicked gives an alert dialogue box with an edit text box and two options OK and CANCEL.
I want to add the item which the user enters in the edit text box into the listview. Can any one help me out how to add it?
this is the code which shows me the Alert dialogue box, the value has to be shown as the items of the 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();
}
You should use an ArrayAdapter and bind it to your ListView. When the user enters a new value, add it to the ArrayAdapter and call mListView.notifyDataSetChanged().
There is a good example of setting up a ListView and ArrayAdapter here http://anujarosha.wordpress.com/2011/11/17/how-to-create-a-listview-using-arrayadapter-in-android/