ListView的簡單用法

一:運行效果圖


二:學習目標

1):掌握ListView控件的使用

2):掌握如何自定義Adapter的使用

三:主要參考步驟及代碼

       採用ListView的SimapleAdapter,進行數據展示,展示水果的圖片和英文單詞,首先需要定義一個樣式文件fruit.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="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/apple_pic" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:layout_marginTop="20dp"
        android:text="Apple" />

</LinearLayout>
使用simapleAdapter的數據一般都是用HashMap構成的列表,列表的每一節對應ListView的每一行。通過SimpleAdapter的構造函數,將HashMap的每個鍵的數據映射到佈局文件中對應控件上,這個佈局文件一般根據自己的需求來自己定義。SimpleAdapter的使用步驟:

1)根據需要定義ListView每行所實現的佈局

2)定義一個HashMap構成的列表,將數據以鍵值對的方式存放在裏面。

3)構造SimpleAdapter的對象

4)將ListView綁定到SimpleAdapter上。

主要代碼:

package com.bzu.bztc.listviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	private ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.listview);
		SimpleAdapter mSimpleAdapter = new SimpleAdapter(this, this.getData(),
				R.layout.fruit_item,
			new String[]{"tvName","imageName"},
			new int[]{R.id.tvName,R.id.imageName});
		listView.setAdapter(mSimpleAdapter);
	}

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("tvName", "Apple");
		map.put("imageName",R.drawable.apple_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "Banana");
		map.put("imageName",R.drawable.banana_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "Orange");
		map.put("imageName",R.drawable.orange_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "WaterMelon");
		map.put("imageName",R.drawable.watermelon_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "Pear");
		map.put("imageName",R.drawable.pear_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "Grape");
		map.put("imageName",R.drawable.grape_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "Pineapple");
		map.put("imageName",R.drawable.pineapple_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "Strawberry");
		map.put("imageName",R.drawable.strawberry_pic);
		list.add(map);
		
		map=new HashMap<String,Object>();
		map.put("tvName", "Cherry");
		map.put("imageName",R.drawable.cherry_pic);
		list.add(map);
		
		return list;
		} 
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章