編輯:關於Android編程
Android基礎整合項目(一) 之節日群發助手part 2
——轉載請注明出處:coder-pig
本節引言:
在上一節中我們已經做出了我們群發助手的第一個界面以及完成了聯系人的讀取以及數據庫的
錄入了,在這一節中將要完成的工作是:
1)自定義我們的ListView的列表項,兩個TextView + CheckBox;
2)使用SimpleCursorAdapter將數據顯示到ListView上;
3)實現listview的全選與全不選
4)點擊重置按鈕後,將數據庫中的state都重置為-1
好了,那麼開始本節的內容吧!

布局代碼如下:
其實就是三等分了布局,另外還隱藏了兩個textview
代碼如下:
<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjxwcmUgY2xhc3M9"brush:java;">
代碼如下:
private void getContacts()
{
GetContactsService gs = new GetContactsService(ChooseActivity.this);
Cursor cursor = gs.query("select _id,pname,pnumber,pstate from contacts", null);
//注意:使用SimpleCursorAdapter的話,這個_id是必不可少的!不然一直報錯
//所以在前面需要弄一個隱藏的組件來放_id
simpleCursorAdapter = new SimpleCursorAdapter(ChooseActivity.this, R.layout.listitem,
cursor, new String[]{"_id","pname","pnumber","pstate"},
new int[]{R.id.listid,R.id.listname,R.id.listphone,R.id.listhide});
list.setAdapter(simpleCursorAdapter);
}
注意!!!!使用SimpleCursorAdapter的話,綁定的數據庫表中一定要有_id這個字段,或者as _id;
而且在綁定時取出的數據必須包含這個_id項,否則的話會報以下錯誤!
java.lang.IllegalArgumentException: column '_id' does not exist
在Activity中調用3的getContacts( ),就可以看到我們的已經把數據庫的數據顯示到ListView上了:

①為listView設置setOnItemClickListener方法,即點擊事件
②點擊後要左什麼呢?改變checkbox的選中狀態,以及統計有多少個選中,然後顯示出來
先定義一個用於統計當前選中的復選框數量:
private int setShow()
{
int num = 0;
for(int i = 0;i < list.getChildCount();i++)
{
LinearLayout layout = (LinearLayout) list.getChildAt(i);
CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
if(cbx.isChecked())num++;
}
return num;
}接著實現點擊後復選框選中,以及已選數目的改變:
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
CheckBox cb = (CheckBox) view.findViewById(R.id.listchoice);
TextView itemhide = (TextView) view.findViewById(R.id.listhide);
TextView itemid = (TextView) view.findViewById(R.id.listid);
int state = Integer.parseInt(itemhide.getText().toString());
//利用這個變量來區分按鈕是否選中
state *= -1;
itemhide.setText(state+"");
if(state == 1)cb.setChecked(true);
else cb.setChecked(false);
textshow.setText(setShow()+"項");
}
});
完成上述代碼後運行出現以下效果:

就是選中全部的列表項,接著顯示數目而已!
btnall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
allflag *= -1;
if(allflag == -1)
{
for(int i = 0;i < list.getChildCount();i++)
{
LinearLayout layout = (LinearLayout) list.getChildAt(i);
CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
cbx.setChecked(true);
btnall.setText("全不選");
textshow.setText(setShow()+"項");
}
}
else if(allflag == 1)
{
for(int i = 0;i < list.getChildCount();i++)
{
LinearLayout layout = (LinearLayout) list.getChildAt(i);
CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
cbx.setChecked(false);
btnall.setText("全選");
textshow.setText(setShow()+"項");
}
}
}
});
在這個按鈕事件要完成的工作:
①遍歷listview,獲取checkbox為選中狀態的,將對應聯系人id存儲到集合中!
②將集合存放到Intent中,it.putIntegerArrayListExtra("ids", checkedId);
代碼如下:
btnnext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ArrayList checkedId = new ArrayList();
for(int i = 0;i < list.getChildCount();i++)
{
LinearLayout layout = (LinearLayout) list.getChildAt(i);
CheckBox cbx = (CheckBox) layout.findViewById(R.id.listchoice);
TextView txtid = (TextView) layout.findViewById(R.id.listid);
if(cbx.isChecked())
checkedId.add(Integer.parseInt(txtid.getText().toString()));
}
//跳轉到第三個頁面,同時把數據存儲到intent對象中
Intent it = new Intent(ChooseActivity.this,ThridActivity.class);
it.putIntegerArrayListExtra("ids", checkedId);
startActivity(it);
}
}); 恩呢,第二個界面就做好了!本節也到此結束了!
好了,最後總結下這節中用到的知識點:
1)在LinearLayout中使用weight屬性將水平或者豎直方向平分成多份!
2)使用visibility對組件進行隱藏:visible(可見),invisible(不可見,但還占據空間),gone(不可見,也不占空間)
3)SimpleCursorAdapter綁定數據庫與ListView需要注意必須要有:_id這個字段或者某字段 as _id;
4)如何統計listview復選框為選中狀態的列數,遍歷listview!
5)使用intent的putIntegerArrayListExtra存儲ArrayList
Android 自定義View之隨機生成圖片驗證碼
本篇文章講的是Android自定義View之隨機生成圖片驗證碼,開發中我們會經常需要隨機生成圖片驗證碼,但是這個是其次,主要還是想總結一些自定義View的開發過程以及一些
小米筆記本Air配置一覽 小米筆記本Air完整配置說明
小米筆記本Air已經發布,在配置上有兩個版本,一款是13寸的高配版和12寸的輕薄款,具體的配置我們來看看。小米筆記本配置小米筆記本Air采用全金屬超輕薄設計
自己動手做一個adb的wifi連接及adb命令的apk
前言估計搞安卓開發的小伙伴們都放棄了eclipse的開發轉戰android studio了吧,現在gradle都3.0 buiild都24.0.2了以後 都不再支持2.2
48.Android 標簽TextView的點擊技巧
48.Android 標簽TextView的點擊技巧Android 標簽TextView的點擊技巧 前言 ClickableSpan源碼 自定義ClickableSpan