为SimpleCursorAdapter增加CheckBox支持
2014-04-21 16:08
483 查看
SimpleCursorAdapter作为Android开发中一个非常常用的Adapter类,在ListView(ListActivity)和Cursor类之间架设了一条非常方便的桥梁。但是需求经常是除了表示之外还会希望能够通过CheckBox进行多重选择。为此对SimpleCursorAdapter类进行了扩展。代码如下
package LyricPlayer.xwg;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;
public class CheckBoxCursorAdapter extends SimpleCursorAdapter {
private ArrayList<Integer> selection = new ArrayList<Integer>();
private int mCheckBoxId = 0;
private String mIdColumn;
public CheckBoxCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int checkBoxId, String idColumn) {
super(context, layout, c, from, to);
mCheckBoxId = checkBoxId;
mIdColumn = idColumn;
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public Object getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
CheckBox checkbox = (CheckBox)view.findViewById(mCheckBoxId);
checkbox.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Cursor cursor = getCursor();
cursor.moveToPosition(position);
int rowId = cursor.getInt(cursor.getColumnIndexOrThrow(mIdColumn));
int index = selection.indexOf(rowId);
if (index != -1) {
selection.remove(index);
} else {
selection.add(rowId);
}
}
});
Cursor cursor = getCursor();
cursor.moveToPosition(position);
int rowId = cursor.getInt(cursor.getColumnIndexOrThrow(mIdColumn));
if (selection.indexOf(rowId)!= -1) {
checkbox.setChecked(true);
} else {
checkbox.setChecked(false);
}
return view;
}
ArrayList<Integer> getSelectedItems(){
return selection;
}
}
做的工作有两个,一个是重新实现了getView方法以增加对CheckBox的支持。
另一个是增加了getSelectedItems方法以取得选中的项目在Cursor中由idColumn指定的信息。一般来说这个信息会事对应数据的Id。数据按照选择的顺序排列。
用法和SimpleCursorAdapter基本一样,只是增加了CheckBox的资源Id和用来从Cursor选择数据的Column名。
当然了,在用于SimpleCursorAdapter的资源文件中需要加上CheckBox控件。就像下面中31行到35行表示的那样。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/music_row"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent">
<TextView
android:id="@+id/music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dip"
android:text="TextView" android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/lyric_comma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="16dip"
android:text="," android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/music_duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="16dip"
android:text="" android:layout_gravity="center_vertical" android:layout_weight="1"/>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
</LinearLayout>
本文出自 “西尖山笔记” 博客,请务必保留此出处http://craftsman1970.blog.51cto.com/3522772/725606
package LyricPlayer.xwg;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;
public class CheckBoxCursorAdapter extends SimpleCursorAdapter {
private ArrayList<Integer> selection = new ArrayList<Integer>();
private int mCheckBoxId = 0;
private String mIdColumn;
public CheckBoxCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int checkBoxId, String idColumn) {
super(context, layout, c, from, to);
mCheckBoxId = checkBoxId;
mIdColumn = idColumn;
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public Object getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
CheckBox checkbox = (CheckBox)view.findViewById(mCheckBoxId);
checkbox.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Cursor cursor = getCursor();
cursor.moveToPosition(position);
int rowId = cursor.getInt(cursor.getColumnIndexOrThrow(mIdColumn));
int index = selection.indexOf(rowId);
if (index != -1) {
selection.remove(index);
} else {
selection.add(rowId);
}
}
});
Cursor cursor = getCursor();
cursor.moveToPosition(position);
int rowId = cursor.getInt(cursor.getColumnIndexOrThrow(mIdColumn));
if (selection.indexOf(rowId)!= -1) {
checkbox.setChecked(true);
} else {
checkbox.setChecked(false);
}
return view;
}
ArrayList<Integer> getSelectedItems(){
return selection;
}
}
做的工作有两个,一个是重新实现了getView方法以增加对CheckBox的支持。
另一个是增加了getSelectedItems方法以取得选中的项目在Cursor中由idColumn指定的信息。一般来说这个信息会事对应数据的Id。数据按照选择的顺序排列。
用法和SimpleCursorAdapter基本一样,只是增加了CheckBox的资源Id和用来从Cursor选择数据的Column名。
当然了,在用于SimpleCursorAdapter的资源文件中需要加上CheckBox控件。就像下面中31行到35行表示的那样。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/music_row"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent">
<TextView
android:id="@+id/music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dip"
android:text="TextView" android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/lyric_comma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="16dip"
android:text="," android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/music_duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="16dip"
android:text="" android:layout_gravity="center_vertical" android:layout_weight="1"/>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
</LinearLayout>
本文出自 “西尖山笔记” 博客,请务必保留此出处http://craftsman1970.blog.51cto.com/3522772/725606
相关文章推荐
- 为SimpleCursorAdapter增加CheckBox支持 推荐
- 自定义带单选框的SimpleCursorAdapter(ListView中增加CheckBox的OnClick响应)
- 自定义带单选框的SimpleCursorAdapter(ListView中增加CheckBox的OnClick响应)
- 【android】ListView+SimpleCursorAdapter+checkbox实现批量删除
- android]ListView+SimpleCursorAdapter+checkbox实现批量删除
- Android 利用SimpleCursorAdapter.ViewBinder 实现 List中标记Checkbox
- SimpleCursorAdapter
- android中使用CursorAdapter和SimpleCursorAdapter的方法
- Android入门:ListView(SimpleCursorAdapter实现)
- sqLite 执行查询语句时报错__及SimpleCursorAdapter
- SimpleCursorAdapter简单实例(附源码)
- Android Tips:CursorAdapter 绑定 ListView后,List滚动CheckBox选中消失的问题
- Android适配器之-----SimpleCursorTreeAdapter
- spinner(SimpleCursorAdapter)数据库查询、获取所选中的值
- Android ListView 最基本的用法,使用SimpleCursorAdapter 。 附例子。
- Android7.0中文文档(API)-- SimpleCursorAdapter
- android 的sqlite 主键与simpleCursorAdapter
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- SimpleCursorAdapter的使用
- Android入门:ListView(SimpleCursorAdapter实现)