Android Open Source - task-timer-legacy Checkable Adapter






From Project

Back to project page task-timer-legacy.

License

The source code is released under:

GNU General Public License

If you think the Android project task-timer-legacy listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gawdl3y.android.actionablelistview;
/*  w w w .j a v  a2 s .c  om*/
import android.util.SparseBooleanArray;
import android.widget.BaseAdapter;

/**
 * An adapter that can keep track of item checked states
 * <p/>
 * Note that if the data set is changed while items are checked, the positions will
 * not accurately reflect the new positions of the items.
 * @author Schuyler Cebulskie
 */
public abstract class CheckableAdapter extends BaseAdapter {
    private SparseBooleanArray mCheckStates = new SparseBooleanArray();
    private int mNumCheckedItems = 0;

    /**
     * Toggles the checked state of the item at {@code position}
     * @param position The position of the item to toggle
     */
    public void toggleItem(int position) {
        mCheckStates.put(position, !mCheckStates.get(position));
        if(mCheckStates.get(position)) mNumCheckedItems++; else mNumCheckedItems--;
    }

    /**
     * Sets whether or not the item at {@code position} is checked
     * @param position The position of the item
     * @param checked  Whether or not the item is checked
     */
    public void setItemChecked(int position, boolean checked) {
        if(checked && !mCheckStates.get(position)) mNumCheckedItems++;
        else if(!checked && mCheckStates.get(position)) mNumCheckedItems--;
        mCheckStates.put(position, checked);
    }

    /**
     * Sets the checked state of all of the items
     * @param checked Whether or not the items are checked
     */
    public void setAllItemsChecked(boolean checked) {
        for(int i = 0; i < getCount(); i++) {
            if(checked && !mCheckStates.get(i)) mNumCheckedItems++;
            else if(!checked && mCheckStates.get(i)) mNumCheckedItems--;
            mCheckStates.put(i, checked);
        }
    }

    /**
     * @param position The position of the item to test
     * @return {@code true} if the item at {@code position} is checked, {@code false} if not
     */
    public boolean isItemChecked(int position) {
        return mCheckStates.get(position);
    }

    /**
     * @return The number of checked items
     */
    public int getCheckedItemCount() {
        return mNumCheckedItems;
    }

    /**
     * @return An array of positions of all of the checked items
     */
    public int[] getCheckedItemPositions() {
        int[] positions = new int[mNumCheckedItems];
        int p = 0;
        for(int i = 0; i < mCheckStates.size(); i++) {
            if(mCheckStates.valueAt(i)) {
                positions[p] = mCheckStates.keyAt(i);
                p++;
            }
        }
        return positions;
    }

    /**
     * @return An array of IDs of all of the checked items
     */
    public long[] getCheckedItemIds() {
        long[] ids = new long[mNumCheckedItems];
        int p = 0;
        for(int i = 0; i < mCheckStates.size(); i++) {
            if(mCheckStates.valueAt(i)) {
                ids[p] = getItemId(mCheckStates.keyAt(i));
                p++;
            }
        }
        return ids;
    }

    /**
     * @return The checked item states
     */
    public SparseBooleanArray getCheckStates() {
        return mCheckStates;
    }

    /**
     * Sets the checked item states
     * @param states The checked item states
     * @throws NullPointerException If {@code states == null}
     */
    public void setCheckStates(SparseBooleanArray states) {
        if(states == null) throw new NullPointerException();
        mCheckStates = states;
    }
}




Java Source Code List

com.gawdl3y.android.actionablelistview.ActionItem.java
com.gawdl3y.android.actionablelistview.ActionableAdapter.java
com.gawdl3y.android.actionablelistview.ActionableListFragment.java
com.gawdl3y.android.actionablelistview.ActionableListView.java
com.gawdl3y.android.actionablelistview.Actionable.java
com.gawdl3y.android.actionablelistview.CheckableAdapter.java
com.gawdl3y.android.actionablelistview.CheckableListView.java
com.gawdl3y.android.actionablelistview.OnListItemCheckStateChangeListener.java
com.gawdl3y.android.tasktimer.TaskTimerApplication.java
com.gawdl3y.android.tasktimer.activities.MainActivity.java
com.gawdl3y.android.tasktimer.activities.SettingsActivity.java
com.gawdl3y.android.tasktimer.adapters.GroupListAdapter.java
com.gawdl3y.android.tasktimer.adapters.NewFragmentStatePagerAdapter.java
com.gawdl3y.android.tasktimer.adapters.TaskListAdapter.java
com.gawdl3y.android.tasktimer.adapters.TaskListFragmentAdapter.java
com.gawdl3y.android.tasktimer.data.TaskTimerDatabaseHelper.java
com.gawdl3y.android.tasktimer.data.TaskTimerProvider.java
com.gawdl3y.android.tasktimer.data.TaskTimerReceiver.java
com.gawdl3y.android.tasktimer.layout.GroupEditDialogFragment.java
com.gawdl3y.android.tasktimer.layout.GroupListItem.java
com.gawdl3y.android.tasktimer.layout.GroupsFragment.java
com.gawdl3y.android.tasktimer.layout.SettingsFragment.java
com.gawdl3y.android.tasktimer.layout.TaskEditDialogFragment.java
com.gawdl3y.android.tasktimer.layout.TaskListFragment.java
com.gawdl3y.android.tasktimer.layout.TaskListItem.java
com.gawdl3y.android.tasktimer.layout.TasksFragment.java
com.gawdl3y.android.tasktimer.pojos.Group.java
com.gawdl3y.android.tasktimer.pojos.TaskTimerEvents.java
com.gawdl3y.android.tasktimer.pojos.TaskTimerThread.java
com.gawdl3y.android.tasktimer.pojos.Task.java
com.gawdl3y.android.tasktimer.pojos.TimeAmount.java
com.gawdl3y.android.tasktimer.util.DialogPreference.java
com.gawdl3y.android.tasktimer.util.Log.java
com.gawdl3y.android.tasktimer.util.Utilities.java