問題背景:
???? 我在其他Activity里有一個數據庫,里面有若干條目,數據庫里存的是最簡單的“名字”string類型的信息。我在另外一個Activity里,通過按鍵Button,顯示出一個帶checkbox的列表,顯示出數據庫里的姓名,然后可以選中多個。類似于文件夾刪除的功能。
下面是實現:
**第一部分,在布局文件夾下新建一個my_checkbox.xml.**
??? 這個布局是用來控制將來listview里,每一行怎么顯示。在這里我是左邊顯示名字,右邊顯示復選框CheckBox。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
?? android:layout_width="fill_parent"
?? android:layout_height="wrap_content"??
?? android:orientation="horizontal" >
?? <TextView
?????? android:id="@+id/item_text"
?????? android:textSize="25dip"
?????? android:layout_weight="1"
?????? android:layout_width="wrap_content"
?????? android:layout_height="wrap_content"/>
?? <CheckBox
?????? android:id="@+id/item_check"
?????? android:textSize="25dip"
?????? android:layout_width="wrap_content"
?????? android:layout_height="wrap_content"
?????? android:clickable="false"
?????? android:focusable="false"
?????? android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>?
</LinearLayout>
**注意:**
1,上面的TextView里的layout_weight=1用來實現最左邊顯示名字,最右邊顯示CheckBox。
2,由于CheckBox的響應優先級高于ListView,這里需要把CheckBox的clickable和focuseable屬性都關閉。將來只通過listview的item是否點擊來判斷。
3,CheckBox的checkMark用來設置當選中之后,是個什么效果。
**第二部分:新建一個布局list_check.xml,用來顯示數據庫的條目。當在主Activity里點擊后就會切換到這個界面。**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
?? android:layout_width="fill_parent"
?? android:layout_height="wrap_content"
?? android:orientation="vertical" >
?? <Button
?????? android:id="@+id/confirmBtn"
?????? android:layout_width="fill_parent"
?????? android:layout_height="wrap_content"
?????? android:text="確定" />
?? <ListView
?????? android:id="@+id/checkList"
?????? android:layout_width="fill_parent"
?????? android:layout_height="wrap_content" >
?? </ListView>
</LinearLayout>
**注:**上面的button是選中若干條目后的確定按鍵,同時也是返回按鍵,返回到主界面。
**第三部分**:在主Activity里設置利用setContentView的方法切換頁面。
這里主Activity的布局文件就不提供了。
下面是主Activity的代碼:
~~~
//為了實現新的布局
Button mChoseBtn = null;
Button mConfirmBtn = null;
boolean firstFlag = true;
ListView list2 = null;
View checkListView = null;
View mainView = null;
/**Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = this.getLayoutInflater();
checkListView = inflater.inflate(R.layout.list_check, null);
mainView = inflater.inflate(R.layout.main, null);
setContentView(mainView);
//切換布局監聽
mChoseBtn = (Button)mainView.findViewById(R.id.choseBtn);
mChoseBtn.setOnClickListener(new ButtonListener());
setUpViews();
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.choseBtn:
Jump2CheckList();
break;
case R.id.confirmBtn:
String s = getCheckInfo();
showToast("您選中的姓名有:"+ s);
Jump2Main();
break;
default:
break;
}
}
}
~~~
~~~
/*切換到主布局*/
public void Jump2Main(){
setContentView(mainView);
setUpViews();
}
~~~
~~~
/*切換到選中布局*/
public void Jump2CheckList(){
setContentView(checkListView);
if(firstFlag){
mConfirmBtn = (Button)checkListView.findViewById(R.id.confirmBtn);
mConfirmBtn.setOnClickListener(new ButtonListener());
firstFlag = false;
}
initCheckList();
}
~~~
**第四部分:給ListView寫適配器**,其實很簡單 也就是上面的initCheckList函數,包含初始化ListView和適配器兩個部分。先看源碼:
~~~
public void initCheckList(){
list2 = (ListView)(checkListView).findViewById(R.id.checkList);
list2.setItemsCanFocus(false);
list2.setAdapter(new CheckListAdapter(this, cursor));
list2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int positon,
long id) {
// TODO Auto-generated method stub
ViewHolder vHolder = (ViewHolder) view.getTag();
vHolder.check.toggle();
isSelected.put(positon, vHolder.check.isChecked());
}
});
}
~~~
下面是適配器:
~~~
/*給CheckList設置適配器*/
public static Map<Integer, Boolean> isSelected;
public class CheckListAdapter extends BaseAdapter{
private Context mContext;
private Cursor mCursor;
//構造函數
public CheckListAdapter(Context context, Cursor cursor){
mContext = context;
mCursor = cursor;
isSelected = new HashMap<Integer, Boolean>();
for(int i=0; i<mCursor.getCount(); i++){
isSelected.put(i, false);
}
}
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.my_checkbox, null);
holder.text = (TextView) convertView.findViewById(R.id.item_text);
holder.check = (CheckBox)convertView.findViewById(R.id.item_check);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
mCursor.moveToPosition(position);
holder.text.setText(Integer.toString(mCursor.getInt(0)));
holder.text.append(mCursor.getString(1));
holder.check.setChecked(isSelected.get(position));
return convertView;
}
public final class ViewHolder{
public TextView text;
public CheckBox check;
}
}
~~~
注:
1,initCheckList里要設置相應的參數,如多選等。
2,public static? Map<Integer, Boolean> isSelected; 這是一個**全局變量**,且是靜態的,用來存儲checkbox的選中狀態。[http://mobile.51cto.com/android-254823.htm](http://mobile.51cto.com/android-254823.htm)?這里將其設成適配器里的一個靜態變量,但奇怪的是到我這就不中了,暫且弄成全局的吧。
3,在適配器的構造函數里初始化上面這個變量,并且所有都設成未選中。
???????????? isSelected = new HashMap<Integer, Boolean>();
??????????????????? for(int i=0; i<mCursor.getCount(); i++){
?????????????????????????? isSelected.put(i, false);
4,由于我跟數據庫做了關聯,所以在適配器的構造函數里傳進去一個cursor,關于cursor的理解可以參考[http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html](http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html)? 說白了他就是一組信息,是個集合。通過cursor.getCount獲得他有多少行的信息。cursor.MoveToposition(i)定位到第i行。獲得數據的方法跟數據時建的表的結構有關。
5,
???????????? public final class ViewHolder{
??????????????????? public TextView text;
??????????????????? public CheckBox check;
???????????? }
至于這個viewholder,實際上不用也可以。他就是把每一行的元素集成了一下,跟布局相對應。但到后來莫名其妙的要用到View.setTag()和View.getTag()來傳遞數據,所以我又把他加上了。
6,適配器的兩大關鍵。
**第一個是**:
????? public int getCount() {
??????????????????? // TODO Auto-generated method stub
??????????????????? return cursor.getCount();
???????????? }
這里是返回list有多少行。調用**ListView.getCount()**實際上就是在調用這個函數。
**第二個就是**:public View getView(int position, View convertView, ViewGroup arg2)這個函數。注意第二個參數convertView就是給每一行設置的布局。通過
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
?????????????????????????? convertView = inflater.inflate(R.layout.my_checkbox, null);
將第一個布局文件,和view關聯起來。至于convertView.setTag(holder);其實是給view貼標簽,也就是傳數據的一種方式。當這個布局不為空時通過holder = (ViewHolder)convertView.getTag();直接獲得。mCursor包含所有行的信息,
mCursor.moveToPosition(position);這句話將其定位到position的位置,
????? holder.text.setText(Integer.toString(mCursor.getInt(0)));
??????????????????? holder.text.append(mCursor.getString(1));
這兩句話是獲得每行的信息。我這個表的結構是第一列(對應索引為0)是一個int,第二列(索引為1)是一個string。
holder.check.setChecked(isSelected.get(position));這句話是設置checkbox的狀態。 也就是說通過isSelected來設定,他的初始態是全不選。所以每次跳轉時,默認的是都不選。
最后程序return convertView;返回這個view,然后當下次調用時,他又當做參數傳進來。
7,解釋下listview設置監聽:
list2.setOnItemClickListener(new OnItemClickListener() {
??????????????????? public void onItemClick(AdapterView<?> arg0, View view, int positon,
????????????????????????????????? long id) {
?????????????????????????? // TODO Auto-generated method stub
?????????????????????????? ViewHolder vHolder = (ViewHolder) view.getTag();
?????????????????????????? vHolder.check.toggle();
?????????????????????????? isSelected.put(positon, vHolder.check.isChecked());
??????????????????????????
??????????????????????????
??????????????????? }
???????????? });
當每個item被點擊時,通過被點擊的view.getTag獲得具體的控件view, CheckBox的狀態反轉。
isSelected.put(positon, vHolder.check.isChecked());這句話是通過訪問position位置的CheckBox的狀態來更新保存信息的isSelected。
8,**最后就是我怎么獲得選中的信息?**
可以這么寫:
~~~
OnClickListener bPop = new OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<list.getCount();i++){
if(MyAdapter.isSelected.get(i)){
ViewHolder vHollder = (ViewHolder) list.getChildAt(i).getTag();
Log.i(TAG, "--onClick --"+vHollder.title.getText());
}
}
}
};
~~~
通過list.getCount進行遍歷,通過list.getChildAt(i).getTag得到被選中的ViewHolder,然后得到信息。 因為這里我跟數據庫掛了鉤,所以我直接讀Cursor里面的信息就可以了。
我的寫法是:
~~~
public String getCheckInfo()
{
String info = "";
for(int i=0; i<list2.getCount(); i++){
if(isSelected.get(i)){
//ViewHolder holder = (ViewHolder)list2.getChildAt(i).getTag();
cursor.moveToPosition(i);
info+=cursor.getInt(0)+".";
}
}
return info;
}
~~~
上面的list2.getCount和cursor.getCount是一樣的效果。
最后說下這個Cursor,因為他包含了數據庫里所有行的信息,所以我直接用他來填充到每個item。如果這個填充的信息是其他的,就是用到其他數據結構了,如?List<Map<String,?Object>>?mData;? 來保存信息。具體可以參考:[http://mobile.51cto.com/android-254823.htm](http://mobile.51cto.com/android-254823.htm)
[http://blog.csdn.net/a859522265/article/details/8204646](http://blog.csdn.net/a859522265/article/details/8204646)? ?[http://www.linuxidc.com/Linux/2011-11/47179p2.htm](http://www.linuxidc.com/Linux/2011-11/47179p2.htm)? [http://blog.sina.com.cn/s/blog_65570a20010108lp.html](http://blog.sina.com.cn/s/blog_65570a20010108lp.html)?? ?[http://bbs.csdn.net/topics/330062289](http://bbs.csdn.net/topics/330062289)?我主要參考的第一篇。
**另外,在數據庫里怎么獲得Cursor呢?**
cursor = mPalmDB.select();
select()函數的封裝是:
~~~
public Cursor select(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
return cursor;
}
~~~
這個可以封裝在數據庫類里,也可以寫到函數里。
源碼連同數據庫操作部分改日再提供哈!
效果圖:
1,主界面

2,在數據庫里添加兩個數據后:

3,點擊上面的“選定”按鍵,切換到另外一個界面

4,選中這兩個條目:

源碼下載:[http://download.csdn.net/detail/yanzi1225627/5226894](http://download.csdn.net/detail/yanzi1225627/5226894)
歡迎android愛好者加群248217350??? ?備注:yanzi
----------------------------本文系原創,轉載請注明作者:yanzi1225627
- 前言
- Linux下使用QT調用opencv讀取攝像頭視頻 調試心得
- Android開發 攝像頭SurfaceView預覽 背景帶矩形框 實現(原理:雙surfaceview,頂層畫矩形框,底層預覽視頻)
- Android開發:安裝NDK,移植OpenCV2.3.1,JNI調用OpenCV全過程
- 2013新春奉送:Android攝像頭開發完美demo---(循環聚焦,縮放大小,旋轉picture,查詢支持的picturesize, ImageButton按鍵效果)
- 如何設置ImageButton按鍵按下去后的 特效----(如類似風車旋轉的動畫特效)
- Android攝像頭:只拍攝SurfaceView預覽界面特定區域內容(矩形框)---完整實現(原理:底層SurfaceView+上層繪制ImageView)
- Android開發:SurfaceView上新建線程繪制旋轉圖片 及 刷新特定區域(臟矩形)
- Android開發:ImageView上繪制旋轉圓環(透明度不同的旋轉圓環,利用canvas.drawArc實現)
- Android上掌紋識別第一步:基于OpenCV的6種膚色分割 源碼和效果圖
- Android開發:實時處理攝像頭預覽幀視頻------淺析PreviewCallback,onPreviewFrame,AsyncTask的綜合應用
- Android攝像頭開發:拍照后添加相框,融合相框和圖片為一副 圖片
- Android(OpenCV) NDK開發: 0xdeadbaad(code=1)錯誤 及 關閉armeabi和libnative_camera_r2.2.2.so的生成
- Android攝像頭開發:實時攝像頭視頻預覽幀的編碼問題(二)
- setContentView切換頁面(無需每次都findViewById)-----二
- Android開發:setContentView切換界面,自定義帶CheckBox的ListView顯示SQlite條目-----實現