六、使用SimpleAdapter为ListView提供列表项
ArrayAdapter简单,但是其每个列表项只能是TextView及其子类,限制较大。如果需要实现更加复杂的列表项则可使用SimpleAdapter。SimpleAdapter非常强大,ListView的大部分应用场景均可通过SimpleAdapter类来提供列表项。
下面是使用SimpleAdapter向ListView提供列表项的例子:
/SimpleAdapterTest/res/layout/activity_main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/btn_refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="刷新"
/>
<ListView
android:id="@+id/lst_songs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
/SimpleAdapterTest/res/layout/lin_item_song.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/img_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/btn_play_song"
android:layout_toRightOf="@id/img_head"
android:orientation="horizontal" >
<TextView
android:id="@+id/txt_song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/txt_singer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="test" />
</LinearLayout>
<Button
android:id="@+id/btn_play_song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="播放" />
</RelativeLayout>
此布局的效果是:
/SimpleAdapterTest/src/com/example/simpleadaptertest/MainActivity.java文件
package com.example.simpleadaptertest;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.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[][] songs = new String[5][2];
songs[0][0] = "林忆莲";
songs[0][1] = "假如让你吻下去";
songs[1][0] = "张智霖";
songs[1][1] = "你太善良";
songs[2][0] = "李克勤";
songs[2][1] = "深深深";
songs[3][0] = "杜德伟";
songs[3][1] = "忘情号";
songs[4][0] = "黎明";
songs[4][1] = "如果这是情";
int[] imgs = new int[] { R.drawable.linyilian, R.drawable.zhangzhilin,
R.drawable.likeqin, R.drawable.dudewei, R.drawable.liming };
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < songs.length; i++)
{
Map<String, Object> item = new HashMap<String, Object>();
item.put("header", imgs[i]);
item.put("song_name", songs[i][1]);
item.put("singer_name", songs[i][0]);
listItems.add(item);
}
// 第一个参数是上下文,因为涉及到控件创建,所以必须提供上下文对象
// 第二个参数是构建列表项的数据,是List<Map<String, Object>>类型
// 第三个参数是提供呈现列表数据的布局(布局id)
SimpleAdapter adapter = new SimpleAdapter(this, listItems,
R.layout.lin_item_song, new String[] { "header", "song_name",
"singer_name" }, new int[] { R.id.img_head,
R.id.txt_song_name, R.id.txt_singer_name });
ListView list = (ListView) findViewById(R.id.lst_songs);
list.setAdapter(adapter);
}
}
实现的效果是:
注意,在使用SimpleAdapter向ListView提供数据构建起来的视图中,找不到ListView的列表项里面的控件。比如,我们只能为上图的“刷新”按钮写点击事件,但是不能为5个“播放”按钮写点击事件,因为“刷新”按钮可以找得到(findViewById),但是5个“播放”按钮却找不到。
如果需要监听用户单机、选中某个列表项的事件可以通过监听Adapter的setOnItemClickListener()、setOnItemSelectedListener()方法。
例如:
/SimpleAdapterTest/src/com/example/simpleadaptertest/MainActivity.java文件
package com.example.simpleadaptertest;
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.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[][] songs = new String[5][2];
songs[0][0] = "林忆莲";
songs[0][1] = "假如让你吻下去";
songs[1][0] = "张智霖";
songs[1][1] = "你太善良";
songs[2][0] = "李克勤";
songs[2][1] = "深深深";
songs[3][0] = "杜德伟";
songs[3][1] = "忘情号";
songs[4][0] = "黎明";
songs[4][1] = "如果这是情";
int[] imgs = new int[] { R.drawable.linyilian, R.drawable.zhangzhilin,
R.drawable.likeqin, R.drawable.dudewei, R.drawable.liming };
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < songs.length; i++)
{
Map<String, Object> item = new HashMap<String, Object>();
item.put("header", imgs[i]);
item.put("song_name", songs[i][1]);
item.put("singer_name", songs[i][0]);
listItems.add(item);
}
// 第一个参数是上下文,因为涉及到控件创建,所以必须提供上下文对象
// 第二个参数是构建列表项的数据,是List<Map<String, Object>>类型
// 第三个参数是提供呈现列表数据的布局(布局id)
SimpleAdapter adapter = new SimpleAdapter(this, listItems,
R.layout.lin_item_song, new String[] { "header", "song_name",
"singer_name" }, new int[] { R.id.img_head,
R.id.txt_song_name, R.id.txt_singer_name });
ListView list = (ListView) findViewById(R.id.lst_songs);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
Toast t = Toast.makeText(MainActivity.this, songs[arg2][1]
+ " 正在播放", 10000);
t.show();
}
});
list.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
Toast t = Toast.makeText(MainActivity.this, songs[arg2][1]
+ " 正在播放...", 10000);
t.show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast t = Toast.makeText(MainActivity.this, "...", 10000);
t.show();
}
});
}
}
/SimpleAdapterTest/res/layout/lin_item_song.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/img_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/img_head"
android:orientation="horizontal" >
<TextView
android:id="@+id/txt_song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/txt_singer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="test" />
</LinearLayout>
</RelativeLayout>
点击之时的效果:
注意,此时已经去掉了“播放”按钮。实践证明,在加上播放按钮的情况下,列表项的setOnItemClickListener和setOnItemSelectedListener将会失效。解决ListView.setOnItemClickListener失效的方法见下文BaseAdapter的例子。
setOnItemClickListener和setOnItemSelectedListener是在AdapterView中定义的,所以不仅是ListView,所有AdapterView及其子类都可以使用这两个监听器监听列表项的点击和触摸事件。
七:用BaseAdapter为ListView提供列表项
从上面的例子中我们可以看到,我们只能将列表项作为一个整体来监听选中和点击事件,且不能添加按钮(准确地说是不能操作)。这样我们的列表项依然受到较大的制约。
扩展BaseAdapter可以取得对Adapter的最大控制权。
下面是使用BaseAdapter向ListView提供列表项的例子:
/BaseAdapterTest/res/layout/activity_main.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/liv_songs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
/BaseAdapterTest/res/layout/lin_item_song.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/img_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn_play"
android:layout_toRightOf="@+id/img_head"
android:orientation="horizontal" >
<TextView
android:id="@+id/txt_song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/txt_singer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="test" />
</LinearLayout>
<!-- 如果没有focusable、clickable、focusableInTouchMode三个属性设置为false,点击事件会被子View优先捕捉到,导致ListView.setOnItemClickListener失效 -->
<Button
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="播放" />
</RelativeLayout>
/BaseAdapterTest/src/com/example/baseadaptertest/MainActivity.java文件
package com.example.baseadaptertest;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.liv_songs);
final String[][] songs = new String[5][2];
songs[0][0] = "林忆莲";
songs[0][1] = "假如让你吻下去";
songs[1][0] = "张智霖";
songs[1][1] = "你太善良";
songs[2][0] = "李克勤";
songs[2][1] = "深深深";
songs[3][0] = "杜德伟";
songs[3][1] = "忘情号";
songs[4][0] = "黎明";
songs[4][1] = "如果这是情";
int[] imgs = new int[] { R.drawable.linyilian, R.drawable.zhangzhilin,
R.drawable.likeqin, R.drawable.dudewei, R.drawable.liming };
BaseAdapter adapter = new songsBaseAdapter(this, songs, imgs);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
Toast t = Toast.makeText(MainActivity.this, songs[arg2][1]
+ " 正在播放...", 10000);
t.show();
}
});
}
}
class songsBaseAdapter extends BaseAdapter
{
private Context context;
private String[][] songs_info;
private int[] songs_img;
public songsBaseAdapter(Context t, String[][] s_info, int[] s_img)
{
context = t;
songs_info = s_info;
songs_img = s_img;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return 5;
}
@Override
public Object getItem(int arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0)
{
// 传入的arg0是列表项的下标(第几个列表项),可用这个值作为列表项的ID
return arg0;
}
@Override
public View getView(int item_count, View arg1, ViewGroup arg2)
{
// 这个方法将提供列表项的布局及细节,第一个参数是列表项下标(第几个列表项),第二个参数是该列表项的View,第三个参数是整个ListView
final int _item_count = item_count;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.lin_item_song, null);
((TextView) arg1.findViewById(R.id.txt_singer_name))
.setText(songs_info[item_count][0]);
((TextView) arg1.findViewById(R.id.txt_song_name))
.setText(songs_info[item_count][1]);
((ImageView) arg1.findViewById(R.id.img_head))
.setBackgroundResource(songs_img[item_count]);
((Button) arg1.findViewById(R.id.btn_play))
.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Toast t = Toast.makeText(context,
songs_info[_item_count][1] + "正在播放", 10000);
t.show();
}
});
return arg1;
}
}
实际效果: