Android ListView Part II



Welcome again to Android ListView. What we are gonna do is build up on the progress of Part I, and add some new concepts such as Intents and SearchManager to it. So let's begin:

 1.  Create a new Android Application Project

  • Follow up on Part I for the basic app setup process or create new by going to File > New > Android Application Project.

New Android Appication

 2.  Add List View to activity_main.xml

  • Add the ListView to the graphical layout of the app from the Palette by dragging and dropping from the Composite Section.
  • Remember to set the android:id to "@andorid:id/list".

Empty ListView

 3.  Code away the MainActivity.java

  • Add the code below to your MainActivity.java.
  • For code explanation scroll to the end of the post. 

 4. Run the app

  • Launch the app in an AVD or on a physical device by pressing CTRL + F11.
Main Screen

Search Results

 Code Explained:

  1. The java code in this tutorial builds up on the ListView Part I. The new thing here is that this app now can search the name in the list item clicked onto the Internet.
  2. This is accomplished by adding an Intent to the app. Intents are used to describe run time actions to be done by the app. Through Intents we can call any other activity or perform any default actions provided by the system (e.g, Intent.ACTION_WEB_SEARCH).
  3. We add this Intent in the onListItemClick() method of the ListActivity which is called when any list item is selected by clicking touching.
  4. Another new thing here are the Toasts. A Toast is like a real toast which pops up to notify you about your choices or any thing else. The function Toast.makeText( Context, String, Length).show() is used to display Toasts.
  5. Now we make a search string, put it in the Extras field of our intent under the SearchManager.Query name. Its similar to a name:value pair.
  6. Finally we start the intent by calling startActivity(my_intent).

 Download Code:


SHARE

Shobhit Chittora

Hi there, I am Shobhit Chittora. I am a college student and like to develop apps for Android platform.I started this blog to share my experiences with Android development and may be help you guys on the “roadtodroid”.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment