Combination of Android's ListView and Simpleadapter

Source: Internet
Author: User

The Simpleadapter is a simple adapter. The prepared data can be displayed in the ListView. More information can be found in the official Android API Development documentation.

Example Description: Content in Simpleadapterlistviewactivity.java:

package com.zzh.day_listview;import java.util.arraylist;import java.util.hashmap;import  Java.util.list;import java.util.map;import android.app.activity;import android.os.bundle;import  android.view.View;import android.widget.AdapterView;import  android.widget.adapterview.onitemclicklistener;import android.widget.listview;import  The use of Android.widget.simpleadapter;import android.widget.toast;/**simpleadapter in the ListView  * @ Author administrator * */public class simpleadapterlistviewactivity extends  Activity{SimpleAdapter adapter; listview lv; list<map<string, object>> data = new arraylist<map<string,  Object>> ();//The data to be displayed map<string, object> map;//@Overrideprotected  void oncreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.simple_listview); LV  =  (LiStview)  findviewbyid (r.id.listview1);//  Initialize data for  (int i = 1; i <  21; i++) {map = new hashmap<string, object> (); Map.put ("Key1", i  +  " <--key1"), Map.put ("Key2",  i+ " <--key2");d ata.add (map);} /* * simpleadapter the meaning of the parameters in the construction method: *  parameter one: To display the activity *  parameter two: The data to be displayed. This data must be of type list<? extends map<string, ?>> , and the keys in the Map must be of type String.  *  parameter three: a custom layout file. Two textview are defined because I only put two map objects when initializing the data.  *  parameter four: is a string type of array, which is placed in the parameter two in the map  key array, and must be, this also shows that the key in the map must be a string type of reason  *  Parameter five: An array of type int that holds the ID of the location (View) where the value stored in the map in parameter two is placed.  *  Here you can see a map collection as an item in a ListView, a row of data, and a key that represents the data that each view,value is to display.  * */adapter = new simpleadapter (This, data, r.layout.simple_listview_item, new string[] {"Key1", "Key2"}, new int[] { r.id.textview1, r.id.textview2&nbSP;}); Lv.setadapter (adapter);//Display data in the ListView Lv.setonitemclicklistener (New onitemclicklistener ()// Register Event {@Overridepublic  void onitemclick (Adapterview<?> parent, view view) to the ListView , Int position, long id) {Toast.maketext (Getapplicationcontext (),  "clicked" +position+ ",  Toast.length_long). Show ();}});}}
Content in Simple_listview.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout 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/listview1" android:layout_width= "Match_parent" Android:layout_h eight= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" > < ;/listview></relativelayout>

Content in Simple_listview_item.xml:

<?xml version= "1.0"  encoding= "Utf-8"? ><relativelayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Match_parent "     android:layout_height= "match_parent"     android:orientation= "vertical"   >    <textview        android:id= "@+id/ TextView1 "        android:layout_width=" Wrap_content "         android:layout_height= "Wrap_content"          android:layout_alignparentleft= "true"         android: Layout_alignparenttop= "true"         android:textsize= "26SP"/>     <textview        android:id= "@+id/textView2"          android:layout_width= "Wrap_content"         android:layout_height= " Wrap_content "        android:layout_alignparentright=" true "         android:layout_alignparenttop= "true"          android:textsize= "26SP"         android:textcolor= "@ Android:color/holo_blue_light " /></RelativeLayout>

Operating effect:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/66/09/wKiom1UrMv6iM5AhAAF9-rHaPVA225.jpg "title=" 2015-04-13_103522.png "alt=" Wkiom1urmv6im5ahaaf9-rhapva225.jpg "/>

This article is from the "Meu Late injury" blog, please be sure to keep this source http://zzhhz.blog.51cto.com/7107920/1631765

Combination of Android's ListView and Simpleadapter

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.