国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ListView使用SimpleAdapter顯示數據

2019-11-09 17:49:51
字體:
來源:轉載
供稿:網友

1、activity_main中加入控件Listview

<RelativeLayout 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"    tools:context=".MainActivity" >    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>2、創建lieview的item布局listview_item

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/tv"        android:text="數據"        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="40dp" /></LinearLayout>  這里就顯示個文本

3、MainActivity代碼:

	PRivate ListView lv;	private List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		lv=(ListView) findViewById(R.id.lv);		//listview加入數據		init();	}	private void init() {		for (int i = 0; i <50; i++) {			//存入map			Map<String , Object> map=new HashMap<String, Object>();			map.put("name", "數據"+i);						//添加到集合中			list.add(map);		}		//SimpleAdapter適配器用法,第一個參數上下文,第二個:數據,第三個:listview中的布局界面,第四個:剛剛Map里面存的name,第五個:就是把第四個的name顯示到哪一個控件上		SimpleAdapter  sim=new SimpleAdapter(MainActivity.this, list, R.layout.listview_item, new String[]{"name"},new int[]{R.id.tv} );		lv.setAdapter(sim);		//判斷點擊了第幾個listview的item		lv.setOnItemClickListener(new OnItemClickListener() {			@Override			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,					long arg3) {				Toast.makeText(MainActivity.this, "點擊了第"+arg2, Toast.LENGTH_SHORT).show();				}		});	}


上一篇:博客筆記總結2

下一篇:APT技術

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临湘市| 陕西省| 托克逊县| 安庆市| 平昌县| 宁明县| 沿河| 商洛市| 荥经县| 长子县| 于都县| 松桃| 麻栗坡县| 汶上县| 湖口县| 乐陵市| 盐城市| 永新县| 班戈县| 中山市| 玉门市| 建德市| 道孚县| 邵阳县| 泰安市| 海林市| 武冈市| 武汉市| 文山县| 麦盖提县| 乌海市| 聂拉木县| 宁海县| 城市| 峨眉山市| 安图县| 浦东新区| 旅游| 黔东| 临城县| 松阳县|