Android——關於ListView控制項的使用方法

來源:互聯網
上載者:User

 

本文將就“ListView控制項的使用方法”進行簡單說明:

1. 建立一個Empty Android Application  Project:

建立項目成功後,eclipse預設為使用者構建了一個基本的HelloWorld應用程式架構,在主介面的Layout中,使用者將看到螢幕正中間有一個TextView控制項,其中的文字內容為:HelloWorld;

2. 建立的項目是基於Activity的,但是在本文中將講述ListView控制項的使用方法,故而需將Activity替換為ListActivity,操作如下:

  a) 開啟項目的Work Path中的src檔案夾下的com.xxx.xxx檔案夾,其中儲存著項目的來源程式檔案“*.java ”,如“MainActivity.java”,將檔案中的欄位“extends Activity" 替換為"extends ListActitivy”;

  b) 在MainActivity.java檔案頭,增加"import android.app.ListActivity;";

  c) 開啟項目的Work Path中的res檔案夾下的layout檔案夾,其中儲存著項目的介面檔案"*.xml",如"MainActivity.xml“,在此介面檔案中增加一下ListView控制項,並將該新控制項id改為"@android:id/list"

注釋(摘自Android開發官網):

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To
do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if
it's in code).

3. 至此,項目的基本架構已經建好,使用者可編譯並運用此項目,可以從虛擬機器的模擬環境中看至一個空的介面,這是因為使用者尚未向ListView中增加Item。如果程式在運行過程中,出現崩潰的情況,說明操作有誤,使用者可以按第2步操作中的說明,對項目進行修改、完善。

 

4. 下一步,按使用者需求,向ListView中增加新的Item:

a) 開啟scr目錄中com.***.***檔案夾下的MainActivity.java源檔案,在OnCreate函數的最下方或其它適當的位置,增加以下代碼:

//Simulation Database.String[] str = {"aaa","bbb","ccc","ddd","eee","fff"};setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, str));//android.R.layout.simple_list_item_1 -> @android:id/list/* *Official Reference: *  * ListActivity has a default layout that consists of a single,  * full-screen list in the center of the screen.  * However, if you desire, you can customize the screen layout  * by setting your own view layout with setContentView() in onCreate().  * To do this, your own view MUST contain a ListView object  * with the id "@android:id/list" (or list if it's in code) */getListView().setTextFilterEnabled(true);

b) 在MainActivity.java源檔案的頭部,增加以下代碼:

import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ListAdapter;

c) 編譯項目,在模擬介面的ListView控制項中,將出現"aaa","bbb","ccc"等共7條的Item。至此,關於ListView控制項的建立工作算是成功了。

(End)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.