ListView下拉列表控件使用方法詳解
本文實(shí)例為大家分享了ListView下拉列表控件的使用方法,供大家參考,具體內(nèi)容如下
ListView列表控件
列表的顯示需要三個(gè)元素:
view 用來顯示數(shù)據(jù)的view
適配器 用來把數(shù)據(jù)映射到view的中介
數(shù)據(jù) 具體的將被映射的字符串,圖片,或者基本組件
案例代碼
布局文件代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="fill_parent" ? ? android:layout_height="fill_parent" ? ? ?> ? ? <ListView? ? ? ? ? android:id="@+id/lv_list" ? ? ?android:layout_width="match_parent" ? ? ?android:layout_height="match_parent" ? ? ? ? /> </LinearLayout>
activity代碼
package rr.yy; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class ListViewDemo extends Activity { ? ?private ListView lv_list; ? ? @Override? ? ? public void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.main); ? ? ? ? lv_list=(ListView)findViewById(R.id.lv_list); ? ? ? ? final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()); ? ? ? ? lv_list.setAdapter(adapter); ? ? ? ? lv_list.setOnItemClickListener(new OnItemClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, ? ? ? ? ? ? ? ? ? ? long arg3) { ? ? ? ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ? ? ? String txt =adapter.getItem(arg2); ? ? ? ? ? ? ? ? Toast.makeText(ListViewDemo.this, txt, 0).show(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ?? ? ? private String[] getData() { ? ? ? ? // TODO Auto-generated method stub ? ? ? ? return new String[]{"lin","kin","park","好好","先生","上學(xué)","但是","得分","讀書","克扣","奮斗"}; ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Intel HAXM為Android 模擬器加速解決模擬器運(yùn)行慢的問題
Android 模擬器一直以運(yùn)行速度慢著稱, 本文介紹使用 Intel HAXM 技術(shù)為 Android 模擬器加速, 使模擬器運(yùn)行度媲美真機(jī), 徹底解決模擬器運(yùn)行慢的問題,感興趣的朋友可以了解下哦2013-01-01Kotlin開發(fā)中open關(guān)鍵字與類名函數(shù)名和變量名的使用方法淺析
這篇文檔中,我們將解釋如何以及為什么將 open 關(guān)鍵字與類名、函數(shù)名和變量名一起使用,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的2023-02-02Android使用系統(tǒng)相機(jī)進(jìn)行拍照的步驟
這篇文章主要介紹了Android使用系統(tǒng)相機(jī)進(jìn)行拍照的步驟,幫助大家更好的進(jìn)行Android開發(fā),感興趣的朋友可以了解下2020-12-12Android基于Xposed修改微信運(yùn)動(dòng)步數(shù)實(shí)例
這篇文章主要介紹了Android基于Xposed修改微信運(yùn)動(dòng)步數(shù)實(shí)例,需要的朋友可以參考下2017-06-06android?scrollview頂部漸漸消失實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了android?scrollview頂部漸漸消失實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11InputFilter實(shí)現(xiàn)EditText文本輸入過濾器實(shí)例代碼解析
EditText是Android的文本輸入框控件。這篇文章給大家介紹 InputFilter實(shí)現(xiàn)EditText文本輸入過濾器實(shí)例代碼解析,需要的朋友一起看看吧2016-11-11