android 繼承listview,Android listView 繼承ListActivity的用法

Android listView 繼承ListActivity的用法 在手機中經常有列表方式。如果Activity中只有唯??個List(這也是通常的情況),可以繼

承ListActivity來實現。我們用兩個例子來學習List。

List例子?:利用Android自帶的List格式

步驟?:Android XML文件

ListActivity是?個全屏的list,如果我們需要定制layout,例如加上?個button之類的什么的,我們

需將ListView 的id設置為“@android:id/list”,我們可以同getListView來獲取ListView的對象,下面

是XML的例子:

....

... ?

方式,即設置

@android :id/list ?-->

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:drawSelectorOnTop="false" />

步驟二:源代碼

1)使用String數組設置List的item內容

例如:String[] items={"One", "Two", "Three", "Four", "Five"};

2)在onCreate中將item的內容加入,使用setListAdapter并設置List的格式

setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,items));

ArrayAdapter有三個參數,第?是對象,第二是List的格式,第三個是List的內容。對于格式這個

例子采用了Android自定義的 幾種格式,后面會具體列出。android.R.layout.simple_list_item_1

是TextView的方式,采用big font,是最為常見的方式。

3)點擊List的Callback方法

由于集成了ListActivity,可以直接使用回調函數onListItemClick

4)例子

public class BeginingBook extends ListActivity {

String[] items={"One","Two","Three","Four","Five","Six","Serven"};

private Button button;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setListAdapter(new ArrayAdapter( this ,

android.R.layout.simple_list_item_1, items ));

button =(Button)findViewById(R.id.chapter7);

}

public void onListItemClick (ListView parent,View v, int position, long id){

button.setText(items[position]); //position是點擊的item的序號,從0開始。

}

}

5)不同的呈現模式

我們修改ArrayAdapter的第二個顯示的模式。另外可以通過getListView()獲取ListView的對象,設

置ListView 的?些屬性,ListView的?些常用方法

:setChoiceMode(),getCheckedItemPositions() 。

setListAdapter (new

ArrayAdapter(this, android.R.layout.simple_list_item_single_choice

,items));

getListView().setChoiceMode (ListView.CHOICE_MODE_SINGLE); //表明有選項,

如果不設置,缺省為none,即我們點擊后仍無反應

setListAdapter (new

ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice

,items));

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); //注意,只要點

擊就會觸發Callback,而不過是的點擊后選上還是變為取消。

setListAdapter (new

ArrayAdapter(this, android.R.layout.simple_list_item_checked ,items));

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

下面,第?個圖為android.R.layout.simple_list_item_1,接著依次為上面三種格式。

7d94397a844139f7cebbd9fecab61f81.png List例子二:自定義的List格式(來自Tutorial)

步驟?:建立?個描述list item格式的XML文件

android:id="@+id/rowtv1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="10dip"

android:textSize="16dip">

步驟二:通過XML定義item

在第?個例子中,我們使用String[]來定制item。但是之前,我們也學習到,為了適合多種語言,

最要這些信息都在XML文件中定義,只需更換XML文件,就可以適配不同的語言。我們

在string.xml文件中增加相關的定義:

… …

Bharain

Bangladesh

Barbados

Belarus

Belgium

Belize

Benin

步驟三:源代碼

public class Chapter7Test2 extends ListActivity {

public void onCreate (Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

String[]

fafc74cc2c5302205aa76597def7623c.png contries=getResources().getStringArray(R.array.countries_arry);?

setListAdapter(new ArrayAdapter(this, R.layout.chapter_7_test2_list,

contries));

ListView lv=getListView ();

lv.setTextFilterEnabled(true);

lv.setOnItemClickListener (new OnItemClickListener(){

public void onItemClick(AdapterView> parent, View view, int position, long id){

//Toast.makeText(Chapter7Test2.this, ((TextView) view).getText(),

Toast.LENGTH_SHORT).show();

Toast.makeText(getApplicationContext(), ((TextView) view).getText(),

Toast.LENGTH_SHORT).show();

}

});

}

}

Toast

上面給出Toast的例子。Toast.makeText(對象,文本內容,格式).show()。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540815.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540815.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540815.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

計算機圖形學的應用

Some of the applications of computer graphics are, 計算機圖形學的一些應用是 Education and Training 教育和培訓 Use in Biology 用于生物學 Computer-Generated Maps 計算機生成的地圖 Architect 建筑師 Presentation Graphics 演示圖形 Computer Art 電腦美術 Entertai…

html頁面授權碼,spring boot 2.0 整合 oauth2 authorization code授權碼模式

oauth2 authorization code 大致流程用戶打開客戶端后,客戶端要求用戶給予授權。用戶同意給予客戶端授權。客戶端使用授權得到的code,向認證服務器申請token令牌。認證服務器對客戶端進行認證以后,確認無誤,同意發放令牌。客戶端請…

Net設計模式實例之代理模式(Proxy Pattern)

一、代理模式簡介(Brief Introduction) 代理模式(Proxy Pattern)對其他對象提供一種代理以控制對這個對象的訪問。 二、解決的問題(What To Solve) 1、遠程代理 遠程代理,也就是為了一個對象…

c語言存儲類_C編程語言的存儲類

c語言存儲類A variables storage class tells us the following, 變量的存儲類告訴我們以下內容: Where the variables would be stored? 變量將存儲在哪里? What will be the initial of the variable, if the initial value is not specifically ass…

