[Android學習筆記2]ListView

來源:互聯網
上載者:User

列表(List)視圖可以將某種控制項按照列表的形式組織起來。

ListActivity 擴充了Activity可以方便 ListView的使用。

ListActivity 類實際上整合了Activity和ListView 的功能,其內部包含了一個ListView ,使用這個類可以直接構造介面中的列表視圖。

ListView的資料來源可以是ArrayAdapter、SimpleAdapter、SimpleCursorAdapter

我們直接看使用SimpleAdapter的例子:

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="vertical" >"<ListView         android:id="@id/android:list"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        />    <TextViewandroid:id="@id/android:empty"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="sorry, no display"/></LinearLayout>

一個LinearLayout布局中含有一個ListView,預設ID為android:list;

ID為android:empty的TextView的意思為若ListView中無資料的話,則顯示該TextView(sorry, no display);

simplelayout.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <TextView         android:id="@+id/tv_name"        android:layout_width="wrap_content"    android:layout_height="wrap_content"        />        <TextView         android:id="@+id/tv_profession"        android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_toRightOf="@id/tv_name"    android:paddingLeft="20dp"        /></RelativeLayout>

MainActivity.java

package com.example.listview;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import android.app.ListActivity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;public class MainActivity extends ListActivity {      //繼承ListActivity類ArrayList<Map<String, Object>> data;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);PrepareData();//R.layout.simplelayout 為ListView一列的布局,在這裡為RelativeLayout相對布局//R.id.tv_name         第一個TextView的ID//R.id.tv_profession   第二個TextView的ID//data為ArrayList結構,其中的成員為Map資料類型//String[]為Map中索引值的keySimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.simplelayout, new String[]{"姓名","職業"}, new int[]{R.id.tv_name, R.id.tv_profession});setListAdapter(adapter);setContentView(R.layout.activity_main);}//重寫onListItemClick方法,實現列表按鍵事件響應@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);//顯示到面板的標題列中setTitle(l.getItemAtPosition(position).toString());}private void PrepareData(){data = new ArrayList<Map<String, Object>>();Map<String, Object> item ;item = new HashMap<String, Object>();item.put("姓名", "張三");item.put("職業","碼農");data.add(item);item = new HashMap<String, Object>();item.put("姓名", "李四");item.put("職業","產品經理");data.add(item);item = new HashMap<String, Object>();item.put("姓名", "王五");item.put("職業","銷售");data.add(item);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

 

運行效果:


點擊中間一列後,標題列的變化:


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.