这里是杂谈啦!
首先说说不同的Adapter,这里之说了2种。
1. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
android.R.layout.simple_list_item_1指每个列表项是一个TestView
public class ListActivityTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// //设置使用自己的界面布局
setContentView(R.layout.main);
ListView lv=(ListView)findViewById(R.id.lv) ;
String[] arr = { "孙悟空", "猪八戒", "唐僧" };
// 定义adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arr);
// 设置该窗口显示列表
lv.setAdapter(adapter);
// 点击事件
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
}
}
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/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F5F5F5"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
2.SimpleAdapter listAdapter = new SimpleAdapter(this,list, R.layout.user, new String[] {"user_name","user_ip"}, new int[]R.id.user_name,R.id.user_ip});
该适配器有5个参数:1.context
2.List<?extends Map<String,?>>类型的集合对象,该集合中每个Map<String,?>对象生成一个列表项。
3.指定布局(自定义布局靠它啦)
4.该参数是一个String[]类型的参数,该参数决定提取Map<String,?>对象中那些key对应的value来生成列表项
5.该参数是一个int[]类型参数,该参数决定使用哪些View组件来组合成一个列表项
public class SimpleAdapterTest extends Activity
{
private String[] names = new String[]
{ "虎头", "弄玉", "李清照", "李白"};
private int[] imageIds = new int[]
{ R.drawable.tiger , R.drawable.nongyu
, R.drawable.qingzhao , R.drawable.libai};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个List集合,List集合的元素是Map
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++)
{
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItems.add(listItem);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this
, listItems
, R.layout.main
, new String[]{ "personName", "header" }
, new int[]{R.id.name , R.id.header});
ListView list = (ListView)findViewById(R.id.mylist);
//为ListView设置Adapter
list.setAdapter(simpleAdapter);
}
}
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"
>
<!-- 定义一个List -->
<ListView android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<!-- 定义一个ImageView,用于作为列表项的一部分。 -->
<ImageView android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
/>
<!-- 定义一个TextView,用于作为列表项的一部分。 -->
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
/>
</LinearLayout>
这里插一个我曾遇到的错误:
E/AndroidRuntime(5169):java.lang.RuntimeException: Unable to start activity ComponentInfo{org.crazyit.listactivity/org.crazyit.listactivity.ListActivityTest}:java.lang.RuntimeException: Your content must have a ListView whose idattribute is 'android.R.id.list'
这里是解决方案:点击打开链接
现在,我们来说说如何改变listview字体的颜色。
这里我先说明下:下面的例子是我看一个前辈的博客看到的,但是当时按他的代码,并没有实现如下效果,并且遇到了一个错误(错误及解决方案在我的另一篇博客里面点击打开链接)。于是我自己做了修改,实现了效果。)
实现的效果是这样的:选中item,其字体设置为#3197FF,未选中的,其字体为#FFFFFF。