Example usage for android.widget ListView setItemChecked

List of usage examples for android.widget ListView setItemChecked

Introduction

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

Prototype

public void setItemChecked(int position, boolean value) 

Source Link

Document

Sets the checked state of the specified position.

Usage

From source file:org.gnucash.android.ui.account.AccountsListFragment.java

@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
    super.onListItemClick(listView, view, position, id);
    if (mInEditMode) {
        mSelectedItemId = id;//  w  ww . j a  v  a  2  s  . c  o m
        listView.setItemChecked(position, true);
        return;
    }
    mAccountSelectedListener.accountSelected(id);
}

From source file:com.simas.vc.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    ListView lv = mNavDrawerFragment.getListView();

    if (position == ListView.INVALID_POSITION || position < lv.getHeaderViewsCount()
            || position >= lv.getCount() - lv.getFooterViewsCount()) {
        // Skip headers and footers
        //noinspection UnnecessaryReturnStatement
        return;//from  ww w .jav  a 2 s.com
    } else {
        // Check the item in the drawer
        lv.setItemChecked(position, true);

        Object checkedItem = lv.getItemAtPosition(lv.getCheckedItemPosition());
        // Make sure the item is not already selected in the current EditorFragment
        if (lv.getChoiceMode() == ListView.CHOICE_MODE_SINGLE && getEditorFragment() != null
                && getEditorFragment().getItem() != checkedItem) {
            // Select item in pager
            int itemPosInPager = position - lv.getHeaderViewsCount() + 1; // +1 for empty item
            mViewPager.setCurrentItem(itemPosInPager);

            // Close drawer
            mNavDrawerFragment.setDrawerOpen(false);
        }
    }
}

From source file:com.ultramegasoft.flavordex2.fragment.EntryListFragment.java

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
    switch (loader.getId()) {
    case LOADER_ENTRIES:
        if (mExportMode) {
            final ListView listView = getListView();
            final long[] checkedItems = listView.getCheckedItemIds();
            for (int i = 0; i < mAdapter.getCount(); i++) {
                listView.setItemChecked(i, false);
            }//w  w w .jav a  2 s . c o  m

            mAdapter.swapCursor(data);
            int pos;
            for (long checked : checkedItems) {
                pos = mAdapter.getItemIndex(checked);
                if (pos != ListView.INVALID_POSITION) {
                    listView.setItemChecked(pos, true);
                }
            }

            invalidateExportMenu();
        } else {
            mAdapter.swapCursor(data);
            setActivatedPosition(mAdapter.getItemIndex(mActivatedItem));
        }
        setListShown(true);
        break;
    case LOADER_CAT:
        if (data.moveToFirst()) {
            setCatName(data.getString(data.getColumnIndex(Tables.Cats.NAME)));
        } else {
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    final FragmentManager fm = getFragmentManager();
                    if (fm != null) {
                        fm.popBackStack();
                    }
                }
            });
        }
        break;
    }
}

From source file:org.opendatakit.survey.android.fragments.InstanceUploaderListFragment.java

@Override
public void onResume() {
    super.onResume();

    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    getListView().setItemsCanFocus(false);

    // if current activity is being reinitialized due to changing
    // orientation restore all checkmarks for ones selected
    ListView ls = getListView();
    for (String id : mSelected) {
        for (int pos = 0; pos < ls.getCount(); pos++) {
            Cursor c = (Cursor) ls.getItemAtPosition(pos);
            String uuid = ODKDatabaseUtils.get().getIndexAsString(c, c.getColumnIndex(InstanceColumns._ID));
            if (id.equals(uuid)) {
                ls.setItemChecked(pos, true);
                break;
            }//from w w w.j ava 2 s .  c o  m
        }

    }

    FragmentManager mgr = getFragmentManager();
    BackgroundTaskFragment f = (BackgroundTaskFragment) mgr.findFragmentByTag("background");

    f.establishInstanceUploaderListener(this);

    if (mDialogState == DialogState.Progress) {
        restoreProgressDialog();
    } else if (mDialogState == DialogState.Alert) {
        restoreAlertDialog();
    }
}

From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuActivity.java

/**
 * Called when Select File was pressed//from ww  w  .j  av  a2s.  co m
 * 
 * @param view
 *            a button that was pressed
 */
public void onSelectFileClicked(final View view) {
    final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/octet-stream");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        // file browser has been found on the device
        startActivityForResult(intent, SELECT_FILE_REQ);
    } else {
        // there is no any file browser app, let's try to download one
        final View customView = getLayoutInflater().inflate(R.layout.app_file_browser, null);
        final ListView appsList = (ListView) customView.findViewById(android.R.id.list);
        appsList.setAdapter(new FileBrowserAppsAdapter(this));
        appsList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        appsList.setItemChecked(0, true);
        new AlertDialog.Builder(this).setTitle(R.string.dfu_alert_no_filebrowser_title).setView(customView)
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, final int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, final int which) {
                        final int pos = appsList.getCheckedItemPosition();
                        if (pos >= 0) {
                            final String query = getResources()
                                    .getStringArray(R.array.dfu_app_file_browser_action)[pos];
                            final Intent storeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(query));
                            startActivity(storeIntent);
                        }
                    }
                }).show();
    }
}

From source file:co.juliansuarez.libwizardpager.wizard.ui.MultipleChoiceFragment.java

/**
 * Must be called from a/*  w  w w .  j  ava  2 s  .  c om*/
 * ViewPager.SimpleOnPageChangeListener.onPageSelected() to treat Choices
 * that are allowed to be checked based on selected choices from a previous
 * page. Calling code example:
 * 
 * <pre>
 * {@code
 * if (position >= mWizardModel.getCurrentPageSequence().size()) return;
 *                         
 * FragmentStatePagerAdapter a = (FragmentStatePagerAdapter) mPager.getAdapter();
 * final Fragment fragment = (Fragment) a.instantiateItem(mPager, position);
 * if (fragment instanceof MultipleChoiceFragment) {
 *    MultipleChoiceFragment f = (MultipleChoiceFragment) fragment;
 *    f.setRequiredChoices();
 * }
 * }
 * </pre>
 */
public void setRequiredChoices() {
    // View v = mPager.getChildAt(position);
    final ListView listView = this.getListView(); // (ListView)v.findViewById(android.R.id.list);

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < listView.getChildCount(); i++) {
                View v = listView.getChildAt(i);
                CheckedTextView c = (CheckedTextView) v.findViewById(android.R.id.text1);

                c.setClickable(false);
                c.setFocusable(false);

                Object dependentView = c.getTag();
                if (dependentView != null && (Boolean) dependentView) {
                    c.setTextColor(getResources().getColor(android.R.color.black));
                    listView.setItemChecked(i, false);
                    c.setTag(false);

                    MultipleChoiceFragment.this.onListItemClick(listView, v, i, -1);
                }

            }

            // Set mandatory items to selected and disabled
            Set<String> mandatorySet = new HashSet<String>();

            Page previousPage = mPage.getPreviousPage();
            if (previousPage != null) {
                ArrayList<Choice> selectedItems = previousPage.getData()
                        .getParcelableArrayList(Page.CHOICE_DATA_KEY);

                if (selectedItems == null || selectedItems.size() == 0) {
                    return;
                }

                for (Choice c : selectedItems) {
                    if (c.getMandatoryNextPageChoiceString() != null) {
                        mandatorySet.add(c.getMandatoryNextPageChoiceString());
                    }
                }

                for (int i = 0; i < listView.getChildCount(); i++) {

                    Log.d("NewSimpleRule", (String) listView.getItemAtPosition(i));
                    View v = listView.getChildAt(i);
                    CheckedTextView c = (CheckedTextView) v.findViewById(android.R.id.text1);

                    if (mandatorySet.contains(c.getText().toString())) {
                        listView.setItemChecked(i, true);
                        c.setTag(true);
                        c.setClickable(true);
                        c.setFocusable(true);
                        c.setTextColor(getResources().getColor(android.R.color.darker_gray));

                        MultipleChoiceFragment.this.onListItemClick(listView, v, i, -1);
                    }
                }

            }
        }
    });

}

From source file:com.poloure.simplerss.Utilities.java

static void setTitlesAndDrawerAndPage(Fragment fragment, int absolutePos) {
    String[] navTitles = s_resources.getStringArray(R.array.navigation_titles);

    if (null != fragment) {
        switchToFragment(fragment, false);
    }//from  ww w  .j ava  2 s  . c  om

    ListView list = s_fragmentDrawer.m_listView;
    HeaderViewListAdapter headerAdapter = (HeaderViewListAdapter) list.getAdapter();
    int headers = headerAdapter.getHeadersCount();

    int listPosition = -10 == absolutePos ? s_viewPager.getCurrentItem() + headers : absolutePos;
    int viewPagerPos = -10 == absolutePos ? s_viewPager.getCurrentItem() : absolutePos - headers;

    // Check the drawer item.
    String title = PagerAdapterTags.s_tagList.get(0);
    String subTitle = null;
    int imageRes = R.drawable.ic_action_labels;

    if (s_fragmentFavourites.isVisible()) {
        listPosition = 0;
        title = navTitles[0];
        imageRes = R.drawable.ic_action_important;
    } else if (s_fragmentManage.isVisible()) {
        listPosition = 1;
        title = navTitles[1];
        imageRes = R.drawable.ic_action_storage;
    } else if (s_fragmentSettings.isVisible()) {
        listPosition = 2;
        title = navTitles[2];
        imageRes = R.drawable.ic_action_settings;
    } else {
        ArrayAdapter<String[]> adapter = (ArrayAdapter<String[]>) headerAdapter.getWrappedAdapter();

        if (null != adapter && 0 < adapter.getCount()) {
            String[] item = adapter.getItem(viewPagerPos);
            title = item[0];
            int count = null == item[1] || item[1].isEmpty() ? 0 : Integer.parseInt(item[1]);
            String countString = s_resources.getQuantityString(R.plurals.actionbar_subtitle_unread, count,
                    count);
            subTitle = 0 == count ? null : countString;
        }
    }
    s_actionBar.setTitle(title);
    s_actionBar.setSubtitle(subTitle);
    s_actionBar.setIcon(imageRes);

    list.setItemChecked(listPosition, true);

    // If we must change the view pager page.
    if (0 <= viewPagerPos) {
        // Switch the view pager page if different.
        if (s_viewPager.getCurrentItem() != viewPagerPos) {
            s_viewPager.setCurrentItem(viewPagerPos);
        }
    }
}

From source file:com.Candy.sizer.CandySizer.java

private void showDialog(int id, final String item, final ArrayAdapter<String> adapter, int itemCounter) {
    // startup dialog
    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

    if (id == STARTUP_DIALOG) {
        // create warning dialog
        alert.setMessage(R.string.sizer_message_startup).setTitle(R.string.caution).setCancelable(true)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for ok
                        dialog.cancel();
                    }//from   ww w.j  a  va  2 s .c  o m
                });
        // delete dialog
    } else if (id == DELETE_DIALOG) {
        alert.setMessage(R.string.sizer_message_delete)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for ok
                        // call delete
                        new CandySizer.SlimDeleter().execute(item);
                        // remove list entry
                        adapter.remove(item);
                        adapter.notifyDataSetChanged();
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for cancel
                        dialog.cancel();
                    }
                });
    } else if (id == DELETE_MULTIPLE_DIALOG) {
        String message;
        if (itemCounter == 1) {
            message = getResources().getString(R.string.sizer_message_delete_multi_one);
        } else {
            message = getResources().getString(R.string.sizer_message_delete_multi);
        }
        alert.setMessage(message).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                final ListView lv = (ListView) getView().findViewById(R.string.listsystem);
                ArrayList<String> itemsList = new ArrayList<String>();
                SparseBooleanArray checked = lv.getCheckedItemPositions();
                for (int i = lv.getCount() - 1; i > 0; i--) {
                    if (checked.get(i)) {
                        String appName = mSysApp.get(i);
                        itemsList.add(appName);
                        // remove list entry
                        lv.setItemChecked(i, false);
                        adapter.remove(appName);
                    }
                }
                adapter.notifyDataSetChanged();
                new CandySizer.SlimDeleter().execute(itemsList.toArray(new String[itemsList.size()]));
            }
        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // action for cancel
                dialog.cancel();
            }
        });
    } else if (id == REBOOT_DIALOG) {
        // create warning dialog
        alert.setMessage(R.string.reboot).setTitle(R.string.caution).setCancelable(true)
                .setPositiveButton(R.string.reboot_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for ok
                        try {
                            dos.writeBytes("reboot");
                            dos.flush();
                            dos.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).setNegativeButton(R.string.reboot_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // action for cancel
                        dialog.cancel();
                    }
                });
    }
    // show warning dialog
    alert.show();
}

From source file:net.i2p.android.wizard.ui.MultipleChoiceFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_wizard_page, container, false);
    ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle());

    final ListView listView = (ListView) rootView.findViewById(android.R.id.list);
    setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice,
            android.R.id.text1, mChoices));
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    // Pre-select currently selected items.
    new Handler().post(new Runnable() {
        public void run() {
            ArrayList<String> selectedItems = mPage.getData().getStringArrayList(Page.SIMPLE_DATA_KEY);
            if (selectedItems == null || selectedItems.size() == 0) {
                return;
            }//from w w  w  .  j ava 2  s .  c  o  m

            Set<String> selectedSet = new HashSet<String>(selectedItems);

            for (int i = 0; i < mChoices.size(); i++) {
                if (selectedSet.contains(mChoices.get(i))) {
                    listView.setItemChecked(i, true);
                }
            }
        }
    });

    return rootView;
}

From source file:com.ultramegasoft.flavordex2.fragment.EntryListFragment.java

/**
 * Show or hide the export Toolbar.//  ww  w  .  j a  v  a  2s .c om
 *
 * @param show Whether to show the export Toolbar
 */
private void showExportToolbar(boolean show, boolean animate) {
    final Activity activity = getActivity();
    if (activity != null && mExportToolbar == null) {
        mExportToolbar = activity.findViewById(R.id.export_toolbar);
        mExportToolbar.inflateMenu(R.menu.export_menu);
        mExportToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.menu_export_selected:
                    final FragmentManager fm = getFragmentManager();
                    if (fm != null) {
                        ExportDialog.showDialog(fm, getListView().getCheckedItemIds());
                        setExportMode(false, true);
                    }
                    return true;
                case R.id.menu_cancel:
                    setExportMode(false, true);
                    return true;
                case R.id.menu_check_all:
                case R.id.menu_uncheck_all:
                    final ListView listView = getListView();
                    final boolean check = item.getItemId() == R.id.menu_check_all;
                    for (int i = 0; i < mAdapter.getCount(); i++) {
                        listView.setItemChecked(i, check);
                    }
                    invalidateExportMenu();
                    return true;
                }
                return false;
            }
        });

        mExportInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.toolbar_slide_in_bottom);
        mExportOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.toolbar_slide_out_bottom);
    }

    invalidateExportMenu();

    mExportToolbar.setVisibility(show ? View.VISIBLE : View.GONE);
    if (animate) {
        mExportToolbar.startAnimation(show ? mExportInAnimation : mExportOutAnimation);
    }
}