[Listview] Android miscellaneous-Use of simpleadapter in listview

Source: Internet
Author: User

Http://www.cnblogs.com/loulijun/archive/2011/12/27/2303488.html

Simpleadapter is the best scalability adapter that can define various la s you want and is easy to use.

Simpleadapter (Context
Context, list <? Extendsmap <string,?>
Data, int resource, string [] From, int [])

Context: context, such as this. Associate the view context running by simpleadapter

Parameter Data: Map list, the data to be displayed in the list, which needs to be implemented by yourself. For example, in the example, getdata (), the type must be consistent with the above, each project must be consistent with the specified entry in from.

Parameter resource: the ID of the individual layout file of listview. This layout is your custom layout. The layout of what you want to display is in this layout. The Control ID defined in to must be included in the layout.

Parameter from: a list of column names associated with each project on map. The column names are in the array.

Parameter to: an int array. The ID in the array is the ID of each control in the custom layout.

Simpleadapter can use a custom listview and setcontentview. You can also directly use the system's built-in listacitivity. This listactivity implements listview, and a lot of optimizations are made when displaying listview.

Listactivity directly extends listactivity, no need to go to setcontentview

Example 1: Customize the layout to display local resources

If listacitivty is inherited directly, you do not need to customize the listview. The following is the single display format of list items.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal" >    <ImageView         android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="3px"        />    <LinearLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <TextView             android:id="@+id/title"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:textSize="16sp"            />        <TextView            android:id="@+id/info"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:textSize="10sp"            />    </LinearLayout></LinearLayout>

Activity

Package COM. loulijun. demo13; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. listactivity; import android. OS. bundle; import android. widget. simpleadapter; public class demo13activity extends listactivity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); simpleadapter adapter = new simpleadapter (this, getdata (), R. layout. main, new string [] {"IMG", "title", "info"}, new int [] {R. id. IMG, R. id. title, r.id.info}); setlistadapter (adapter);} private list <Map <string, Object> getdata () {list <Map <string, object> List = new arraylist <Map <string, Object> (); Map <string, Object> map = new hashmap <string, Object> (); map. put ("IMG", R. drawable. e001); map. put ("title", "xiaozong"); map. put ("info", "DJ"); list. add (MAP); map = new hashmap <string, Object> (); map. put ("IMG", R. drawable. e002); map. put ("title", "mink"); map. put ("info", "Four beautiful women"); list. add (MAP); map = new hashmap <string, Object> (); map. put ("IMG", R. drawable. e04b); map. put ("title", "Milk Tea"); map. put ("info", "qingchun sister"); list. add (MAP); map = new hashmap <string, Object> (); map. put ("IMG", R. drawable. e04e); map. put ("title", "rhubarb"); map. put ("info", "is a puppy"); list. add (MAP); map = new hashmap <string, Object> (); map. put ("IMG", R. drawable. e11a); map. put ("title", "hello"); map. put ("info", "Every Thing"); list. add (MAP); map = new hashmap <string, Object> (); map. put ("IMG", R. drawable. e11d); map. put ("title", "world"); map. put ("info", "Hello World"); list. add (MAP); return list ;}}

| ------------------------------ Gorgeous split line -------------------------------------------- |

Note: Custom listview also has its advantages, because the inherited listacitivity layout has been fixed, but if we need to implement some effects in listview, such as the quick scroll bar, you need to customize it. In addition, if you inherit other types such as tabactivity

The listacitivty cannot be inherited Because Java is a single inheritance and the custom listview is required at this time.

If you customize listview instead of inheriting listactivity, You need to implement it as follows:

Mylist. xml. Other attributes can be defined in listview.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView         android:id="@+id/listview"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        ></ListView></LinearLayout>

If you use a custom listview, You need to modify the watermark in the above Code. The other parts of acitivity are the same, and the difference is that the gray area

public class Demo13Activity extends Activity {    private ListView lv;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.mylist);        lv = (ListView)findViewById(R.id.listview);        SimpleAdapter adapter = new SimpleAdapter(this, getData(),                R.layout.main, new String[] { "img", "title", "info" },                new int[] { R.id.img, R.id.title, R.id.info });        //setListAdapter(adapter);        lv.setAdapter(adapter);    }

The running effect is as follows:

| ------------------------------------------------------------------------ |

You can customize the layout, such as linear layout or grid layout.

Next, let's talk about the use of viewbinder. The above example shows the local resources. The images are saved locally, but there is a problem with the above method to display the images obtained on the network, if the listview needs to display external resources, you must set viewbinder to display network resources through the binding mechanism of viewbinder. Below is an example of displaying network images (if possible, it is best to use baseadapter)

Example 2: Customize the layout, display network resources, and use viewbinder

To access network resources, first add the permission to your configuration file.

<uses-permission android:name="android.permission.INTERNET"/>

Other la s are the same as above

Package COM. loulijun. demo13; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. malformedurlexception; import java.net. URL; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. bundle; import android. view. view; import android. widget. imageview; import android. widget. listview; import android. widget. simpleadapter; import android. widget. simpleadapter. viewbinder; public class demo13activity extends activity {private listview lv; Private Static final string iphoneurl = "http://www.51aigoo.com/images/20100107/6b21df8c2419480e.jpg"; Private Static final string macbookprourl = "http://www.esundigi.net/images/goods/20110317/6ece8f319694f0b1.jpg "; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. mylist); Lv = (listview) findviewbyid (R. id. listview); simpleadapter adapter = new simpleadapter (this, getdata (), R. layout. main, new string [] {"IMG", "title", "info"}, new int [] {R. id. IMG, R. id. title, r.id.info}); // setlistadapter (adapter); adapter. setviewbinder (New myviewbinder (); LV. setadapter (adapter);} // obtain network image resources. The returned type is bitmap, which is used to set public bitmap getbitmap (string httpurl) {bitmap BMP = NULL in listview; // retrieve the network image in listview. Try {URL url = new URL (httpurl); httpurlconnection conn = (httpurlconnection) URL. openconnection (); inputstream is = Conn. getinputstream (); BMP = bitmapfactory. decodestream (is);} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();} return BMP;} // Private list of data to be displayed on listview <Map <string, Object> getdata () {list <Map <string, object >>> list = new arraylist <Map <string, object >>(); Map <string, Object> map = new hashmap <string, Object> (); // set whether the bound data is an image map. put ("IMG", getbitmap (iphoneurl); map. put ("title", "iphone4"); map. put ("info", "can be viewed remotely but cannot afford Pipeline"); list. add (MAP); map = new hashmap <string, Object> (); map. put ("IMG", getbitmap (macbookprourl); map. put ("title", "MacBook Pro"); map. put ("info", "Buy and play next year"); list. add (MAP); return list ;}/// implement the viewbinder Interface Class myviewbinder implements viewbinder {/*** view: View of the top data * Data: data to be bound to the view * textrepresentation: A string indicating the security of the supported data. The result is data. tostring () or an empty string, but cannot be null * return value: if the data is bound to the view, return true, otherwise false */@ override public Boolean setviewvalue (view, object data, string textrepresentation) {If (view instanceof imageview) & (Data instanceof Bitmap) {imageview IV = (imageview) view; bitmap BMP = (Bitmap) data; IV. setimagebitmap (BMP); Return true;} return false ;}}

Running result:

Article selection:

Farmer's uncle: http://www.cnblogs.com/over140/archive/2010/11/24/1886151.html

Http://www.cnblogs.com/over140/archive/2010/12/15/1906303.html

Viewbinder solution: http://www.anddev.org/listview_simpleadapter_and_bitmaps_-_bug_-t11817.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.