Android ListView examples

Source: Internet
Author: User

There are three implementation methods, from simple to deep. Pay attention to the usage of the Adapter. In fact, if you read the Android documentation, you will find many adapters,

If you are not quite clear about the adapter mode, you can first make up for this knowledge. In actual work, the design pattern is a good helper.

Two layout files:

Main. xml

<?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="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />        <ListView         android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        ></ListView></LinearLayout>

Listview. xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linerlayout1"    android:orientation="vertical"    android:layout_height="fill_parent"    android:layout_width="fill_parent"    >       <TextView          android:id="@+id/person_name"          android:textSize="23sp"          android:layout_width="wrap_content"          android:layout_height="wrap_content"       />       <TextView          android:id="@+id/person_age"          android:layout_width="wrap_content"          android:layout_height="wrap_content"      />      <TextView          android:id="@+id/person_email"          android:layout_width="wrap_content"          android:layout_height="wrap_content"      />      <TextView          android:id="@+id/person_address"          android:layout_width="wrap_content"          android:layout_height="wrap_content"      />  </LinearLayout>  

Activity: LincListViewActivity. java

Package com. linc. listview; import android. app. activity; import android. OS. bundle; import android. widget. arrayAdapter; import android. widget. listView; public class LincListViewActivity extends Activity {private final static String [] data = {"Zhang Fei", "Zhang Liao", "Zhang Jiao", "Zhang Sanfeng", "Zhang ", "Zhang Dengcai", "Zhang zolin", "Zhang Damin"}; // create a data source. zhang [] data2 = new Zhang [] {new Zhang ("Zhang Fei", 38, "zhangfei@gmail.com", "Yanshan"), new Zhang ("Zhang Liao", 36, "zhangliao@sina.com", "Yumen"), new Zhang ("Zhang Jiao", 51, "zhangjiao@gmail.com", "Lu"), new Zhang ("Zhang Sanfeng", 200, "sanfeng@gmail.com ", "Liaodong"), new Zhang ("", 25, "5zhao@gmail.com", ""), new Zhang ("", 25, "5zhao@gmail.com ", "Jizhou"), new Zhang ("Zhang zolin", 25, "5zhao@gmail.com", "Jizhou"), new Zhang ("Zhang Damin", 25, "5zhao@gmail.com ", ""), new Zhang ("", 25, "5zhao@gmail.com", "")};/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); ListView listview = (ListView) findViewById (R. id. listview);/** first: normal String */ArrayAdapter <String> adapter1 = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, data);/** type 2: Literary objects */ArrayAdapter <Zhang> adapter2 = new ArrayAdapter <Zhang> (this, android. r. layout. simple_list_item_1, data2);/** third: Custom adapter */ListAdapter adapter3 = new ListAdapter (this, R. layout. listview, data2); listview. setAdapter (adapter3 );}}

Data Object: Zhang. java

package com.linc.listview;public class Zhang {           private String name;      private int age;      private String email;      private String address;      public String getName() {return name;}public int getAge() {return age;}public String getEmail() {return email;}public String getAddress() {return address;}       public Zhang(String name, int age, String email, String address) {          super();          this.name = name;          this.age = age;          this.email = email;          this.address = address;      }         @Override      public String toString() {          return "Person [name=" + name + ", age=" + age + ", email=" + email                  + ", address=" + address + "]";      }     }  

Adapter class: ListAdapter. java

Package com. linc. listview; import android. content. context; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. arrayAdapter; import android. widget. textView; public class ListAdapter extends ArrayAdapter <Zhang> {private LayoutInflater mInflater; public ListAdapter (Context context, int textViewResourceId, Zhang [] obj) {super (context, textViewResourceId, obj ); // TODO Auto-generated constructor stubthis. mInflater = LayoutInflater. from (context) ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {if (convertView = null) {// create a new view. convertView = mInflater. inflate (R. layout. listview, null);} ViewHolder holder = null; if (holder = null) {holder = new ViewHolder (); // you can find the child views in each ViewItem, put it in holder. name = (TextView) convertView. findViewById (R. id. person_name); holder. age = (TextView) convertView. findViewById (R. id. person_age); holder. email = (TextView) convertView. findViewById (R. id. person_email); holder. address = (TextView) convertView. findViewById (R. id. person_address); // Save the reference object convertView of each subview in each displayed ViewItem. setTag (holder);} else {holder = (ViewHolder) convertView. getTag () ;}// obtain the data currently to be displayed. Zhang person = getItem (position); holder. name. setText (person. getName (); holder. age. setText (String. valueOf (person. getAge (); holder. email. setText (person. getEmail (); holder. address. setText (person. getAddress (); return convertView;} private static class ViewHolder {TextView name; TextView age; TextView email; TextView address ;}}

A more complex example: coexistence of a single row and multiple rows

You can also refer to: View Tag Introduction

Or compare the GridView to learn their similarities and differences. Android custom GridView adds multiple text boxes to an image.

One of their adapters is inherited from ArrayAdapter, and the other is BaseAdapter.

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.