The use of the ListView in the Android Learning path

Source: Internet
Author: User

Learn Android three days, found this ListView in Android application is very much, so it took some time to study carefully!

The following is my personal understanding, if there are errors or not thoughtful place, but also ask you to leave a message! There are mistakes to progress, this is my famous quote!!! Oh!

Simply introduce the following, Listveiw is like a collection of objects, you can display a column of data, and can add click events, very user-friendly operation

Mobile phone comes with more systems.

such as the mobile phone settings interface:


* Layout Design

This interface requires two layout production, one is Mainlayout, which contains a ListView

The code is as follows:

<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= "vertical" ><listview    android:id= "@+id/listview"    android:layout_width= " Wrap_content "    android:layout_height=" wrap_content "    /></linearlayout>
This is the equivalent of an empty collection.

Here is the perlistviewlayout, which is the style of each piece of data

The code is as follows:

<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:orien        tation= "Horizontal" > <imageview android:id= "@+id/imageviewid" android:layout_height= "Wrap_content"         Android:layout_width= "Wrap_content" android:src= "@drawable/ic_launcher"/> <textview Android:layout_width= "20DP" android:layout_height= "Wrap_content"/> <linearlayout Android Oid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "vertical" &        Gt <textview android:id= "@+id/textviewid1" android:layout_height= "Wrap_content" Android:        Layout_width= "Wrap_content" android:textsize= "30sp" android:text= "Content1"/> <textview android:id="@+id/textviewid2" android:layout_height= "wrap_content" android:layout_width= "Wrap_content" android:text= "Content1"/> </LinearLayout></LinearLayout>
The corresponding interface is as follows:


Can be divided into several parts:

This can be the style and layout of each column of data

Every piece of data in the Mainlayout will be displayed in this way.

* Creation of data sources
The first thing to do is to take out the data and display several data

Wear a three array, respectively, into ImageView, TextView1, TextView2

The code is as follows:

Private int[] Picts = {r.drawable.ic_launcher, R.drawable.pict1, R.drawable.pict2, R.DRAWABLE.DOG_BEW}; Private string[] Contents1 = {"Zhangsan", "Lisi", "Wangwu", "Wangliu"};p rivate string[] contents2 = {"Zhangsan_1", "lisi_1 "," Wangwu_1 "," wangliu_1 "};
The id,textview1 content of the picture resource, the content of TextView2. One by one correspondence

Storing data in a list

The code is as follows:

Create data source list<map<string, object>> data;data = new arraylist<map<string,object>> (); for (int i = 0 ; i < picts.length; i++) {map<string,object> oneline = new hashmap<string, object> (); Oneline.put ("Pict", Picts[i]); O Neline.put ("Contents1", Contents1[i]), Oneline.put ("Contents2", Contents2[i]);d ata.add (oneline);}

It's best to use list<map<string,objuct>> here.

Start creating a custom adapter

The code is as follows:

Class Myadapter extends Simpleadapter{public myadapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, int[] to) {Super (context, data, resource, from, to);} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {View view = Super.getview (position, Conve Rtview, parent); ImageView ImageView = (ImageView) View.findviewbyid (R.ID.IMAGEVIEWID); TextView textview1 = (TextView) View.findviewbyid (R.ID.TEXTVIEWID1); TextView textview2 = (TextView) View.findviewbyid (R.ID.TEXTVIEWID2); Imageview.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {toast.maketext (Listviewactivity3.this, "Click on a Picture", Toast.length_short). Show ();}}); Textview1.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {Toast.maketext ( Listviewactivity3.this, "Click on Text 1", Toast.length_short). Show ();}); Textview2.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {toAst.maketext (Listviewactivity3.this, "click on Text 2", Toast.length_short). Show ();}); /Implementation Events ... return view;}}
Notice the view above, the event is click Mainlayout get the ID and text of each control in Perlistviewlayout
Create an Adapter

The code is as follows:

Create Adapter Myadapter adapter = new Myadapter (listviewactivity3.this, data, R.layout.listview_one_linex, from, to); Listview.setadapter (adapter); Listview.setonitemclicklistener (new Adapterview.onitemclicklistener () {@ overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int position,long arg3) {Toast.maketext ( Listviewactivity3.this, "First" + (position+1) + "line", Toast.length_short). Show ();});
Start looking at the from and to parameters,

Context-the context where the View associated with this simpleadapter is runningdata-a List of Maps. Each entry in the list corresponds to one row in the list. The Maps contain the data for each row, and should include all the Entri<span style= "White-space:pre" ></span>e s specified in ' from ' resource-resource identifier of a view layout that defines the click for this list item. The layout file should include at least those named views Defin<span style= ' white-space:pre ' ></span>ed in ' to ' FROM-A List of column names that'll be added to the MAP associated with each item.to-the views that should display C Olumn in the ' from ' parameter. These should all is textviews. The first n views in this list is given the values of T<span style= "White-space:pre" ></span>he First n column s in the From parameter.
English can not understand, see for yourself, my understanding is one by one correspondence
This defines:

Private string[] from = {"Pict", "Contents1", "Contents2"};p rivate int[] to = {R.id.imageviewid, r.id.textviewid1, R.ID.TEXTVIEWID2};
Here's the diagram I made.

This is my own understanding, there must be many places unreasonable, I am understand!!!!




The use of the ListView in the Android Learning path

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.