Android開發之ListView的使用

如何使用ListView來顯示數據?

1.讓MainActivity繼承ListActivity類

2.在main_layout中定義一個Listview控件,並設置id爲android:list(因爲之後使用到的SimpleAdapter默認關聯的id爲list)

3.定義一個佈局文件,用來規定ListView中的數據顯示格式

4.添加數據

5.設置Adapter

MainActivity.java:

package com.mycompany.listview;

import android.app.ListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.item,new String[]{"name", "number"}, new int[]{R.id.name, R.id.number});
        setListAdapter(adapter);
    }

    private List<HashMap<String, String>> getData(){
        // 生成List對象
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        // 生成HashMap對象,用來存放一行數據
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", "police");
        map.put("number", "110");
        list.add(map);      // 注意,此時add進去的只是map的引用
        HashMap<String, String> map2 = new HashMap<String, String>();
        map2.put("name", "doctor");
        map2.put("number", "120");
        list.add(map2);
        HashMap<String, String> map3 = new HashMap<String, String>();
        map3.put("name", "fireman");
        map3.put("number", "119");
        list.add(map3);

        return list;
    }
}
注意:

SimpleAdapter的構造函數的第一個參數爲當前Activity;

第二個參數爲List對象,我用getData()代替,getData()的作用就是爲List對象添加數據,最後返回該List對象;

第三個參數爲自定義的數據顯示格式佈局文件;

第四個參數爲String數組,元素爲所有要顯示的鍵值對中的鍵;

第五個參數爲Int數組,元素爲自定義佈局文件中用來顯示數據的控件id。


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="com.mycompany.listview.MainActivity">

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
</RelativeLayout>

item.xml:(自定義的佈局文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/name"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/number"
        android:gravity="right"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
</LinearLayout>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章