Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 入門整合實作:節日祝福簡訊群發助手(下)

編輯:關於 Android 程式設計

Android 基礎整合專案(一)之節日群發助手 Part 2

——轉載請註明出處:coder-pig

本節引言

上一節我們完成了群發助手的第一個畫面,也做好了聯絡人讀取與資料庫寫入。這一節要繼續完成:

1)自定義 ListView 的列表項:兩個 TextView 加上一個 CheckBox;

2)用 SimpleCursorAdapter 把資料顯示到 ListView;

3)實作全選與全不選;

4)點擊重置鈕時,把資料庫中所有 state 重設為 -1。

正文

一、建立第二個 Activity 的版面

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jay.example.festivalsmshelper.MainActivity" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="選擇要發送的人" />

    <ListView
        android:id="@+id/listcontacts"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/TextView1"
        android:layout_above="@+id/buttomlayout"
        android:layout_marginTop="10dp" >
    </ListView>

    <LinearLayout
        android:id="@+id/buttomlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全選" />

        <Button
            android:id="@+id/btnreset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="重置" />

        <Button
            android:id="@+id/btnnext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一步" />

        <TextView
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="已選"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/textshow"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp" />

    </LinearLayout>

</RelativeLayout>

二、建立列表項版面

做法是把版面用 weight 三等分,另外隱藏兩個 TextView(一個放 _id,一個暫存勾選狀態)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/listid"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <TextView
        android:id="@+id/listname"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/listphone"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/listhide"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <CheckBox
        android:id="@+id/listchoice"
        android:focusable="false"
        android:clickable="false"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

</LinearLayout>

三、用 SimpleCursorAdapter 綁定資料庫與 ListView

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 別名),且查詢結果一定要包含它,否則會拋出:

java.lang.IllegalArgumentException: column '_id' does not exist

四、在 onCreate() 中呼叫

在 Activity 的 onCreate() 呼叫上面的 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);
        //用這個變數區分該列是否勾選
        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,把勾選狀態的聯絡人 id 收集到集合中;② 透過 putIntegerArrayListExtra 把集合放進 Intent,帶到下一個頁面。

btnnext.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        ArrayList<Integer> checkedId = new ArrayList<Integer>();
        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 欄位或別名。

4)如何走訪 ListView 統計 CheckBox 為勾選狀態的列數。

5)用 Intent 的 putIntegerArrayListExtra 傳遞 ArrayList 集合到另一個 Activity。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved