How do you rate this blog

Saturday, November 19, 2011

ListViews with Alert Dialog


In this post I have used both List views as well as Alert Dialog components of android. So what does a list view mean? A list view in simple words is a collection of scrollable items displayed as a list. An alert dialog is a small window which opens up based on some selection usually to alert the user on some activity.
                
      In order to use both of them the flow is as follows, I have created an array which has names of cars which will be displayed in the list view. When the user clicks on the list item an alert dialog will come up which will display “You have selected <car_name>”.
So lets get on with the coding part, first open the main.xml file. Delete the existing textbox and add a new large text box and give the name of the app in it. Then add the list view component into the layout. The Main.xml file will look as follows.



Now that we are done with the layout, we have to add functionality for the same. So go to the mainclass java file, as you see that the class already extends Activity so lets change this to ListActivity. We will then remove the setContentView function which is already present since we will using the one of the default layouts for the listview called android.R.layout.simple_list_item_1 in the array adapter. Then create an array of string which will have the contents of the list. Then create a new ArrayAdapter called mylist which will be for the current context , layout and the array defined previously. So the code will look like below
super.onCreate(savedInstanceState);
String[] actionList=new String[]{"Ferrari","BMW","Porche"};       
ArrayAdapter<String> mylist=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,actionList);
setListAdapter(mylist);

Now  that we have defined the listview we will make alert dialog box to apper when the user clicks on the listview items.
So override the function onListItemClick function and obtain the item which has been selected to a string as shown below.
protected void onListItemClick(ListView l, View v, int position, long id) {
String pos=(String) getListAdapter().getItem(position);
Then create an action dialog as shown below.
AlertDialog.Builder alerter=new AlertDialog.Builder(ListViewDemoActivity.this);
Then set the title and message as shown below.
alerter.setTitle("AlertDilog Demo");
alerter.setMessage("You have selected :" +pos );
Now we have to declare a button which can be associated to any task which you would like to perform, here I have set only one called OK and no action has been set for this, so when the user clicks on OK it will take him back to the list view and no action will be performed.
alerter.setPositiveButton("OK", null);
alerter.show();}
The code will be like in the screen shot shown below



Now that we are done right click on the project and say RunAs android application, once it runs, you will get a screen shot as shown in the image below.



Click on any of the list view items, here I have clicked on Ferrari and you will get a dialog box as shown below.



Once you click OK button it will again take you back to the main screen. Hope you were able to get what has been explained.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete