为SimpleCursorAdapter增加CheckBox支持 推荐
2011-11-26 19:01
369 查看
SimpleCursorAdapter作为Android开发中一个非常常用的Adapter类,在ListView(ListActivity)和Cursor类之间架设了一条非常方便的桥梁。但是需求经常是除了表示之外还会希望能够通过CheckBox进行多重选择。为此对SimpleCursorAdapter类进行了扩展。代码如下
做的工作有两个,一个是重新实现了getView方法以增加对CheckBox的支持。
另一个是增加了getSelectedItems方法以取得选中的项目在Cursor中由idColumn指定的信息。一般来说这个信息会事对应数据的Id。数据按照选择的顺序排列。
用法和SimpleCursorAdapter基本一样,只是增加了CheckBox的资源Id和用来从Cursor选择数据的Column名。
当然了,在用于SimpleCursorAdapter的资源文件中需要加上CheckBox控件。就像下面中31行到35行表示的那样。
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>
相关文章推荐
- 为SimpleCursorAdapter增加CheckBox支持
- 自定义带单选框的SimpleCursorAdapter(ListView中增加CheckBox的OnClick响应)
- 自定义带单选框的SimpleCursorAdapter(ListView中增加CheckBox的OnClick响应)
- 【android】ListView+SimpleCursorAdapter+checkbox实现批量删除
- android]ListView+SimpleCursorAdapter+checkbox实现批量删除
- Android 利用SimpleCursorAdapter.ViewBinder 实现 List中标记Checkbox
- android中SimpleCursorAdapter _id错误的问题
- Android中文API (109) —— SimpleCursorTreeAdapter
- SimpleCursorTreeAdapter介绍
- SimpleAdapter 和 SimpleCursorAdapter的使用
- Android基础学习之BaseAdaptery以及SimpleCursorAdapter的使用
- android 的sqlite 主键与simpleCursorAdapter
- Android 使用SimpleCursorAdapter为listview的item添加按钮
- android适配器SimpleCursorAdapter的使用以及最后一个参数的作用
- simpleCursorAdapter的一个问题
- Android-Adapter实现方法(ArrayAdapter,SimpleAdapter,SimpleCursorAdapter)
- SQLiteDatebase 、SQLiteOpenHelper、SimpleCursorAdapter
- ArrayAdapter ,SimpleAdapter ,SimpleCursorAdapter 区别
- ListActivity+sqlite+SimpleCursorAdapter
- Android实现获取本机中所有图片(Loader,CursorLoader,LoaderManager,SimpleCursorAdapter的简单应用)