Example usage for android.util SparseBooleanArray valueAt

List of usage examples for android.util SparseBooleanArray valueAt

Introduction

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

Prototype

public boolean valueAt(int index) 

Source Link

Document

Given an index in the range 0...size()-1, returns the value from the indexth key-value mapping that this SparseBooleanArray stores.

Usage

From source file:net.sourceforge.servestream.fragment.UrlListFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

    DragSortListView list = (DragSortListView) getListView();
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    list.setDropListener(dropListener);/*from w w  w .  java2 s .  c  o  m*/
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public synchronized void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (mActionMode == null) {
                UriBean uriBean = (UriBean) parent.getItemAtPosition(position);
                processUri(uriBean.getUri().toString());
            } else {
                for (int i = 0; i < mChecked.size(); i++) {
                    if (mChecked.keyAt(i) == position && mChecked.valueAt(i)) {
                        view.setBackgroundResource(0);
                        getListView().setItemChecked(position, false);
                        mAdapter.notifyDataSetChanged();
                    }
                }

                // Notice how the ListView api is lame
                // You can use mListView.getCheckedItemIds() if the adapter
                // has stable ids, e.g you're using a CursorAdaptor
                SparseBooleanArray checked = getListView().getCheckedItemPositions();
                boolean hasCheckedElement = false;

                checked = getListView().getCheckedItemPositions();

                for (int i = 0; i < checked.size(); i++) {
                    if (checked.valueAt(i)) {
                        View v = getListView().getChildAt(checked.keyAt(i));
                        if (v != null) {
                            v.setBackgroundColor(getActivity().getResources()
                                    .getColor(R.color.selection_background_color_light));
                        }
                    }
                }

                mAdapter.notifyDataSetChanged();

                for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
                    hasCheckedElement = checked.valueAt(i);
                }

                if (hasCheckedElement) {
                    if (mActionMode == null) {
                        mActionMode = ((ActionBarActivity) getActivity())
                                .startSupportActionMode(mActionModeCallback);
                    }
                } else {
                    if (mActionMode != null) {
                        mActionMode.finish();
                        mActionMode = null;
                    }
                }

                mChecked = getListView().getCheckedItemPositions().clone();
            }
        }
    });
    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (mActionMode == null) {
                mActionMode = ((ActionBarActivity) getActivity()).startSupportActionMode(mActionModeCallback);
                getListView().setItemChecked(position, true);
                view.setBackgroundColor(
                        getActivity().getResources().getColor(R.color.selection_background_color_light));
                mChecked = getListView().getCheckedItemPositions().clone();
                return true;
            }
            return false;
        }
    });

    DragSortController controller = new DragSortController(list);
    controller.setDragInitMode(DragSortController.ON_DRAG);
    controller.setDragHandleId(R.id.drag_handle);
    list.setOnTouchListener(controller);

    mAdapter = new UrlListAdapter(getActivity(), new ArrayList<UriBean>(), this);
    setListAdapter(mAdapter);
}

From source file:com.github.jobs.ui.dialog.RemoveServicesDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final ArrayList<Parcelable> services = getArguments().getParcelableArrayList(ARG_SERVICES);
    CharSequence[] items = new CharSequence[services.size()];
    for (int i = 0, servicesSize = services.size(); i < servicesSize; i++) {
        TemplateService service = (TemplateService) services.get(i);
        items[i] = getString(R.string.service_list_item, service.getType(), service.getData());
    }//from  w ww  . j ava2 s . c  o  m
    final SparseBooleanArray checked = new SparseBooleanArray();
    return new AlertDialog.Builder(getActivity()).setTitle(R.string.title_remove_services)
            .setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    checked.put(which, isChecked);
                }
            }).setPositiveButton(R.string.remove_selected_services, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // get a list of the services to delete
                    ArrayList<TemplateService> toRemove = new ArrayList<TemplateService>();
                    for (int i = 0; i < checked.size(); i++) {
                        if (checked.valueAt(i)) {
                            int index = checked.keyAt(i);
                            toRemove.add((TemplateService) services.get(index));
                        }
                    }
                    // fire service deletion
                    FragmentActivity activity = getActivity();
                    if (activity instanceof EditTemplateActivity) {
                        EditTemplateActivity editTemplateActivity = (EditTemplateActivity) activity;
                        editTemplateActivity.removeServices(toRemove);
                    }
                }
            }).setNegativeButton(R.string.cancel, null).create();
}

From source file:net.naonedbus.fragment.impl.FavorisFragment.java

private boolean hasItemChecked() {
    final SparseBooleanArray checked = mListView.getCheckedItemPositions();
    for (int i = 0; i < checked.size(); i++) {
        if (checked.valueAt(i))
            return true;
    }//  w  w  w.j  a  va2 s .  c  o m
    return false;
}

From source file:net.naonedbus.fragment.impl.FavorisFragment.java

private List<Integer> getCheckedItemsIds() {
    final List<Integer> ids = new ArrayList<Integer>();
    final SparseBooleanArray checked = mListView.getCheckedItemPositions();
    for (int i = 0; i < checked.size(); i++) {
        if (checked.valueAt(i)) {
            final Favori item = (Favori) mListView.getItemAtPosition(checked.keyAt(i));
            ids.add(item.getId());//from  w ww .ja  v a 2  s  .  co  m
        }
    }

    return ids;
}

From source file:de.qspool.clementineremote.ui.fragments.LibraryFragment.java

@SuppressLint({ "InlinedApi", "NewApi" })
@Override//from  www .  j ava  2  s .c  om
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_library, container, false);

    mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.library_refresh_layout);
    mEmptyLibrary = (SwipeRefreshLayout) view.findViewById(R.id.library_refresh_empty_layout);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mEmptyLibrary.setOnRefreshListener(this);
    mSwipeRefreshLayout.setColorSchemeResources(R.color.orange);
    mEmptyLibrary.setColorSchemeResources(R.color.orange);

    mList = (ListView) view.findViewById(R.id.library);

    mLibraryEmptyText = (TextView) mEmptyLibrary.findViewById(R.id.library_empty_txt);

    mList.setOnItemClickListener(oiclLibraryClick);
    mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    mList.setMultiChoiceModeListener(new MultiChoiceModeListener() {
        @Override
        public boolean onActionItemClicked(ActionMode mode, android.view.MenuItem item) {
            SparseBooleanArray checkedPositions = mList.getCheckedItemPositions();
            final LinkedList<SongSelectItem> selectedItems = new LinkedList<>();
            final LinkedList<String> urls = new LinkedList<>();
            mLibraryQueriesDone = 0;

            for (int i = 0; i < checkedPositions.size(); ++i) {
                int position = checkedPositions.keyAt(i);
                if (checkedPositions.valueAt(i)) {
                    selectedItems.add(mAdapters.getLast().getItem(position));
                }
            }

            for (SongSelectItem libraryItem : selectedItems) {
                OnSongSelectFinishedListener listener;

                switch (item.getItemId()) {
                case R.id.library_context_add:

                    listener = new OnSongSelectFinishedListener() {
                        @Override
                        public void OnSongSelectFinished(LinkedList<SongSelectItem> l) {
                            addSongsToPlaylist(l);
                        }
                    };

                    break;
                case R.id.library_context_download:
                    listener = new OnSongSelectFinishedListener() {
                        @Override
                        public void OnSongSelectFinished(LinkedList<SongSelectItem> l) {
                            for (SongSelectItem libItem : l) {
                                urls.add(libItem.getUrl());
                            }
                            mLibraryQueriesDone++;

                            // Have we got all queries?
                            if (mLibraryQueriesDone == selectedItems.size() && !urls.isEmpty()) {
                                DownloadManager.getInstance()
                                        .addJob(ClementineMessageFactory.buildDownloadSongsMessage(
                                                ClementineRemoteProtocolBuffer.DownloadItem.Urls, urls));
                            }

                        }
                    };

                    break;
                default:
                    return false;
                }
                queryLibraryItems(libraryItem, listener);
            }
            mode.finish();
            return true;
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, android.view.Menu menu) {
            android.view.MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.library_context_menu, menu);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getActivity().getWindow()
                        .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.grey_cab_status));
            }

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, android.view.Menu menu) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getActivity().getWindow()
                        .setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.actionbar_dark));
            }
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        }
    });

    createRootAdapter();

    showList();

    mActionBar.setTitle("");
    mActionBar.setSubtitle("/");

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    prefs.registerOnSharedPreferenceChangeListener(this);

    setHasOptionsMenu(true);

    return view;
}

