zl程序教程

您现在的位置是:首页 >  工具

当前栏目

ListView应用--SimpleAdapter

应用 -- listview
2023-09-11 14:15:38 时间

MainActivity.java

package maureen.holiday_3_mylistview_simpleadapter;

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

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

public class MainActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_main);

		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.activity_main, new String[] { "bookpicture",
						"Bookname", "author", "Isbn", "price", "abt" },
				new int[] { R.id.bookpicture, R.id.Bookname, R.id.author,
						R.id.Isbn, R.id.price, R.id.abt });

		setListAdapter(adapter);
	}

	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("Bookname", "数据结构");
		map.put("author", "作者:严蔚敏");
		map.put("Isbn", "编号:001");
		map.put("price", "价格:30元");
		map.put("abt", "梗概:数据结构是计算机存储、组织数据的方式。");
		map.put("bookpicture", R.drawable.shujujiegou);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("Bookname", "小时代");
		map.put("author", "作者:郭敬明");
		map.put("Isbn", "编号:002");
		map.put("price", "价格:10元");
		map.put("abt", "梗概:以上海为背景,讲述四个从小感情深厚、有着不同价值观和人生观的女生");
		map.put("bookpicture", R.drawable.tinytimes);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("Bookname", "后会无期");
		map.put("author", "作者:韩寒");
		map.put("Isbn", "编号:003");
		map.put("price", "价格:50元");
		map.put("abt", "梗概:《后会无期》是一部由劳雷影业、果麦文化和博纳影业联合出品");
		map.put("bookpicture", R.drawable.houhuiwuqi);
		list.add(map);

		return list;

	}

}


activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/bookpicture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5px" />
"

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/Bookname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="22px" />

            <TextView
                android:id="@+id/author"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15px" />

            <TextView
                android:id="@+id/Isbn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13px" />

            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13px" />

            <TextView
                android:id="@+id/abt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="13px" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>


使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。

运行效果如图: