Saturday, January 21, 2012

Download : Static ListView with Images in Android using SimpleAdapter

Build : Android 2.3.3





Hi, Today we will learn how to develop static ListView with Images in Android. As you would be knowing that using Simple Adapter we can map static data to views in XML. Generally we use ArrayList to display list of arrays in Simple Adapter. Each entry in the ArrayList corresponds to one row in the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views.

In the beginning declare ArrayList that uses HashMap to map objects 

private ArrayList <HashMap<String, Object>> socialmedia;

Then onCreate method you define ListView

ListView listView = (ListView)findViewById(R.id.mylistview);

and start mapping objects one by one as shown below :

socialmedia = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hm;
        
//With the help of HashMap add Key, Values of Social Media, like title and images path
              
hm = new HashMap<String, Object>();
     hm.put(LISTNAME, "Blinklist");
     hm.put(IMAGENAME, R.drawable.blinklist); 
     socialmedia.add(hm);

Once we do this we define Simple adapter to map our objects with views in XML
Here we will have to XML files 
1) main.xml (which will represents ListView)
2) listbox.xml (which will represents TextView and Images)


SimpleAdapter adapter = new SimpleAdapter(this, socialmedia, R.layout.listbox,
new String[]{LISTNAME,IMAGENAME}, new int[]{R.id.list, R.id.images});

and thus set adapter 

listView.setAdapter(adapter);

and that's it TADA. We are done. Wasn't that Simple :)

If you want any further onClick actions you can use "OnItemClickListener"


OnItemClickListener listener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            // Do Something onClick
            }
}; 
 
 

Thank you :) See you next time with another beautiful tutorial


Hello! If you have not signed, subscribe to our RSS feed feed and receive our email updates.
Name: Email:

0 comments:

Post a Comment