列表显示(ListView)

列表显示(ListView

ListView与ScrollView类似。但是作用更加广泛,应用也比较多。

代码:

<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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="55px"
        android:text="@string/txt1" 
        android:gravity="center_horizontal"/>
<ListView 
        android:id="@+id/carlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>
 
MainActivity.java 文件的内容。

package com.example.listview2;
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.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	private int[] pic = new int[]{R.drawable.car1,R.drawable.car3,R.drawable.car4};	//定义一个整型数组存储图片。
	private String[][] data = new String[][] {{"价格","100000000"},{"价格","3130000"},{"价格","4500000"}};//定义一个二维数组存储相关信息
	private ListView datalist = null; //定义列表显示组件
	private List<Map<String,String>> list = new ArrayList<Map<String,String>>(); //定义一个list集合存储需要在相关组件上显示的信息
	private SimpleAdapter SA = null; //进行数据转换操作
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.datalist = (ListView)super.findViewById(R.id.carlist);
		for (int i = 0; i < this.data.length; i++) {
			Map<String,String> map = new HashMap<String,String>();//定义Map集合,保存每一条数据。
			map.put("image", String.valueOf(pic[i]));//与car.xml中的ImageView组件相匹配。
map.put("name", data[i][0]);//与car.xml文件中的文本组件相匹配
map.put("fee", data[i][1]);//与car.xml文件中的文本组件相匹配
map.put("xingneng", "性能");//与car.xml文件中的文本组件相匹配
map.put("word", "好");//与car.xml文件中的文本组件相匹配
this.list.add(map);//把所有信息保存到list集合中。}this.SA = new SimpleAdapter(this, list, R.layout.car, new String[] {"image","name","fee","xingneng","word"},new int[] {R.id.image,R.id.name,R.id.fee,R.id.xingneng,R.id.word});//第一个参数就是上下文的意思,一般都是this,第二个参数表示要转换数据的List集合,第三个参数表示要加载的资源文件,第四个参数表示一个String数组主要是将Map对象中的名称与car.xml文件中的名称相对应,第五个参数表示car.xml文件中的控件的id。
datalist.setAdapter(SA);}


 

car.xml文件的内容。

<?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="horizontal"
    android:gravity="left">
    <ImageView 
        android:id="@+id/image"
   	 	android:layout_width="304px"
   		android:layout_height="match_parent"
        />
	<LinearLayout 
     	android:layout_width="190px"
    	android:layout_height="match_parent"
    	android:orientation="vertical"
    	android:gravity="center_horizontal">
   		<TextView 
        	android:id="@+id/name"
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:padding="5px"
        	android:textSize="45px"
        	/>
	    <TextView 
	        android:id="@+id/fee"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:padding="5px"
	        android:textSize="45px"
	        />
		
    </LinearLayout>
    <LinearLayout 
 		    android:layout_width="match_parent"
		    android:layout_height="match_parent"
		    android:orientation="vertical"
		    android:gravity="center_horizontal">
		     <TextView 
		        android:id="@+id/xingneng"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:padding="5px"
		        android:textSize="45px"/>
	       <TextView 
		        android:id="@+id/word"
		        android:layout_width="wrap_content"
		        android:layout_height="wrap_content"
		        android:padding="5px"
		        android:textSize="45px"/>
	    </LinearLayout>
</LinearLayout>

注:SimpleAdapter类的主要功能是将List集合中的数据转换为ListView可以支持的数据,而要实现这种转换,首先需要定义一个数据的显示模板(专门定义一个布局管理器,基本上是表格布局),在这一模板中可以定义ListView每一行需要显示的组件,而在需要转换的List集合里保存的是多条Map集合的数据,这些Map集合里保存着一些具体的要显示的信息。即:模板中的每一个组件的id实际上就相当于规定了Map集合的Key,而模板中每个组件的显示内容则由Map所保存的value决定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙潭一条鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值