Android教程(九)– ListView控件的使用

今天我們來說說這個ListView控件的使用,ListView其實在任何一款APP(大型)裡都會用到的,當然高級的可能是自定義的,但是它們也是繼承瞭ListView這個父類的。

好瞭我們就直奔主題吧。

第一步,建立一個工程Ep.ListView,其餘命名不更改。

第二步,在視圖上添加一個ListView:

[java] 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 
 
    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/hello_world" /> 
 
    <ListView 
        android:id="@+id/listView1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView1" 
        android:layout_below="@+id/textView1" > 
    </ListView> 
 
</RelativeLayout> 

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1" >
    </ListView>

</RelativeLayout>
第三步,寫核心代碼:

[java] 
package com.example.ep.listview; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.widget.ListView; 
import android.widget.ArrayAdapter; 
 
public class MainActivity extends Activity { 
     
    static final String[] COUNTRIES = new String[]{ 
              "Americian","Belize","China","Japan","Korean","Russian" 
             }; 
    private ListView lv; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        lv=(ListView)findViewById(R.id.listView1); 
         
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 
                 ,COUNTRIES)); 
    } 
 
    private void setListAdapter(ArrayAdapter<String> arrayAdapter) { 
        // TODO Auto-generated method stub  
        lv.setAdapter(arrayAdapter); 
    } 
 

package com.example.ep.listview;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.ArrayAdapter;

public class MainActivity extends Activity {
 
 static final String[] COUNTRIES = new String[]{
     "Americian","Belize","China","Japan","Korean","Russian"
    };
 private ListView lv;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  lv=(ListView)findViewById(R.id.listView1);
  
  setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1
           ,COUNTRIES));
 }

 private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
  // TODO Auto-generated method stub
  lv.setAdapter(arrayAdapter);
 }

}

這裡主要是把一個已經定義好的數組的值賦給這個ListView,我們還用到瞭列表適配器。

 

最後上一個結果圖:

 
 

You May Also Like