



下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(fā)技術】android之ListView和SimpleAdapter的組合使用
SimpleAdapter是簡單適配器??梢詫蕚浜玫臄祿@示在ListView中。更多信息可以看看Android官方API開發(fā)文檔。
示例說明:在SimpleAdapterListViewActivity.java中的內容:package
com.zzh.day_listview;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import
android.widget.ListView;
import
android.widget.SimpleAdapter;
import
android.widget.Toast;
/**SimpleAdapter在ListView中的使用
*
@author
Administrator
*
*/
public
class
SimpleAdapterListViewActivity
extends
Activity
{
SimpleAdapter
adapter;
ListView
lv;
List<Map<String,
Object>>
data
=
new
ArrayList<Map<String,
Object>>();//要顯示的數據
Map<String,
Object>
map;//
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_listview);
lv
=
(ListView)
findViewById(R.id.listView1);
//
初始化數據
for
(int
i
=
1;
i
<
21;
i++)
{
map
=
new
HashMap<String,
Object>();
map.put("key1",
i
+
"
<--key1");
map.put("key2",
i+"
<--key2");
data.add(map);
}
/*
*
SimpleAdapter構造方法中參數的意義:
*
參數一:要顯示在的activity
*
參數二:要顯示的數據。此數據必須是List<?
extends
Map<String,
?>>
類型的,而且Map里的鍵必須是String類型的。
*
參數三:自定義的布局文件。定義了兩個TextView,因為我只在初始化數據的時候放了兩個map對象。
*
參數四:是一個String類型的數組,里面放的是,參數二中Map
鍵的數組,而且必須是,這也說明了Map里的鍵必須是String類型的原因
*
參數五:int類型的數組,存放的是,參數二中Map中存的值所要放的位置(View)的id。
*
在這里可以將一個map集合看成是一個ListView中的一個item,即一行數據,而key則代表著每個View,value則是要顯示的數據。
*
*/
adapter
=
new
SimpleAdapter(this,
data,
R.layout.simple_listview_item,
new
String[]
{"key1","key2"},
new
int[]
{
R.id.textView1,
R.id.textView2
});
lv.setAdapter(adapter);//將數據顯示在ListView中
lv.setOnItemClickListener(new
OnItemClickListener()//給ListView注冊事件
{
@Override
public
void
onItemClick(AdapterView<?>
parent,
View
view,
int
position,
long
id)
{
Toast.makeText(getApplicationContext(),
"點擊了第"+position+"項",
Toast.LENGTH_LONG).show();
}
});
}
}simple_listview.xml中的內容:<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
</ListView>
</RelativeLayout>simple_listview_item.xml中的內容:<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="26sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度三方勞務派遣公司航空航天領域人才派遣合同
- 2025年度魚塘承包合同轉讓及聯合養(yǎng)殖項目協(xié)議
- 二零二五年度混凝土路面施工環(huán)保責任書
- 二零二五年度勞動合同變更協(xié)議范本:企業(yè)員工勞動保護與職業(yè)健康協(xié)議
- 二零二五年度房屋贈與與智慧城市建設合同范本
- 2025年度退休技術人員返聘企業(yè)項目合作協(xié)議
- 2025年度高性能鋼筋研發(fā)與承包合同
- 二零二五年度醫(yī)院耳鼻喉科醫(yī)師定期坐診合同
- 二零二五年度食品添加劑代理進口合同范本(含質量檢測)
- 二零二五年度土地托管與農業(yè)產業(yè)扶貧合作協(xié)議
- 感知覺與溝通評估三明醫(yī)學科技職業(yè)
- 人教版(2024)六年級全一冊 第17課 設計我的種植園
- 承包商入廠安全培訓試題附參考答案【完整版】
- 加盟京東商城合同模板
- 食品安全與質量檢測技能大賽考試題庫400題(含答案)
- 四川省公務員考試行測真題
- (212題)2024綜合基礎知識考試題庫及解析
- 探索多元化的員工安全意識培訓方式
- 論電視劇《知否知否應是綠肥紅瘦》的現代家庭教育觀及啟示
- 病歷終末質控(質控或醫(yī)務科病歷質控)
- 2024屆高考安徽省江南十校聯考物理試卷(含答案)
評論
0/150
提交評論