Android控件之ListView

一、单行纯文本的列表


1.配置数据源

<span style="font-size:18px;">	private ArrayAdapter<String> adapter;</span>
<span style="font-size:18px;"><span style="white-space: pre;">	</span>adapter = new ArrayAdapter<String>(this,
<span style="white-space: pre;">			</span>android.R.layout.simple_list_item_1);
<span style="white-space: pre;">	</span>adapter.add("AAA");
<span style="white-space: pre;">	</span>adapter.add("BBB");
<span style="white-space: pre;">	</span>adapter.add("CCC");</span>

2.添加数据

<span style="font-size:18px;">	this.listView = (ListView) findViewById(R.id.listView1);
	this.listView.setAdapter(adapter);
<span style="white-space: pre;">	</span>this.listView.setOnItemClickListener(this);</span>

3.设置监听事件

<span style="font-size:18px;">	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		String str = adapter.getItem(position);
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}</span>

二、自定义列表

1.配置数据源

<span style="font-size:18px;">package com.zyl.listview;

public class ListViewData {
	private String name;
	private String sex;
	private int imageID;

	public ListViewData(String name, String sex, int imageID) {
		super();
		this.name = name;
		this.sex = sex;
		this.imageID = imageID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getImageID() {
		return imageID;
	}

	public void setImageID(int imageID) {
		this.imageID = imageID;
	}

}</span>

2.配置布局文件

<span style="font-size:18px;"><?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/imageView_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <LinearLayout 
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_cell_big"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/tv_cell_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        
    </LinearLayout>

</LinearLayout></span>

3.添加适配器

<span style="font-size:18px;">package com.zyl.listview;

import com.example.listviewtest.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CustomActivity extends Activity {
	private ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_custom);
		
		this.listView=(ListView) findViewById(R.id.listView_custom);
		this.listView.setAdapter(adapter);
	}
	private BaseAdapter adapter=new BaseAdapter() {
		private ListViewData[] data=new ListViewData[]{
				new ListViewData("小明", "男", R.drawable.ic_launcher),
				new ListViewData("小李", "女", R.drawable.ic_launcher),
				new ListViewData("小王", "男", R.drawable.ic_launcher)
		};
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LinearLayout linearLayout=null;
			if(convertView!=null){
				linearLayout= (LinearLayout) convertView;
			}else{
				linearLayout=(LinearLayout) LayoutInflater.from(CustomActivity.this).inflate(R.layout.list_cell, null);		
			}
			ImageView iv=(ImageView) linearLayout.findViewById(R.id.imageView_icon);
			TextView tv_name=(TextView) linearLayout.findViewById(R.id.tv_cell_big);
			TextView tv_sex=(TextView) linearLayout.findViewById(R.id.tv_cell_small);
			iv.setImageResource(data[position].getImageID());
			tv_name.setText(data[position].getName());
			tv_sex.setText(data[position].getSex());
			return linearLayout;
		}
		
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		@Override
		public ListViewData getItem(int position) {
			// TODO Auto-generated method stub
			return data[position];
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.length;
		}
	};
}</span><span style="font-size:32px;">
</span>

三、关于ListActivity

<span style="font-size:18px;">package com.zyl.listview;

import android.app.ListActivity;
import android.os.Bundle;

public class ListViewActivity extends ListActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//设置适配器
		setListAdapter(ListAdapter adapter);
	}	
}</span><span style="font-size:32px;">
</span>
使用ListActivity可以省略一个只包含ListView控件的布局文件,在onCreate()方法中直接设置适配器即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值