ArrayAdapter创建ListView学习笔记

activity_main.xml总的布局文件、array_item.xml为list1的布局、checked_item.xml为list2的布局。

java代码:

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ListView list1 = (ListView) findViewById(R.id.list1);
		// 定义一个数组
		String[] arr1 = { "孙悟空", "猪八戒", "牛魔王" };
		// 将数组包装ArrayAdapter
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>
			(this, R.layout.array_item, arr1);
		// 为ListView设置Adapter
		list1.setAdapter(adapter1);
		ListView list2 = (ListView) findViewById(R.id.list2);
		// 定义一个数组
		String[] arr2 = { "Java", "Hibernate", "Spring" , "Android" };
		// 将数组包装ArrayAdapter
		ArrayAdapter<String> adapter2 = new ArrayAdapter<String>
			(this, R.layout.checked_item, arr2);
		// 为ListView设置Adapter
		list2.setAdapter(adapter2);	
	}

activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
<!-- 设置使用红色的分隔条 -->
<ListView  
    android:id="@+id/list1"
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:divider="#f00"
	android:dividerHeight="2px"
	android:headerDividersEnabled="false"
	/>
<!-- 设置使用绿色的分隔条 -->
<ListView  
    android:id="@+id/list2"
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:divider="#0f0"
	android:dividerHeight="2px"
	android:headerDividersEnabled="false"
	/>
</LinearLayout>

array_item.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<TextView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/TextView"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"	
	android:textSize="24dp"
	android:padding="10px"
	android:shadowColor="#f0f"
	android:shadowDx="4"
	android:shadowDy="4"
	android:shadowRadius="2"/>

checked_item.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/TextView"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"	
	android:textSize="24dp"
	android:checkMark="@drawable/ok"
	android:shadowColor="#f0f"
	android:shadowDx="4"
	android:shadowDy="4"
	android:shadowRadius="2"/>

显示效果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值