{"id":195,"date":"2011-09-27T15:37:10","date_gmt":"2011-09-27T19:37:10","guid":{"rendered":"http:\/\/digitallibraryworld.com\/?p=195"},"modified":"2011-09-27T15:37:24","modified_gmt":"2011-09-27T19:37:24","slug":"android-listview-and-listactivity-example-from-web-developers-perspective","status":"publish","type":"post","link":"https:\/\/heisbudi.com\/?p=195","title":{"rendered":"Android ListView and ListActivity example from Web Developer&#8217;s perspective"},"content":{"rendered":"<figure id=\"attachment_197\" aria-describedby=\"caption-attachment-197\" style=\"width: 181px\" class=\"wp-caption alignright\"><a href=\"http:\/\/digitallibraryworld.com\/wp-content\/uploads\/2011\/09\/Capture.png\"><img loading=\"lazy\" class=\"size-medium wp-image-197 \" title=\"Android ListView\" src=\"http:\/\/digitallibraryworld.com\/wp-content\/uploads\/2011\/09\/Capture-181x300.png\" alt=\"Android ListView\" width=\"181\" height=\"300\" srcset=\"https:\/\/heisbudi.com\/wp-content\/uploads\/2011\/09\/Capture-181x300.png 181w, https:\/\/heisbudi.com\/wp-content\/uploads\/2011\/09\/Capture.png 493w\" sizes=\"(max-width: 181px) 100vw, 181px\" \/><\/a><figcaption id=\"caption-attachment-197\" class=\"wp-caption-text\">Android ListView<\/figcaption><\/figure>\n<p>So, like I mentioned in my previous blog, I started picking up android recently. It&#8217;s been fun. It&#8217;s confusing sometimes translating web developer&#8217;s framework into Android&#8217;s mindset. This is what I learn. My current self-initiated project is to build a room reservation system for Android. Ball State has an <a title=\"BSU Open Room\" href=\"http:\/\/bsu.edu\/libraries\/openroom\" target=\"_blank\">online room reservation<\/a> system, and I&#8217;m basically building an android app for it. When someone is searching for a room, basically list of rooms will be displayed. This list has 2 rows(pictured on the right).<\/p>\n<p>I&#8217;m so used to being a web developer. To me this looks like a drop down(combo) box where each option has its own unique ID field. Well&#8230;. similar, but a bit different in android. This was somewhat confusing to me because each row in drop down box has only single row. In android, a list item can consist of more than 1 row or column, but still uses one single unique identifier.<\/p>\n<p>Let&#8217;s get crackin&#8217; from the bottom up.<\/p>\n<p>First of all, you can customize pretty much how everything looks in android. You just need to add an XML file to create a layout. Kinda like the CSS in Android in my opinion. So, let&#8217;s define how each row is going to look like. Basically I want each Item to have 2 rows and to look like below.<\/p>\n<figure id=\"attachment_198\" aria-describedby=\"caption-attachment-198\" style=\"width: 300px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/digitallibraryworld.com\/wp-content\/uploads\/2011\/09\/Capture1.png\"><img loading=\"lazy\" class=\"size-medium wp-image-198\" title=\"ListView item\" src=\"http:\/\/digitallibraryworld.com\/wp-content\/uploads\/2011\/09\/Capture1-300x129.png\" alt=\"ListView item\" width=\"300\" height=\"129\" srcset=\"https:\/\/heisbudi.com\/wp-content\/uploads\/2011\/09\/Capture1-300x129.png 300w, https:\/\/heisbudi.com\/wp-content\/uploads\/2011\/09\/Capture1.png 339w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-198\" class=\"wp-caption-text\">ListView item<\/figcaption><\/figure>\n<p>The first row is a TextView, the room number posted outside each room in the library. This will be populated based on the search query sent to a web service I wrote. The second row will be the detail of the room, will also be pulled from the web service. I need to tell Android that I want each of my item in a ListView to look exactly like the one on the left when it&#8217;s filled with data.<\/p>\n<p>This is the XML layout code. I called mine item_listing.xml<\/p>\n<pre lang=\"XML\">\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<LinearLayout\r\n  xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n  android:orientation=\"vertical\"\r\n  android:layout_width=\"fill_parent\"\r\n  android:layout_height=\"fill_parent\" \r\n  android:background=\"@color\/room_listing_background\">\r\n\t<TextView \r\n\t\tandroid:layout_height=\"wrap_content\" \r\n\t\tandroid:id=\"@+id\/txtRoom\" \r\n\t\tandroid:textAppearance=\"?android:attr\/textAppearanceLarge\" \r\n\t\tandroid:layout_width=\"fill_parent\" \r\n                android:background=\"@drawable\/room_listing_states\"\r\n                android:paddingLeft=\"@dimen\/left_padding\" \r\n                android:textColor=\"#ffffff\" \r\n                android:textStyle=\"bold\" \r\n                android:paddingTop=\"5dp\"><\/TextView>\r\n\t<TextView \r\n\t\tandroid:layout_height=\"wrap_content\" \r\n\t\tandroid:text=\"TextView\" android:id=\"@+id\/txtCapacity\" \r\n\t\tandroid:background=\"@drawable\/room_listing_states\" \r\n\t\tandroid:layout_width=\"fill_parent\" \r\n\t\tandroid:textColor=\"#ffffff\" \r\n\t\tandroid:textAppearance=\"?android:attr\/textAppearanceMedium\" \r\n\t\tandroid:paddingLeft=\"15dp\" \r\n\t\tandroid:paddingBottom=\"5dp\" \r\n\t\tandroid:layout_margin=\"0dp\"><\/TextView>\r\n <\/LinearLayout>\r\n\r\n<\/pre>\n<p>Don&#8217;t worry about the background=@drawable code above for now. It&#8217;s yet another layout(drawable) to tell each item how they should be displayed when they are in clicked mode, hover mode, or regular mode. I&#8217;ll post the code for it below.<\/p>\n<p>Okay, I just defined how each item is going to look. Now, I need to define how the screen is going to look. I basically want it to look like the first picture on the right above(Android ListView). The area with white background is 2 TextViews object occupying 2 rows. These 2 rows do not scroll. The red area is a scrollable ListView populated using the XML layout(rule) above for each item. Let&#8217;s call this rooms.xml This is the code:<\/p>\n<pre lang=\"XML\">\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<LinearLayout\r\n  xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n  android:orientation=\"vertical\"\r\n  android:layout_width=\"fill_parent\"\r\n  android:layout_height=\"fill_parent\" android:background=\"@color\/screen_background\">\r\n    <TextView \r\n    \tandroid:layout_width=\"wrap_content\" \r\n    \tandroid:layout_height=\"wrap_content\" \r\n    \tandroid:id=\"@+id\/lblAvailRooms\" \r\n    \tandroid:textColor=\"@color\/label_color\" \r\n    \tandroid:layout_marginLeft=\"@dimen\/left_padding\" \r\n    \tandroid:textAppearance=\"?android:attr\/textAppearanceMedium\" \r\n        android:layout_marginTop=\"10dp\" \r\n        android:text=\"@string\/strAvailableRooms\"\/>\r\n\r\n    \t<TextView \r\n\t    \tandroid:layout_width=\"wrap_content\" \r\n\t    \tandroid:layout_height=\"wrap_content\" \r\n\t    \tandroid:text=\"@string\/strTo\" \r\n\t    \tandroid:id=\"@+id\/lblTime\" \r\n\t    \tandroid:textColor=\"@color\/label_color\" \r\n\t    \tandroid:layout_marginLeft=\"@dimen\/left_padding\" \r\n\t    \tandroid:layout_marginBottom=\"@dimen\/bottom_padding\" \r\n                android:textAppearance=\"?android:attr\/textAppearanceMedium\"\/>\r\n    \t<ListView \r\n                android:layout_height=\"wrap_content\" \r\n                android:layout_width=\"fill_parent\" \r\n                android:id=\"@+id\/android:list\" \/>\r\n<\/LinearLayout>\r\n<\/pre>\n<p>Note the ListView section of the layout. I named the list id <strong>list.<\/strong> This is so that I can shift all the data population work to Android, requiring me to code less(very less). This is the awesome part. I don&#8217;t need to worry about writing loop to populate the list. I just need to provide the data, and shove it to Android. Let&#8217;s do that now. I need to create an activity to display room.xml layout that extends ListActivity. I&#8217;m copying only the onCreate method, which shows how to populate the ListView<\/p>\n<pre LANG=\"JAVA\">\r\npublic class Rooms extends ListActivity {\r\n\tprivate List<HashMap<String, String>> fill;\r\n\tprivate static final String ENC = \"UTF-8\";\r\n\tprivate int selected;\r\n\t@Override\r\n\tpublic void onCreate(Bundle savedInstance){\r\n\t\tsuper.onCreate(savedInstance);\r\n                \/\/use rooms.xml layout\r\n\t\tthis.setContentView(R.layout.rooms);\r\n\t\t\r\n                \/\/set the display text for the white background area. \r\n                \/\/don't worry about this\r\n\t\tTextView lblAvailRoom = (TextView) findViewById(R.id.lblAvailRooms);\r\n\t\tlblAvailRoom.setText(getResources().getString(R.string.strAvailableRooms) + \r\n                \" \" + this.getIntent().getExtras().getString(\"startDay\"));\r\n\t\tTextView lblTime = (TextView) findViewById(R.id.lblTime);\r\n\t\tlblTime.setText(getResources().getString(R.string.strFrom) + \r\n                \" \"+ this.getIntent().getExtras().getString(\"startTime\") + \r\n                \" \" + getResources().getString(R.string.strTo) + \" \" + \r\n                this.getIntent().getExtras().getString(\"endTime\"));\r\n\t\t\r\n                \/\/the rooms parameter passed to this activity contain the result of the web service call.\r\n                \/\/It contains the following string \r\n                \/\/roomid - the unique identifier of a room that will be used in code\r\n                \/\/name - the actual name of the room\r\n                \/\/capacity - capacity of of the room\r\n               \/\/ the are formatted as follow: roomid,name,capacity;roomid,name,capacity\r\n\r\n\r\n                \/\/check to see how many room is available \r\n                \/\/so that i can declare the size of my List\r\n\t\tint length= this.getIntent().getExtras().getString(\"rooms\").split(\";\").length;\r\n\t\t\r\n\t\tString[] rooms = new String[length];\r\n\t\trooms=this.getIntent().getExtras().getString(\"rooms\").split(\";\");\r\n\t\t\r\n\t\tfill = new ArrayList<HashMap<String, String>>();\r\n\r\n                \/\/I'm using HashMap to store my name-value pair of the available room\r\n\t\tfor(int i=0;i<rooms.length;i++){\r\n\t\t\tString[] temp=new String[3];\r\n\t\t\ttemp=rooms[i].split(\",\");\r\n\t\t\tHashMap<String, String> map = new HashMap<String, String>();\r\n\t\t\tmap.put(\"roomid\",temp[0]);\r\n\t\t\tmap.put(\"name\", temp[1]);\r\n\t\t\tmap.put(\"capacity\", temp[2]);\r\n\t\t\tmap.put(\"max\", \"Capacity: \"+ temp[2] + \" People\");\r\n\t\t\t\r\n\t\t\tfill.add(map);\r\n\t\t}\r\n                \/\/If you don't understand what HashMap does, look it up on google.\r\n                \/\/ at this point all my room information is available in a variable called fill\r\n                \/\/ all I need to do is create an adapter, fill the adapter with the fill variable,\r\n                \/\/ and send the adapter to android to process(to fill the layout with data)\r\n            \r\n               \/\/use simple adapter\t\t\r\n               \/\/note that I tell the adapter to use the variable named \"name\" and \"max\"\r\n               \/\/to bing to to text view I specified\r\n               \/\/the \"name\" variable will be displayed on android id txtRoom\r\n               \/\/in item_listing.xml\r\n               \/\/the \"max\" variable will be displayed on android id txtCapacity\r\n              \/\/ in item_lising.xml\r\n\t\tSimpleAdapter adapter = new SimpleAdapter(this, fill, R.layout.item_listing, \r\n                    new String[] {\"name\",\"max\"}, new int[]{R.id.txtRoom,R.id.txtCapacity});\r\n\r\n              \/\/send it off to android ti display on a layout\r\n\t\tsetListAdapter(adapter);\r\n\t}\r\n}\r\n<\/pre>\n<p>Note that the <i>fill<\/i> variable, and the <i>selected<\/i> variable are both global within this class. This means I can track which item is selected anywhere in this class. To track which item is selected, onListItemClick abstract method needs to be implemented<\/p>\n<pre LANG=\"JAVA\">\r\n\tprotected void onListItemClick(ListView l, View v, int position, long id){\r\n\t\tsuper.onListItemClick(l, v, position, id);\r\n\t\tselected=position;\t\t\t\t\r\n\t}\r\n<\/pre>\n<p>Now, you can refer to a clicked item from anywhere in this class by calling:<\/p>\n<pre LANG=\"JAVA\">\r\nfillList.get(selected).get(\"name\")\r\n<\/pre>\n<p>or<\/p>\n<pre LANG=\"JAVA\">\r\n\/\/this is the unique identifier of a selected item in the list\r\nfillList.get(selected).get(\"roomid\")\r\n<\/pre>\n<p>That&#8217;s pretty much it. The hardest part is to understand it from web developer&#8217;s view. <\/p>\n<p>As promised above, this is the XML code that is responsible of how buttons should look in different state:<br \/>\nroom_listing_states.xml<\/p>\n<pre LANG=\"XML\">\r\n\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<selector\r\n  xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\">\r\n\t<item android:state_pressed=\"true\">\r\n\t\t<shape>\r\n\t\t\t<stroke \r\n\t\t\t\tandroid:width=\"@dimen\/stroke_width\"\r\n\t\t\t\tandroid:color=\"@color\/black\" \/>\r\n\t\t\t<gradient \r\n\t\t\t\tandroid:startColor=\"@color\/black\" \r\n\t\t\t\tandroid:endColor=\"@color\/fonts\" \r\n\t\t\t\tandroid:angle=\"270\"\/>\r\n\t\t<\/shape>\r\n\t<\/item>\r\n\t\t<item android:state_focused=\"true\">\r\n\t\t<shape>\r\n\t\t\t<stroke \r\n\t\t\t\tandroid:width=\"@dimen\/stroke_width\"\r\n\t\t\t\tandroid:color=\"@color\/black\" \/>\r\n\t\t\t<gradient \r\n\t\t\t\tandroid:startColor=\"@color\/hover_bg\" \r\n\t\t\t\tandroid:endColor=\"@color\/hover_bg\"\/>\r\n\t\t<\/shape>\r\n\t<\/item>\r\n\t<item>\r\n\t\t<shape>\r\n\t\t\t<solid android:color=\"@color\/button_background\" \/>\t\r\n\t\t<\/shape>\r\n\t<\/item>\r\n<\/selector>\r\n<\/pre>\n<div data-counters='1' data-style='square' data-size='regular' data-url='https:\/\/heisbudi.com\/?p=195' data-title='Android ListView and ListActivity example from Web Developer&#8217;s perspective' class='linksalpha_container linksalpha_app_3'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div><div data-position='' data-url='https:\/\/heisbudi.com\/?p=195' data-title='Android ListView and ListActivity example from Web Developer&#8217;s perspective' class='linksalpha_container linksalpha_app_7'><a href='\/\/www.linksalpha.com\/share?network='facebook' class='linksalpha_icon_facebook'><\/a><a href='\/\/www.linksalpha.com\/share?network='twitter' class='linksalpha_icon_twitter'><\/a><a href='\/\/www.linksalpha.com\/share?network='googleplus' class='linksalpha_icon_googleplus'><\/a><a href='\/\/www.linksalpha.com\/share?network='mail' class='linksalpha_icon_mail'><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Web developer working with Android ListView<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/195"}],"collection":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=195"}],"version-history":[{"count":20,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/heisbudi.com\/index.php?rest_route=\/wp\/v2\/posts\/195\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heisbudi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}