Example usage for android.util SparseBooleanArray size

List of usage examples for android.util SparseBooleanArray size

Introduction

In this page you can find the example usage for android.util SparseBooleanArray size.

Prototype

public int size() 

Source Link

Document

Returns the number of key-value mappings that this SparseBooleanArray currently stores.

Usage

From source file:nl.mpcjanssen.simpletask.Simpletask.java

@NotNull
private List<Task> getCheckedTasks() {
    ArrayList<Task> checkedTasks = new ArrayList<Task>();
    SparseBooleanArray checkedItems = getListView().getCheckedItemPositions();
    if (checkedItems == null) {
        return checkedTasks;
    }//from   w  w w  .  ja  va  2s.  c  om
    for (int i = 0; i < checkedItems.size(); i++) {
        if (checkedItems.valueAt(i)) {
            Task t = getTaskAt(checkedItems.keyAt(i));
            // Ignore headers
            if (t != null) {
                checkedTasks.add(getTaskAt(checkedItems.keyAt(i)));
            }
        }
    }
    return checkedTasks;
}

From source file:com.tweetlanes.android.core.view.TweetFeedFragment.java

@Override
public void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);

    if (mTwitterStatusIdWhenRefreshed != null)
        state.putLong("TwitterStatusIdWhenRefreshed", mTwitterStatusIdWhenRefreshed);

    if (mLastTwitterStatusIdSeen != null)
        state.putLong("LastTwitterStatusIdSeen", mLastTwitterStatusIdSeen);

    state.putInt("NewStatuses", mNewStatuses);
    state.putBoolean("HidingListHeading", mHidingListHeading);

    mSelectedItems.clear();/*w ww .  j  a  v  a 2  s. c  o m*/
    android.util.SparseBooleanArray items = mTweetFeedListView.getRefreshableView().getCheckedItemPositions();
    for (int i = 0; i < items.size(); i++) {
        int key = items.keyAt(i);
        mTweetFeedListView.getRefreshableView().setItemChecked(key, false);
    }

}

From source file:com.huewu.pla.lib.internal.PLAListView.java

/**
 * Returns the set of checked items ids. The result is only valid if the
 * choice mode has not been set to {@link #CHOICE_MODE_NONE}.
 * /*from   w ww. j av  a2s .c o  m*/
 * @return A new array which contains the id of each checked item in the
 *         list.
 * 
 * @deprecated Use {@link #getCheckedItemIds()} instead.
 */
@Deprecated
public long[] getCheckItemIds() {
    // Use new behavior that correctly handles stable ID mapping.
    if (mAdapter != null && mAdapter.hasStableIds())
        return getCheckedItemIds();

    // Old behavior was buggy, but would sort of work for adapters without
    // stable IDs.
    // Fall back to it to support legacy apps.
    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
        final SparseBooleanArray states = mCheckStates;
        final int count = states.size();
        final long[] ids = new long[count];
        final ListAdapter adapter = mAdapter;

        int checkedCount = 0;
        for (int i = 0; i < count; i++) {
            if (states.valueAt(i)) {
                ids[checkedCount++] = adapter.getItemId(states.keyAt(i));
            }
        }

        // Trim array if needed. mCheckStates may contain false values
        // resulting in checkedCount being smaller than count.
        if (checkedCount == count)
            return ids;
        else {
            final long[] result = new long[checkedCount];
            System.arraycopy(ids, 0, result, 0, checkedCount);

            return result;
        }
    }
    return new long[0];
}

From source file:org.mozilla.gecko.GeckoApp.java

/**
 * @param aPermissions//w w  w. ja  v a  2  s .c om
 *        Array of JSON objects to represent site permissions.
 *        Example: { type: "offline-app", setting: "Store Offline Data: Allow" }
 */