From source file:net.naonedbus.fragment.impl.FavorisFragment.java

private Favori getFirstSelectedItem() {
    final SparseBooleanArray checkedPositions = mListView.getCheckedItemPositions();
    for (int i = 0; i < checkedPositions.size(); i++) {
        if (checkedPositions.valueAt(i)) {
            return (Favori) mListView.getItemAtPosition(checkedPositions.keyAt(i));
        }//w  w w  . j av  a2  s  .c om
    }
    return null;
}

From source file:net.naonedbus.fragment.impl.FavorisFragment.java

private int getCheckedItemsCount() {
    final SparseBooleanArray checkedPositions = mListView.getCheckedItemPositions();
    int count = 0;
    for (int i = 0; i < checkedPositions.size(); i++) {
        if (checkedPositions.valueAt(i)) {
            count++;//from w  ww .ja v a2s .co  m
        }
    }
    return count;
}

From source file:org.dvbviewer.controller.ui.fragments.TimerList.java

@Override
public void onClick(DialogInterface dialog, int which) {
    switch (which) {
    case DialogInterface.BUTTON_POSITIVE:
        SparseBooleanArray checkedPositions = getListView().getCheckedItemPositions();
        Log.i(RecordingList.class.getSimpleName(), "items selected: " + checkedPositions.size());
        if (checkedPositions != null && checkedPositions.size() > 0) {
            int size = checkedPositions.size();
            TimerDeleter deleter = new TimerDeleter(TimerList.this);
            List<Timer> timers = new ArrayList<Timer>();
            for (int i = 0; i < size; i++) {
                if (checkedPositions.valueAt(i)) {
                    timers.add(mAdapter.getItem(checkedPositions.keyAt(i)));
                }/*from  ww  w .jav a2s .  c  o m*/
            }
            Timer[] array = new Timer[timers.size()];
            deleter.execute(timers.toArray(array));
        }
        mode.finish();
        break;

    case DialogInterface.BUTTON_NEGATIVE:
        // No button clicked
        break;
    }
}