jsonp請求html頁面,JavaScript中的JSON和JSONP

簡單地使用json并不能支持跨域資源請求,為了解決這個問題,需要采用jsonp數據交互協議。眾所周知,js文件的調用不受跨域與否的限制,因此如果想通過純web端跨域訪問數據,只能在遠程服務器上設法將json數據封裝進js格式的…

2017軟件工程實踐

課程信息 軟件工程實踐 參考教材 《構建之法》 作者:鄒欣, 編輯:周筠 他山之石 北京航空航天大學 羅杰, 劉乾 東北師范大學 楊貴福 北京電子科技學院 婁嘉鵬 教師:汪璟玢 助教:卞…

suse leap_Ruby程序檢查leap年

suse leapProblem statement: Given a year, we have to check whether it is a Leap year or not using Ruby program. 問題陳述 :給定年份,我們必須使用Ruby程序檢查是否為Le年。 Methods used: 使用的方法: gets(): This method is a pu…

html導航欄點擊不能跳轉,無法單擊導航欄中的鏈接CSS HTML

不確定是否允許您鏈接您的網站,但是如果允許。 因此,我可以將所有鏈接懸停在導航欄中,但我無法點擊它們,并且S的圖片是可移動的,但無法點擊,我做錯了什么?無法單擊導航欄中的鏈接CSS HTMLNickeb…

JAVA 取得當前目錄的路徑/Servlet/class/文件路徑/web路徑/url地址

2019獨角獸企業重金招聘Python工程師標準>>> 在寫Java程序時不可避免要獲取文件的路徑...總結一下,遺漏的隨時補上 1.可以在servlet的init方法里 String path getServletContext().getRealPath("/"); 這將獲取web項目的全路徑 例如 :E:\eclipseM9\worksp…

關于細分到字段的權限系統_操作系統中的細分

關于細分到字段的權限系統為什么需要細分? (Why Segmentation is required?) In the Operating System, an important drawback of memory management is the separation of the users view of memory and the actual physical memory. Paging is the scheme which…

計算機科學技術專業解析,專業解讀—計算機科學與技術

原標題:專業解讀—計算機科學與技術專業培養目標:本專業培養具有良好的科學素養,系統地、較好地掌握計算機科學與技術包括計算機硬件、軟件與應用的基本理論、基本知識和基本技能與方法,能在科研部門、教育單位、企業、事業、技術…

阿里云服務器配置開發環境第五章:Centos7.3切換為iptables防火墻

centos7.3默認使用的防火墻應該是firewall,而不是iptables。而我們xxmj服務器使用的是iptables防火墻。所以,在配置防火墻之前,我們需要先關閉firewall,安裝iptables。 1.關閉firewall service firewalld stop systemctl disable …

mba學什么書_MBA的完整形式是什么?

mba學什么書MBA:工商管理碩士 (MBA: Master of Business Administration) MBA is an abbreviation of a Master of Business Administration. It is a masters degree for post-graduation in business administration. This business masters degree program is a …

Qt for Android 開發大坑

Qt for Android 開發大坑 作者: qyvlik Qt 5.5.1 這里說一說比較常見的 Qt 開發安卓的大坑。希望同學們不要做無謂的掙扎,跳過這些坑。輸入框 首當其沖的是輸入框,Qt 的輸入在安卓上表現不佳. 無法支持安卓原生的輸入法訪問 Qt 的輸入框。就是安卓輸入法…

bca ac如何聯合索引_BCA的完整形式是什么?

bca ac如何聯合索引BCA:計算機應用學士學位 (BCA: Bachelor of Computer Applications) BCA is an abbreviation of Bachelor of Computer Applications. It is a three-year undergraduate program in Computer applications. It is considered equivalent to B.Te…

path r'c test.html',robot framework - robot命令參數解析

robot 命令參數解析version > 3.0.1原文檔查看命令:robot --helprobot -h-F --extension value通過文件擴展名控制需要執行的用例。如果只執行一個文件,這個參數無效。需要執行多個擴展名時,用“:”分隔開。Examples:--extension robot-F robot:txt-N…

嘿,程序員,你該學點經濟學了!

前言: 筆者一直認為,一個好的程序員,不僅僅是代碼敲得好,其它方面的知識和能力相同非常重要。特別是隨著年齡的增長。非常多人也慢慢的往管理層發展。這個時候溝通與協調能力變得更加重要,而一些策劃,推廣方…

linux硬件配置_Linux硬件配置

linux硬件配置What sort of hardware configuration is expected to run Linux? This is a decent question; the real hardware configuration for the OS changes intermittently. The Linux Hardware?HOWTO gives a (pretty much) complete posting of hardware supported…

重郵2019計算機考研復試名單,重慶郵電大學2019年碩士研究生招生復試通知

當前2019年考研分數線已經公布,穩穩過線的同學即可全心準備復試了,中公考研小編整理了“重慶郵電大學2019年碩士研究生招生復試通知”文章,希望對大家有所幫助!各復試考生:根據《2019年重慶郵電大學碩士研究生復試工作方案》&…

Linux相關圖解隨記

01.dns解析過程02.用戶訪問網站流程03.局域網電腦上網流程04.網站架構圖解轉載于:https://blog.51cto.com/qinbin/1954149