Example usage for android.util SparseBooleanArray indexOfValue

List of usage examples for android.util SparseBooleanArray indexOfValue

Introduction

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

Prototype

public int indexOfValue(boolean value) 

Source Link

Document

Returns an index for which #valueAt would return the specified key, or a negative number if no keys map to the specified value.

Usage

From source file:net.kazzz.NFCTagWriter.java

/**
 * ISO15693 Tag????????//  ww  w  . ja  va2s.c o m
 * 
 * 
 * @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.kontalk.ui.AbstractComposeFragment.java

private int getCheckedItemPosition() {
    SparseBooleanArray checked = getListView().getCheckedItemPositions();
    return checked.keyAt(checked.indexOfValue(true));
}