private void showSiteSettingsDialog(String aHost, JSONArray aPermissions) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    View customTitleView = getLayoutInflater().inflate(R.layout.site_setting_title, null);
    ((TextView) customTitleView.findViewById(R.id.title)).setText(R.string.site_settings_title);
    ((TextView) customTitleView.findViewById(R.id.host)).setText(aHost);
    builder.setCustomTitle(customTitleView);

    // If there are no permissions to clear, show the user a message about that.
    // In the future, we want to disable the menu item if there are no permissions to clear.
    if (aPermissions.length() == 0) {
        builder.setMessage(R.string.site_settings_no_settings);
    } else {
        // Eventually we should use a list adapter and custom checkable list items
        // to make a two-line UI to match the mock-ups
        CharSequence[] items = new CharSequence[aPermissions.length()];
        boolean[] states = new boolean[aPermissions.length()];
        for (int i = 0; i < aPermissions.length(); i++) {
            try {
                items[i] = aPermissions.getJSONObject(i).getString("setting");
                // Make all the items checked by default
                states[i] = true;
            } catch (JSONException e) {
                Log.i(LOGTAG, "JSONException: " + e);
            }
        }
        builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener() {
            public void onClick(DialogInterface dialog, int item, boolean state) {
                // Do nothing
            }
        });
        builder.setPositiveButton(R.string.site_settings_clear, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                ListView listView = ((AlertDialog) dialog).getListView();
                SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();

                // An array of the indices of the permissions we want to clear
                JSONArray permissionsToClear = new JSONArray();
                for (int i = 0; i < checkedItemPositions.size(); i++) {
                    boolean checked = checkedItemPositions.get(i);
                    if (checked)
                        permissionsToClear.put(i);
                }
                GeckoAppShell.sendEventToGecko(
                        GeckoEvent.createBroadcastEvent("Permissions:Clear", permissionsToClear.toString()));
            }
        });
    }

    builder.setNegativeButton(R.string.site_settings_cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    mMainHandler.post(new Runnable() {
        public void run() {
            builder.create().show();
        }
    });
}

From source file:net.kazzz.NFCTagWriter.java

/**
 * ISO15693 Tag????????// ww  w. ja  v  a2  s  .com
 * 
 * 
 * @param btnWrite ?????
 * @param editWrite ??
 * @param listMemBlock?
 * @param useNDEFNDEF????
 */
private void prepareISO15693(final Button btnWrite, final EditText editWrite, final ListView listMemBlock) {
    final CheckBox chkUseNDEF = (CheckBox) findViewById(R.id.chk_useNDEF);

    //??
    ISO15693Tag ft = new ISO15693Tag(this.mNfcTag);
    try {
        SystemInformationResponse sysInfo = ft.getSystemInformation();
        if (sysInfo == null || sysInfo.hasError()) {
            throw new ISO15693Exception(
                    "ISO15693 ??????????? : "
                            + sysInfo.getErrorCode());
        }

        final MemorySizeInfo memInfo = sysInfo.getMemoryInfo();
        if (memInfo == null || memInfo.getNumberOfBlocks() == 0) {
            throw new ISO15693Exception("ISO15693 ?????????");
        }

        chkUseNDEF.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                listMemBlock.setEnabled(!chkUseNDEF.isChecked());
                listMemBlock.setClickable(!chkUseNDEF.isChecked());
                //NDEF??NDEF?????
                if (chkUseNDEF.isChecked()) {
                    btnWrite.setEnabled(true);
                    editWrite.setEnabled(true);
                    //NDEF????10byte?
                    editWrite.setHint((memInfo.getBlockSize() * memInfo.getNumberOfBlocks()) - 10
                            + "??");
                } else {
                    if (editWrite.getText().length() > 0) {
                        btnWrite.setEnabled(true);
                        editWrite.setEnabled(true);
                    } else {
                        btnWrite.setEnabled(false);
                        editWrite.setEnabled(false);
                    }
                    editWrite.setHint(memInfo.getBlockSize() + "??");
                }
            }
        });

        ReadMultipleBlocksResponse rbResp = ft.readMultipleBlocks((byte) 0, memInfo.getBlockSize(),
                memInfo.getNumberOfBlocks());
        if (rbResp == null || rbResp.hasError()) {
            throw new ISO15693Exception(
                    "ISO15693 ReadMultipleBlock?????" + sysInfo.getErrorCode());
        }

        //??????
        ViewHolder[] holders = new ViewHolder[memInfo.getNumberOfBlocks()];
        for (byte i = 0; i < memInfo.getNumberOfBlocks(); i++) {
            BlockData bd = rbResp.getBlockDatas()[i];

            ViewHolder holder = new ViewHolder();
            holder.blockName = "block " + i;
            holder.isWritable = !bd.isLocked();
            holder.accessMode = bd.isLocked() ? "(Lock)" : "(UnLock)";

            //??
            if (bd.getData() != null && bd.getData().length > 0) {
                holder.data = bd.getData();
            } else {
                holder.data = new byte[memInfo.getBlockSize()];
                for (int b = 0; b < holder.data.length; b++) {
                    holder.data[b] = 0x00; //??
                }
            }
            holders[i] = holder;
        }
        final LayoutInflater layoutInflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //ArrayAdapter? (??
        ArrayAdapter<ViewHolder> adapter = new ArrayAdapter<ViewHolder>(this,
                android.R.layout.simple_list_item_multiple_choice, holders) {

            /* (non-Javadoc)
             * @see android.widget.BaseAdapter#isEnabled(int)
             */
            @Override
            public boolean isEnabled(int position) {
                SparseBooleanArray arry = listMemBlock.getCheckedItemPositions();
                if (arry.indexOfValue(true) != -1 && !arry.get(position) //??????? 
                        && (!arry.get(position - 1) && !arry.get(position + 1))) //????? 
                {
                    //???????
                    return false;
                }
                return super.isEnabled(position);
            }

            /* (non-Javadoc)
             * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
             */
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                if (null == convertView) {
                    convertView = layoutInflater.inflate(android.R.layout.simple_list_item_multiple_choice,
                            null);
                }

                ViewHolder holder = this.getItem(position);

                if (holder != null) {

                    StringBuilder sb = new StringBuilder();
                    sb.append(holder.blockName).append(" ").append(holder.accessMode).append("\n");
                    if (holder.data != null && holder.data.length > 0) {
                        //UTF-8?
                        Charset utfEncoding = Charset.forName("UTF-8");
                        sb.append(" : ").append(new String(holder.data, utfEncoding).trim());
                    }
                    ((TextView) convertView).setText(sb.toString());
                }
                return convertView;
            }

        };

        listMemBlock.setAdapter(adapter);

        // ?????  
        //listMemBlock.setItemsCanFocus(false);  
        //?
        listMemBlock.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        listMemBlock.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //?????
                SparseBooleanArray arry = listMemBlock.getCheckedItemPositions();

                ByteBuffer buff = ByteBuffer.allocate(arry.size() * memInfo.getBlockSize());
                int writableBlockCount = 0;
                for (int i = 0; i < arry.size(); i++) {
                    int key = arry.keyAt(i);
                    //?????
                    Object o = parent.getItemAtPosition(key);
                    if (o != null && o instanceof ViewHolder) {
                        ViewHolder holder = (ViewHolder) o;
                        if (holder.isWritable) {
                            writableBlockCount++;
                            if (holder.data != null && holder.data.length > 0) {
                                buff.put(holder.data);
                            }
                        }
                    }
                }
                //UTF-8?
                StringBuilder sb = new StringBuilder();
                Charset utfEncoding = Charset.forName("UTF-8");
                sb.append(new String(buff.array(), utfEncoding).trim());

                editWrite.setHint(memInfo.getBlockSize() * writableBlockCount + "??");

                if (writableBlockCount > 0) {
                    editWrite.setText(sb.toString().trim());
                    editWrite.setTag(arry);
                    //????????????
                    btnWrite.setEnabled(true);
                    editWrite.setEnabled(true);
                } else {
                    btnWrite.setEnabled(false);
                    editWrite.setEnabled(false);
                }

            }
        });
    } catch (ISO15693Exception e) {
        e.printStackTrace();
        Log.e(TAG, e.toString());
    }
}

