Android ListView(干货)

Java代码

public class MainActivity extends ListActivity {//这里继承的是ListActivity

	char name = 'a';
	int age = 97;
	ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//做了20组显示数据
		for (int i = 0; i<20; i++){
			HashMap<String,String> map = new HashMap<String, String>();
			map.put("name", ""+name);
			map.put("age", ""+age);
			list.add(map);
			name = (char) (age);
			age = age +1;
		}
		//适配器参类型分别为:
		//Context context, 
		//List<? extends Map<String, ?>> data,   --规定了参数类型
		//int resource,    -----单元布局文件
		//String[] from,   -----通过这俩别称
		//int[] to         -----分别匹配到这俩控件
		SimpleAdapter listASimpleAdapter = 
				new SimpleAdapter(this, 
								  list, 
						          R.layout.unit, 
						          new String[]{"name","age"}, 
						          new int[]{R.id.name,R.id.age});
		
		setListAdapter(listASimpleAdapter);//进行适配
		
	}

	@Override
	//ListView触屏事件
	//四个参数分别为:ListView本身,控件,位置,控件id
	//SimpleAdapter 直接把data的id就作为position,CurserAdapter 根据View的positon来获取到这个view上数据id
	//上面这句话引自这句话是引自--http://blog.csdn.net/azuremaple/article/details/7748353
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}
	

}


主界面布局文件

<LinearLayout 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"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false"
            android:scrollbars="vertical" />
    </LinearLayout>

</LinearLayout>


每单元布局文件

<LinearLayout 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"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name"
        android:layout_width="100dp"
        android:layout_height="30dp" 
        android:gravity="center"/>

    <TextView
        android:id="@+id/age"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"/>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值