Android Train--listview bindings Arrayadapter, Simpleadapter, Simplecursoradapter, Baseadapter

Source: Internet
Author: User

ListView Binding Arrayadapter

Res/layout/activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal">    <!--Add a ListView control -    <ListViewAndroid:id= "@+id/lv"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" /></LinearLayout>

Mainactivity.java

 Packagecom.train.openso.myapplication_a;Importandroid.content.Intent;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.EditText;ImportAndroid.widget.ListView; Public classMainactivityextendsactionbaractivity {//(1) define an array to hold the contents of the item in the ListView.     Private Final StaticString [] STRs =Newstring[]{"First", "second", "third", "fourth", "fifth", "sixth"}; PrivateListView LV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (r.id.lv); //=============================================================        //(2) Create a Arrayadapter object by implementing the Arrayadapter constructorArrayadapter<string> Arrayadapter =NewArrayadapter<string> ( This, Android.        R.LAYOUT.SIMPLE_LIST_ITEM_SINGLE_CHOICE,STRS); //(3) Bind Arrayadapter for ListViewLv.setadapter (Arrayadapter);        Lv.setchoicemode (listview.choice_mode_multiple); //to bind a click Listener to the ListView, click the number of rows to display in the title bar after clickingLv.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) {//Click on the title to display click on the first few linesSettitle ("You clicked on the" +position+ "line");     }        }); //================================================================    }}

ListView Binding Simpleadapter

Res/layout/activity_main.xml

<linearlayout    xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "Horizontal" >    <!--Add a ListView control--    <listview        android:id = "@+ Id/lv "        android:layout_width=" fill_parent "        android:layout_height=" Fill_parent "/></linearlayout >

Item.xml---> As a layout for each row

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_height= "Fill_parent"Android:layout_width= "Fill_parent">    <ImageViewAndroid:layout_alignparentright= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/itemimage"        />    <TextViewAndroid:id= "@+id/itemtitle"Android:layout_height= "Wrap_content"Android:layout_width= "Fill_parent"android:textsize= "20SP"        />    <TextViewAndroid:id= "@+id/itemtext"Android:layout_height= "Wrap_content"Android:layout_width= "Fill_parent"Android:layout_below= "@+id/itemtitle"        /></Relativelayout>

Mainactivity.java

 Packagecom.train.openso.myapplication_a;Importandroid.content.Intent;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.EditText;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter;Importjava.util.ArrayList;ImportJava.util.HashMap; Public classMainactivityextendsactionbaractivity {PrivateListView LV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (r.id.lv); //============= binding data to a ListView in Java code//data loading in dynamic arraysarraylistNewArraylist();  for(inti=0;i<9;i++) {HashMap<string, object> map =NewHashmap<string, object>(); Map.put ("Itemimage", R.mipmap.ic_launcher); Map.put ("Itemtitle", "first" +i+ "line"); Map.put ("Itemtext", "This is the first" +i+ "line");        Arraylist.add (map); }        //Create a Arrayadapter object by implementing the Arrayadapter constructor//New Simpleadapter (this, the data source, the layout XML for each row, the data source individual key value, the element ID of the layout of each row); //can be abstracted as: The data source is loaded in the individual specified XML format and bound to Simpleadapter. Simpleadapter Msimpleadapter =NewSimpleadapter ( This, ArrayList, R.layout.item,NewString[] {"Itemimage", "Itemtitle", "Itemtext"},                New int[] {r.id.itemimage,r.id.itemtitle,r.id.itemtext});        Lv.setadapter (Msimpleadapter); Lv.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) {Settitle ("+position+" line ".");        }        }); /*=============================================================//(2) Create a Arrayadapter object by implementing the Arrayadapter constructor arrayadapter<string> arrayadapter = new Arrayadapter<string> (this, Android.        R.LAYOUT.SIMPLE_LIST_ITEM_SINGLE_CHOICE,STRS);        (3) for the ListView binding Arrayadapter Lv.setadapter (Arrayadapter);        Lv.setchoicemode (listview.choice_mode_multiple); To bind a click Listener to the ListView, click the number of rows in the title bar to display the clicked Lv.setonitemclicklistener (new Adapterview.onitemclicklistener () {@                Override public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {            Click on the title to show click on the first few lines Settitle ("You clicked on the" +position+ "line");     }        }); ================================================================*/    }}

Data that uses Simpleadapter is typically a list of hashmap that each section of the list corresponds to each row of the ListView. The data for each key of HashMap is mapped to the corresponding control in the layout file through the Simpleadapter constructor. This layout file is usually defined by its own needs. Comb the steps to use Simpleadapter.

(1) Define the layout implemented by each row of the ListView as needed.

(2) Define a list of HashMap and store the data in the form of key-value pairs.

(3) constructs the Simpleadapter object.

(4) Bind the Lsitview to the Simpleadapter.

----------------=------------

Look at the Baseadapter, a little hold, later study

Reference: http://www.cnblogs.com/noTice520/archive/2011/12/05/2276379.html

Android Train--listview bindings Arrayadapter, Simpleadapter, Simplecursoradapter, Baseadapter

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.