接着上一篇实现一个带有复选框的列表视图,这要求对Adapter有比较清楚的理解。
1. ArrayAdapter从Layout读取TextView控件,返回给ListView显示,这个处理在ArrayAdapter的getView方法里,我们可以继承这个类,覆盖getView,改成读取CheckBox控件,看下面的代码:
CheckListView.java
class CheckArrayAdapter<T> extends ArrayAdapter<T>
{
public CheckArrayAdapter(Context context, int checkBoxResId,
T[] objects)
{
super(context, checkBoxResId, objects);
}
public CheckArrayAdapter(Context context, int checBoxResId)
{
super(context, checBoxResId);
}
public View getView(int position, View convertView, ViewGroup parent, int resource)
{
CheckBox checkBox;
if (convertView == null)
checkBox = (CheckBox)LayoutInflater.from(getContext()).inflate(resource, parent, false);
else
checkBox = (CheckBox)convertView;
T item = getItem(position);
if (item instanceof CharSequence)
checkBox.setText((CharSequence) item);
else
checkBox.setText(item.toString());
return checkBox;
}
}
{
public CheckArrayAdapter(Context context, int checkBoxResId,
T[] objects)
{
super(context, checkBoxResId, objects);
}
public CheckArrayAdapter(Context context, int checBoxResId)
{
super(context, checBoxResId);
}
public View getView(int position, View convertView, ViewGroup parent, int resource)
{
CheckBox checkBox;
if (convertView == null)
checkBox = (CheckBox)LayoutInflater.from(getContext()).inflate(resource, parent, false);
else
checkBox = (CheckBox)convertView;
T item = getItem(position);
if (item instanceof CharSequence)
checkBox.setText((CharSequence) item);
else
checkBox.setText(item.toString());
return checkBox;
}
}
2. 前一篇的CheckListView类改为从ListActivity继承,ListActivity默认以ListView为根视图,因此不需要Layout文件以及调用setContentView。
CheckListView.java
public class CheckListView extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
CheckArrayAdapter<String> adapter = new CheckArrayAdapter<String>(
this, R.layout.checkbox_item, mStrList);
getListView().setAdapter(adapter);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
ListView listView = (ListView)parent;
CheckBox checkBox = (CheckBox)listView.getChildAt(pos);
checkBox.setChecked(!checkBox.isChecked());
}
});
getListView().setSelection(2);
}
private String [] mStrList =
{
"Hello1",
"Hello2",
"Hello3"
};
}
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
CheckArrayAdapter<String> adapter = new CheckArrayAdapter<String>(
this, R.layout.checkbox_item, mStrList);
getListView().setAdapter(adapter);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id)
{
ListView listView = (ListView)parent;
CheckBox checkBox = (CheckBox)listView.getChildAt(pos);
checkBox.setChecked(!checkBox.isChecked());
}
});
getListView().setSelection(2);
}
private String [] mStrList =
{
"Hello1",
"Hello2",
"Hello3"
};
}
在onCreate里,我们创建一个CheckArrayAdapter,其构造函数要求传入一个以CheckBox为根视图的Layout文件ID,怎么样创建Layout已经非常熟悉,不再描述。不过创建了Layout后需要对CheckBox设一个属性,将Focusable设为False,只有这样,ListView才能得到按键事件,才能触发OnItemClickListener。
调用getListView()将返回ListActivity里的ListView控件。
在ItemClick事件,切换CheckBox的选中状态。
3. 运行程序看效果: