Example usage for android.widget ListView setChoiceMode

List of usage examples for android.widget ListView setChoiceMode

Introduction

In this page you can find the example usage for android.widget ListView setChoiceMode.

Prototype

public void setChoiceMode(int choiceMode) 

Source Link

Document

Defines the choice behavior for the List.

Usage

From source file:com.keithcassidy.finishline.LineCrossingsFragment.java

private void loadList() {
    ListView list = (ListView) getActivity().findViewById(R.id.listViewCrossings);
    TextView noneYet = (TextView) getActivity().findViewById(R.id.textViewNone);
    if (list != null && noneYet != null) {
        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        list.setOnItemClickListener(new OnItemClickListener() {
            @Override/*  w ww. java  2s.  co  m*/
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                getListView().setItemChecked(position, true);

                CheckedTextView c = (CheckedTextView) view.findViewById(R.id.crossing_time);
                if (c != null) {
                    c.setChecked(true);
                }
            }
        });

        ArrayList<Location> crossings = (ArrayList<Location>) dbStorage.getCrossings();
        mAdapter = new CrossingsAdapter(getActivity(), R.layout.crossing_list_item, crossings);
        list.setAdapter(mAdapter);

        if (crossings.isEmpty()) {
            list.setVisibility(ListView.INVISIBLE);
            noneYet.setVisibility(ListView.VISIBLE);
        } else {
            list.setVisibility(ListView.VISIBLE);
            noneYet.setVisibility(ListView.INVISIBLE);
        }

        list.post(new Runnable() {
            @Override
            public void run() {
                //select the last one in the list by default
                getListView().setItemChecked(getListView().getCount() - 1, true);
                getListView().setSelection(getListView().getCount() - 1);
            }
        });
    }
}

From source file:com.github.jvanhie.discogsscrobbler.NowPlayingFragment.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setSelection(final ListView list) {
    list.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
    list.setSelector(R.drawable.track_selector);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override//from  w w w  .  j a  va  2  s  .  c  om
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if (mBound) {
                //fully stop previous playlist since we start again from the beginning of the selected track
                ArrayList<Track> trackList = new ArrayList<Track>(mService.trackList);
                mService.stop();
                mService.trackList = trackList;
                mService.currentTrack = i;
                mService.resume();
            }
        }
    });
    list.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {

        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            MenuInflater inflater = getActivity().getMenuInflater();
            inflater.inflate(R.menu.now_playing_cab, menu);
            return true;
        }

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

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            SparseBooleanArray checkedItems = list.getCheckedItemPositions();
            final List<Integer> positions = new ArrayList<Integer>();
            if (checkedItems != null) {
                for (int i = 0; i < checkedItems.size(); i++) {
                    if (checkedItems.valueAt(i)) {
                        positions.add(checkedItems.keyAt(i));
                    }
                }
            }
            switch (menuItem.getItemId()) {
            case R.id.play_selection:
                if (mBound) {
                    //change tracklist and start over again
                    ArrayList<Track> newTrackList = new ArrayList<Track>();
                    for (Integer i : positions) {
                        newTrackList.add(mService.trackList.get(i));
                    }
                    //fully stop previous playlist since we start again from the beginning
                    mService.stop();
                    mService.trackList = newTrackList;
                    mService.currentTrack = 0;
                    mService.resume();
                }
                actionMode.finish();
                break;
            case R.id.remove_selection:
                int currPlaying = mService.currentTrack;
                int newPlaying = -1;
                //change tracklist and start over again
                ArrayList<Track> newTrackList = new ArrayList<Track>();
                for (int i = 0; i < mService.trackList.size(); i++) {
                    if (!positions.contains(i)) {
                        newTrackList.add(mService.trackList.get(i));
                    }
                    //calculate new currently playing counter
                    if (i == currPlaying) {
                        if (positions.contains(i)) {
                            currPlaying++;
                        } else {
                            newPlaying = newTrackList.size() - 1;
                        }
                    }
                }
                //the currently playing track was removed, start fresh from the next available
                if (currPlaying != mService.currentTrack) {
                    mService.stop();
                    if (newPlaying != -1) {
                        mService.trackList = newTrackList;
                        mService.currentTrack = newPlaying;
                        mService.resume();
                    }
                } else {
                    //pause the playlist and resume with the new tracklist
                    mService.pause();
                    mService.trackList = newTrackList;
                    mService.currentTrack = newPlaying;
                    mService.resume();
                }
                actionMode.finish();
                break;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });
}

From source file:com.nest5.businessClient.AccountsActivity.java

/**
 * Sets up the 'connect' screen content.
 *//*w w w  . ja v a  2s  .  com*/
private void setConnectScreenContent() {
    List<String> accounts = getGoogleAccounts();
    if (accounts.size() == 0) {
        // Show a dialog and invoke the "Add Account" activity if requested
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(R.string.needs_account);
        builder.setPositiveButton(R.string.add_account, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT));
            }
        });
        builder.setNegativeButton(R.string.skip, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.setIcon(android.R.drawable.stat_sys_warning);
        builder.setTitle(R.string.attention);
        builder.show();
    } else {
        final ListView listView = (ListView) findViewById(R.id.select_account);
        listView.setAdapter(new ArrayAdapter<String>(mContext, R.layout.account, accounts));
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setItemChecked(mAccountSelectedPosition, true);

        final Button connectButton = (Button) findViewById(R.id.connect);
        connectButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Set "connecting" status
                SharedPreferences prefs = Util.getSharedPreferences(mContext);
                prefs.edit().putString(Util.CONNECTION_STATUS, Util.CONNECTING).commit();
                // Get account name
                mAccountSelectedPosition = listView.getCheckedItemPosition();
                TextView account = (TextView) listView.getChildAt(mAccountSelectedPosition);
                // Register
                register((String) account.getText());
                finish();
            }
        });
    }

    // internetConnectionStatus = (ImageView) findViewById(R.id.header_connection_status);
    SharedPreferences prefs = Util.getSharedPreferences(mContext);
    if (!isNetworkAvailable()) {
        internetConnectionStatus.setImageResource(R.drawable.error);

        prefs.edit().putInt(Util.INTERNET_CONNECTION, Util.INTERNET_DISCONNECTED).commit();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "No tienes una conexin a internet activa. Habiltala haciendo click en aceptar y seleccionando luego una red.")
                .setCancelable(false).setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                        startActivityForResult(intent, 1);
                    }
                }).setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                }).show();

    } else {
        prefs.edit().putInt(Util.INTERNET_CONNECTION, Util.INTERNET_CONNECTED).commit();
    }
}

From source file:com.csipsimple.ui.calllog.CallLogListFragment.java

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

    // View management
    mDualPane = getResources().getBoolean(R.bool.use_dual_panes);

    // Modify list view
    ListView lv = getListView();
    lv.setVerticalFadingEdgeEnabled(true);
    // lv.setCacheColorHint(android.R.color.transparent);
    if (mDualPane) {
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setItemsCanFocus(false);/*from w w w  . ja v a 2 s. co m*/
    } else {
        lv.setChoiceMode(ListView.CHOICE_MODE_NONE);
        lv.setItemsCanFocus(true);
    }

    // Map long press
    lv.setLongClickable(true);
    lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> ad, View v, int pos, long id) {
            turnOnActionMode();
            getListView().setItemChecked(pos, true);
            mMode.invalidate();
            return true;
        }
    });
}

From source file:net.named_data.nfd.FaceListFragment.java

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

    @SuppressLint("InflateParams")
    View v = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_face_list_list_header, null);
    getListView().addHeaderView(v, null, false);
    getListView().setDivider(getResources().getDrawable(R.drawable.list_item_divider));

    // Get info unavailable view
    m_faceListInfoUnavailableView = v.findViewById(R.id.face_list_info_unavailable);

    // Get progress bar spinner view
    m_reloadingListProgressBar = (ProgressBar) v.findViewById(R.id.face_list_reloading_list_progress_bar);

    // Setup list view for deletion
    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override//from   w w  w  .  ja  va  2  s  .c om
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if (checked && id < 256) {
                getListView().setItemChecked(position, false);
                return;
            }
            if (checked)
                m_facesToDelete.add((int) id);
            else
                m_facesToDelete.remove((int) id);
        }

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

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

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_item_delete_face_item:
                G.Log("Requesting to delete " + String.valueOf(m_facesToDelete));

                // Delete selected faceIds
                m_faceDestroyAsyncTask = new FaceDestroyAsyncTask();
                m_faceDestroyAsyncTask.execute(m_facesToDelete);

                m_facesToDelete = new HashSet<>();
                mode.finish();
                return true;
            default:
                return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
        }

        private HashSet<Integer> m_facesToDelete = new HashSet<>();
    });
}

From source file:at.bitfire.davdroid.syncadapter.SelectCollectionsFragment.java

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

    final ListView listView = getListView();
    listView.setPadding(20, 30, 20, 30);

    View header = getActivity().getLayoutInflater().inflate(R.layout.select_collections_header, null);
    listView.addHeaderView(header);//www . jav  a  2 s .  c  o m

    final ServerInfo serverInfo = (ServerInfo) getArguments().getSerializable(KEY_SERVER_INFO);
    final SelectCollectionsAdapter adapter = new SelectCollectionsAdapter(serverInfo);
    setListAdapter(adapter);

    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int itemPosition = position - 1; // one list header view at pos. 0
            if (adapter.getItemViewType(itemPosition) == SelectCollectionsAdapter.TYPE_ADDRESS_BOOKS_ROW) {
                // unselect all other address books
                for (int pos = 1; pos <= adapter.getNAddressBooks(); pos++)
                    if (pos != itemPosition)
                        listView.setItemChecked(pos + 1, false);
            }

            getActivity().supportInvalidateOptionsMenu();
        }
    });
}

From source file:com.akalipetis.action_mode_list_fragment.ActionModeListFragment.java

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    ListView list = getListView();
    int choiceMode = list.getChoiceMode();
    mLastChoiceMode = choiceMode;//from   w  w  w .j a va  2 s . com
    if (choiceMode == ListView.CHOICE_MODE_SINGLE)
        mLastCheckedItem = list.getCheckedItemPosition();
    else
        mLastCheckedItem = ListView.INVALID_POSITION;
    if (Build.VERSION.SDK_INT < 11) {
        list.setItemChecked(mLastCheckedItem, false);

        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        list.setLongClickable(false);
        ((ActionBarActivity) getActivity()).startSupportActionMode(new InternalOlderListener());
        if (mListener != null)
            mListener.onItemCheckedStateChanged(mWrapper, position, id, true);
    } else {
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    }
    list.setItemChecked(position, true);
    return true;
}

From source file:com.akalipetis.fragment.ActionModeListFragment.java

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    ListView list = getListView();
    int choiceMode = list.getChoiceMode();
    mLastChoiceMode = choiceMode;/*www .  j  a v a2 s.  co m*/
    if (choiceMode == ListView.CHOICE_MODE_SINGLE)
        mLastCheckedItem = list.getCheckedItemPosition();
    else
        mLastCheckedItem = ListView.INVALID_POSITION;
    if (Build.VERSION.SDK_INT < 11) {
        list.setItemChecked(mLastCheckedItem, false);

        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        list.setLongClickable(false);
        ((AppCompatActivity) getActivity()).startSupportActionMode(new InternalOlderListener());
        list.setItemChecked(position, true);
        if (mListener != null)
            mListener.onItemCheckedStateChanged(mWrapper, position, id, true);
    } else {
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        list.setItemChecked(position, true);
    }
    return true;
}

From source file:com.roamprocess1.roaming4world.ui.calllog.CallLogListFragment.java

private void turnOnActionMode() {
    Log.d(THIS_FILE, "Long press");
    // mMode = getSherlockActivity().startActionMode(new CallLogActionMode());
    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

}

From source file:com.myandroidremote.AccountsActivity.java

/**
 * Sets up the 'connect' screen content.
 *///from   w w  w . j a va2s. co m
private void setConnectScreenContent() {
    List<String> accounts = getGoogleAccounts();
    if (accounts.size() == 0) {
        // Show a dialog and invoke the "Add Account" activity if requested
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(R.string.needs_account);
        builder.setPositiveButton(R.string.add_account, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT));
            }
        });
        builder.setNegativeButton(R.string.skip, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                showLoadingScreen();
            }
        });
        builder.setIcon(android.R.drawable.stat_sys_warning);
        builder.setTitle(R.string.attention);
        builder.show();
    } else {
        final ListView listView = (ListView) findViewById(R.id.select_account);
        listView.setAdapter(new ArrayAdapter<String>(mContext, R.layout.account, accounts));
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setItemChecked(mAccountSelectedPosition, true);

        final Button connectButton = (Button) findViewById(R.id.connect);
        connectButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Register in the background and terminate the activity
                mAccountSelectedPosition = listView.getCheckedItemPosition();
                TextView account = (TextView) listView.getChildAt(mAccountSelectedPosition);
                register((String) account.getText());

                // show connecting
                showLoadingScreen();
            }
        });

        final Button exitButton = (Button) findViewById(R.id.exit);
        exitButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }
}