From source file:org.spinsuite.bchat.view.FV_Thread.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (m_view != null)
        return m_view;
    //   Inflate// w  w  w .  ja va 2  s.c  om
    m_view = inflater.inflate(R.layout.v_business_chat_thread, container, false);
    //   Scroll
    lv_Thread = (ListView) m_view.findViewById(R.id.lv_Thread);
    et_Message = (EditText) m_view.findViewById(R.id.et_Message);
    ib_Send = (ImageButton) m_view.findViewById(R.id.ib_Send);
    //   Listener
    ib_Send.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (et_Message.getText() == null || et_Message.getText().toString().trim().length() == 0)
                return;
            //   Send Message
            sendMessage(null);
        }
    });
    //   Hide Separator
    lv_Thread.setDividerHeight(0);
    lv_Thread.setDivider(null);
    lv_Thread.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    lv_Thread.setOnItemClickListener(new ListView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
            DisplayBChatThreadItem item = (DisplayBChatThreadItem) m_ThreadAdapter.getItem(position);
            //   Show Image
            String fileName = item.getFileName();
            if (fileName != null && fileName.length() > 0) {
                String m_FilePath = Env.getBC_IMG_DirectoryPathName(m_ctx);
                File file = new File(m_FilePath + File.separator + fileName);
                //   Show
                AttachmentHandler.showAttachment(m_ctx, Uri.fromFile(file));
            }
        }
    });
    //   
    lv_Thread.setMultiChoiceModeListener(new MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            // Capture total checked items
            final int checkedCount = lv_Thread.getCheckedItemCount();
            // Set the CAB title according to total checked items
            mode.setTitle(checkedCount + " " + getString(R.string.BChat_Selected));
            // Calls toggleSelection method from ListViewAdapter Class
            m_ThreadAdapter.toggleSelection(position);

        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.action_delete:
                SparseBooleanArray selectedItems = m_ThreadAdapter.getSelectedItems();
                StringBuffer inClause = new StringBuffer();
                for (int i = (selectedItems.size() - 1); i >= 0; i--) {
                    if (selectedItems.valueAt(i)) {
                        DisplayBChatThreadItem selectedItem = m_ThreadAdapter.getItem(selectedItems.keyAt(i));
                        //   Add Separator
                        if (inClause.length() > 0) {
                            inClause.append(", ");
                        }
                        //   Add Value
                        inClause.append("'").append(selectedItem.getSPS_BC_Message_UUID()).append("'");
                        //   Remove Item
                        m_ThreadAdapter.remove(selectedItem);
                    }
                }
                //   Delete Records in DB
                if (inClause.length() > 0) {
                    BCMessageHandle.getInstance(m_ctx).deleteMessage(m_Request,
                            "SPS_BC_Message_UUID IN(" + inClause.toString() + ")");
                }
                mode.finish();
                return true;
            case R.id.action_copy:
                selectedItems = m_ThreadAdapter.getSelectedItems();
                boolean justOne = selectedItems.size() == 1;
                StringBuffer text = new StringBuffer();
                for (int i = (selectedItems.size() - 1); i >= 0; i--) {
                    if (selectedItems.valueAt(i)) {
                        DisplayBChatThreadItem selectedItem = m_ThreadAdapter.getItem(selectedItems.keyAt(i));
                        //   Valid File
                        if (selectedItem.getFileName() != null)
                            continue;
                        //   Add New Line
                        if (text.length() > 0) {
                            text.append(Env.NL);
                        }
                        //   Add to Text
                        if (justOne) {
                            text.append(selectedItem.getText());
                        } else {
                            text.append(selectedItem.getCopy());
                        }
                    }
                }
                //   Add To Clipboard
                if (text.length() > 0) {
                    Env.setClipboardText(m_ctx, text.toString());
                }
                mode.finish();
                return true;
            default:
                return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.bc_thread_selected, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            m_ThreadAdapter.removeSelection();
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

    });
    //   Return
    return m_view;
}

From source file:org.dvbviewer.controller.ui.fragments.RecordingList.java

@Override
public void onClick(DialogInterface dialog, int which) {
    switch (which) {
    case DialogInterface.BUTTON_POSITIVE:
        Log.i(RecordingList.class.getSimpleName(), "onClick");
        SparseBooleanArray checkedPositions = getListView().getCheckedItemPositions();
        Log.i(RecordingList.class.getSimpleName(), "items selected: " + checkedPositions.size());
        if (checkedPositions != null && checkedPositions.size() > 0) {

            int size = checkedPositions.size();
            RecordingDeleter deleter = new RecordingDeleter(this);
            List<Recording> recordings = new ArrayList<Recording>();
            for (int i = 0; i < size; i++) {
                if (checkedPositions.valueAt(i)) {
                    recordings.add(mAdapter.getItem(checkedPositions.keyAt(i)));
                }/*  w w w . ja  v  a 2 s. c om*/
            }
            Recording[] array = new Recording[recordings.size()];
            deleter.execute(recordings.toArray(array));
        }
        mode.finish();
        break;

    case DialogInterface.BUTTON_NEGATIVE:
        // No button clicked
        break;
    }
}