From source file:org.bangbang.support.v4.widget.HListView.java

/**
     * Returns the set of checked items ids. The result is only valid if
     * the choice mode has not been set to {@link #CHOICE_MODE_SINGLE}.
     */* w  w  w. ja  va2 s .  c o  m*/
     * @return A new array which contains the id of each checked item in the list.
     */
    public long[] getCheckItemIds() {
        if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
            final SparseBooleanArray states = mCheckStates;
            final int count = states.size();
            final long[] ids = new long[count];
            final ListAdapter adapter = mAdapter;

            for (int i = 0; i < count; i++) {
                ids[i] = adapter.getItemId(states.keyAt(i));
            }

            return ids;
        }

        return new long[0];
    }

From source file:com.appunite.list.HorizontalListView.java

/**
 * Returns the set of checked items ids. The result is only valid if the
 * choice mode has not been set to {@link #CHOICE_MODE_NONE}.
 * //w  ww.j  a v a 2  s.  co  m
 * @return A new array which contains the id of each checked item in the
 *         list.
 *         
 * @deprecated Use {@link #getCheckedItemIds()} instead.
 */
@Deprecated
public long[] getCheckItemIds() {
    // Use new behavior that correctly handles stable ID mapping.
    if (mAdapter != null && mAdapter.hasStableIds()) {
        return getCheckedItemIds();
    }

    // Old behavior was buggy, but would sort of work for adapters without stable IDs.
    // Fall back to it to support legacy apps.
    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
        final SparseBooleanArray states = mCheckStates;
        final int count = states.size();
        final long[] ids = new long[count];
        final ListAdapter adapter = mAdapter;

        int checkedCount = 0;
        for (int i = 0; i < count; i++) {
            if (states.valueAt(i)) {
                ids[checkedCount++] = adapter.getItemId(states.keyAt(i));
            }
        }

        // Trim array if needed. mCheckStates may contain false values
        // resulting in checkedCount being smaller than count.
        if (checkedCount == count) {
            return ids;
        } else {
            final long[] result = new long[checkedCount];
            System.arraycopy(ids, 0, result, 0, checkedCount);

            return result;
        }
    }
    return new long[0];
}