LWUIT and J2ME – The detail view


This tutorial is the third part of building the JKUAT Mess application. In this tutorial we are going to learn how to display the details of an item when a list is clicked. At the end of tutorial you should be having an application that flows from end to end. At the end of this tutorial you should have an application similar to the screen shots below

Meal selection menu

Meal selection menu

List of available food

List of available food

Detail view of a particular food item

Detail view of a particular food item

 

 

 

 

 

 

 

 

1.    The first thing we are going to do is to make our FoodList instance a class variable so that we are able to access it everywhere in the application. To do this just do the following declaration as a class variable

  • FoodList[] foodArray

After doing that in the showFoodList method all we need to do is instantiate the FoodList class as follows

  • foodArray = new FoodList[theImgs.length];

2.    Please look carefully at the showFoodList method, I have added some more values to the foodArray instance. I have declared the following arrays to hold more details about our food

  • String priceArray – holds the prizes of our food
  • String descArray – holds some details about the food in question

In the for loop for populating the foodArray instance I have added the following lines of code

  • foodArray[i].setPrice(prizeArray[i]);
  • foodArray[i].setDetails(descArray[i]);

3.    We are now going to create another method called showDetails. This method will be used to display an item’s details when selected on the list. The method signature is as follows

  • public void showDetails(int index)

The parameter is the index of the selected list item. This method is invoked in the actionPerformed method when a list selection is done and the index of the selected item is passed as a parameter. The code snippet for this is as follows

  • int index=foodList.getSelectedIndex();
  • showDetails(index);

I have tried my best to comment the code in order to make it easier for you to understand what is going on.
You can download the code here. You can also get the tutorial in pdf format here

If you have any questions please feel free to ask . Next I am going to work on a tutorial on how to connect your J2ME application to the internet so keep it here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s