Example usage for android.widget Spinner setSelection

List of usage examples for android.widget Spinner setSelection

Introduction

In this page you can find the example usage for android.widget Spinner setSelection.

Prototype

public void setSelection(int position, boolean animate) 

Source Link

Document

Jump directly to a specific item in the adapter data.

Usage

From source file:ca.farrelltonsolar.classic.HourLogChart.java

private void setupSpinner(MenuItem item) {
    try {/*from  ww w .ja v a  2 s  . c  o  m*/
        item.setVisible(true);
        item.setActionView(R.layout.action_chart_select);
        View view = MenuItemCompat.getActionView(item);
        if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;
            String[] itemArray = getResources().getStringArray(R.array.minute_log_chart_selection);
            ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_spinner_item, itemArray); //selected item will look like a spinner set from XML
            spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(spinnerArrayAdapter);
            spinner.setSelection(MonitorApplication.chargeControllers().getCurrentChargeController()
                    .getHourLogMenuSelection(), false);
            spinner.setOnItemSelectedListener(
                    new OnItemSelectedListenerWrapper(new AdapterView.OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            chartView.clearSeries();
                            if (position < mSeries.size()) {
                                MonitorApplication.chargeControllers().getCurrentChargeController()
                                        .setHourLogMenuSelection(position);
                                chartView.addSeries(mSeries.get(position));
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    }));
        }
    } catch (Exception ex) {
        Log.w(getClass().getName(), String.format("Hour Log Chart failed to load setupSpinner %s ex: %s",
                Thread.currentThread().getName(), ex));
    }
}

From source file:ca.farrelltonsolar.classic.DayLogChart.java

private void setupSpinner(MenuItem item) {
    try {//from   w w  w . ja  v a2s . c o  m
        item.setVisible(true);
        item.setActionView(R.layout.action_chart_select);
        View view = MenuItemCompat.getActionView(item);
        if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;
            String[] itemArray = getResources().getStringArray(R.array.day_log_chart_selection);
            ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_spinner_item, itemArray); //selected item will look like a spinner set from XML
            spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(spinnerArrayAdapter);
            spinner.setSelection(MonitorApplication.chargeControllers().getCurrentChargeController()
                    .getDayLogMenuSelection(), false);
            spinner.setOnItemSelectedListener(
                    new OnItemSelectedListenerWrapper(new AdapterView.OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            chartView.clearSeries();
                            if (position < mSeries.size()) {
                                MonitorApplication.chargeControllers().getCurrentChargeController()
                                        .setDayLogMenuSelection(position);
                                chartView.addSeries(mSeries.get(position));
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    }));
        }
    } catch (Exception ex) {
        Log.w(getClass().getName(), String.format("Day Log Chart failed to load setupSpinner %s ex: %s",
                Thread.currentThread().getName(), ex));
    }
}

From source file:com.cachirulop.moneybox.activity.MovementDetailActivity.java

/**
 * Load the movements data to the window fields.
 *//*from w w w. j a  va2  s  .c om*/
private void initData() {
    TextView txt;
    Spinner amount;
    int pos;

    _movement = (Movement) getIntent().getExtras().getSerializable("movement");

    updateDateAndTime();

    amount = (Spinner) findViewById(R.id.sAmount);
    pos = ((CurrencySpinnerAdapter) amount.getAdapter()).getItemPositionByAmount(_movement.getAmount());
    amount.setSelection(pos, true);

    txt = (TextView) findViewById(R.id.txtDescription);
    txt.setText(_movement.getDescription());
}

From source file:it.polimi.spf.app.fragments.profile.ProfileFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem personaSelector = menu.findItem(R.id.profileview_persona_selector);
    if (personaSelector == null) {
        return;//www .  ja v  a 2  s  . c  o  m
    }

    Spinner spinner = (Spinner) personaSelector.getActionView().findViewById(R.id.profileview_persona_spinner);
    if (spinner == null) {
        return;
    }

    List<SPFPersona> personas = SPF.get().getProfileManager().getAvailablePersonas();
    spinner.setAdapter(new ArrayAdapter<SPFPersona>(getActivity().getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_1, personas));
    spinner.setSelection(personas.indexOf(mCurrentPersona), false);
    spinner.setOnItemSelectedListener(this);

}

From source file:com.example.linhdq.test.documents.creation.visualisation.LayoutQuestionDialog.java

@NonNull
@Override//from  w  w  w . j  a  v  a2s.  c  om
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Context context = getContext();
    mLayout = null;
    Pair<String, String> language = PreferencesUtils.getOCRLanguage(context);

    final InstallStatus installStatus = OcrLanguageDataStore.isLanguageInstalled(language.first, context);

    if (!installStatus.isInstalled()) {
        final String defaultLanguage = context.getString(R.string.default_ocr_language);
        final String defaultLanguageDisplay = context.getString(R.string.default_ocr_display_language);
        language = Pair.create(defaultLanguage, defaultLanguageDisplay);
    }
    mLanguage = language.first;

    AlertDialog.Builder builder;

    builder = new AlertDialog.Builder(context);
    builder.setCancelable(false);
    View layout = View.inflate(context, R.layout.dialog_layout_question, null);
    builder.setView(layout);

    final ViewFlipper titleViewFlipper = (ViewFlipper) layout.findViewById(R.id.layout_title);
    final ImageView columnLayout = (ImageView) layout.findViewById(R.id.column_layout);
    final ImageView pageLayout = (ImageView) layout.findViewById(R.id.page_layout);
    final ImageSwitcher fairy = (ImageSwitcher) layout.findViewById(R.id.fairy_layout);
    fairy.setFactory(new ViewSwitcher.ViewFactory() {
        public View makeView() {
            return new ImageView(context);
        }
    });
    fairy.setImageResource(R.drawable.fairy_looks_center);
    fairy.setInAnimation(context, android.R.anim.fade_in);
    fairy.setOutAnimation(context, android.R.anim.fade_out);

    final int color = context.getResources().getColor(R.color.progress_color);

    final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.LIGHTEN);

    columnLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mLayout != LayoutKind.COMPLEX) {
                fairy.setImageResource(R.drawable.fairy_looks_left);
                titleViewFlipper.setDisplayedChild(2);
                columnLayout.setColorFilter(colorFilter);
                pageLayout.clearColorFilter();
                mLayout = LayoutKind.COMPLEX;
            }

        }
    });
    pageLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mLayout != LayoutKind.SIMPLE) {
                mLayout = LayoutKind.SIMPLE;
                titleViewFlipper.setDisplayedChild(1);
                fairy.setImageResource(R.drawable.fairy_looks_right);
                pageLayout.setColorFilter(colorFilter);
                columnLayout.clearColorFilter();
            }
        }
    });

    final Spinner langButton = (Spinner) layout.findViewById(R.id.button_language);
    List<OcrLanguage> installedLanguages = OcrLanguageDataStore.getInstalledOCRLanguages(context);

    // actual values uses by tesseract
    final ArrayAdapter<OcrLanguage> adapter = new ArrayAdapter<>(context, R.layout.item_spinner_language,
            installedLanguages);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    langButton.setAdapter(adapter);
    for (int i = 0; i < installedLanguages.size(); i++) {
        OcrLanguage lang = installedLanguages.get(i);
        if (lang.getValue().equals(language.first)) {
            langButton.setSelection(i, false);
            break;
        }
    }
    langButton.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            final OcrLanguage item = adapter.getItem(position);
            mLanguage = item.getValue();
            PreferencesUtils.saveOCRLanguage(context, item);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    builder.setPositiveButton(R.string.start_scan, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            if (mLayout == null) {
                mLayout = LayoutKind.SIMPLE;
            }
            LayoutChoseListener listener = (LayoutChoseListener) getActivity();
            listener.onLayoutChosen(mLayout, mLanguage);
            dialog.dismiss();
        }
    });

    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            getActivity().finish();
            dialog.dismiss();
        }
    });

    return builder.create();

}

From source file:com.renard.ocr.documents.creation.visualisation.LayoutQuestionDialog.java

@NonNull
@Override//from   w ww  . ja  va 2 s. c  o  m
public Dialog onCreateDialog(Bundle savedInstanceState) {
    getAnalytics().sendScreenView(SCREEN_NAME);
    final Context context = getContext();
    mLayout = null;
    Pair<String, String> language = PreferencesUtils.getOCRLanguage(context);

    final OcrLanguage.InstallStatus installStatus = OcrLanguageDataStore.isLanguageInstalled(language.first,
            context);

    if (!installStatus.isInstalled()) {
        final String defaultLanguage = context.getString(R.string.default_ocr_language);
        final String defaultLanguageDisplay = context.getString(R.string.default_ocr_display_language);
        language = Pair.create(defaultLanguage, defaultLanguageDisplay);
    }
    mLanguage = language.first;

    AlertDialog.Builder builder;

    builder = new AlertDialog.Builder(context);
    builder.setCancelable(false);
    View layout = View.inflate(context, R.layout.dialog_layout_question, null);
    builder.setView(layout);

    final ViewFlipper titleViewFlipper = (ViewFlipper) layout.findViewById(R.id.layout_title);
    final ImageView columnLayout = (ImageView) layout.findViewById(R.id.column_layout);
    final ImageView pageLayout = (ImageView) layout.findViewById(R.id.page_layout);
    final ImageSwitcher fairy = (ImageSwitcher) layout.findViewById(R.id.fairy_layout);
    fairy.setFactory(new ViewSwitcher.ViewFactory() {
        public View makeView() {
            return new ImageView(context);
        }
    });
    fairy.setImageResource(R.drawable.fairy_looks_center);
    fairy.setInAnimation(context, android.R.anim.fade_in);
    fairy.setOutAnimation(context, android.R.anim.fade_out);

    final int color = context.getResources().getColor(R.color.progress_color);

    final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.LIGHTEN);

    columnLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mLayout != LayoutKind.COMPLEX) {
                fairy.setImageResource(R.drawable.fairy_looks_left);
                titleViewFlipper.setDisplayedChild(2);
                columnLayout.setColorFilter(colorFilter);
                pageLayout.clearColorFilter();
                mLayout = LayoutKind.COMPLEX;
            }

        }
    });
    pageLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mLayout != LayoutKind.SIMPLE) {
                mLayout = LayoutKind.SIMPLE;
                titleViewFlipper.setDisplayedChild(1);
                fairy.setImageResource(R.drawable.fairy_looks_right);
                pageLayout.setColorFilter(colorFilter);
                columnLayout.clearColorFilter();
            }
        }
    });

    final Spinner langButton = (Spinner) layout.findViewById(R.id.button_language);
    List<OcrLanguage> installedLanguages = OcrLanguageDataStore.getInstalledOCRLanguages(context);

    // actual values uses by tesseract
    final ArrayAdapter<OcrLanguage> adapter = new ArrayAdapter<>(context, R.layout.language_spinner_item,
            installedLanguages);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    langButton.setAdapter(adapter);
    for (int i = 0; i < installedLanguages.size(); i++) {
        OcrLanguage lang = installedLanguages.get(i);
        if (lang.getValue().equals(language.first)) {
            langButton.setSelection(i, false);
            break;
        }
    }
    langButton.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            final OcrLanguage item = adapter.getItem(position);
            mLanguage = item.getValue();
            PreferencesUtils.saveOCRLanguage(context, item);
            getAnalytics().sendOcrLanguageChanged(item);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    builder.setPositiveButton(R.string.start_scan, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            if (mLayout == null) {
                mLayout = LayoutKind.SIMPLE;
            }
            LayoutChoseListener listener = (LayoutChoseListener) getActivity();
            listener.onLayoutChosen(mLayout, mLanguage);
            getAnalytics().sendOcrStarted(mLanguage, mLayout);
            dialog.dismiss();
        }
    });

    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            getActivity().finish();
            dialog.dismiss();
            getAnalytics().sendLayoutDialogCancelled();
        }
    });

    return builder.create();

}

From source file:nf.frex.android.FrexActivity.java

private void prepareDecorationsDialog(final Dialog dialog) {
    final Registry<DistanceFunction> distanceFunctions = Registries.distanceFunctions;
    int checkedIndex = distanceFunctions.getIndex(view.getDistanceFunctionId());

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            distanceFunctions.getIds());
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    final Spinner distanceFunctionSpinner = (Spinner) dialog.findViewById(R.id.distance_function_spinner);
    distanceFunctionSpinner.setAdapter(arrayAdapter);
    distanceFunctionSpinner.setSelection(checkedIndex, false);

    final SeekBarConfigurer dilationSeekBarConfigurer = SeekBarConfigurer.create(
            (SeekBar) dialog.findViewById(R.id.dilation_seek_bar), -3.0, 1.0, true, view.getDistanceDilation());
    final SeekBarConfigurer translateXSeekBarConfigurer = SeekBarConfigurer.create(
            (SeekBar) dialog.findViewById(R.id.transl_x_seek_bar), -2.5, 2.5, false,
            view.getDistanceTranslateX());
    final SeekBarConfigurer translateYSeekBarConfigurer = SeekBarConfigurer.create(
            (SeekBar) dialog.findViewById(R.id.transl_y_seek_bar), -2.5, 2.5, false,
            view.getDistanceTranslateY());

    final CheckBox turbulenceCheckBox = (CheckBox) dialog.findViewById(R.id.turbulence);
    turbulenceCheckBox.setChecked(view.getGeneratorConfig().isTurbulenceEnabled());

    final SeekBarConfigurer turbIntensSeekBarConfigurer = SeekBarConfigurer.create(
            (SeekBar) dialog.findViewById(R.id.turb_intens_seek_bar), -1.0, 1.0, true,
            view.getGeneratorConfig().getTurbulenceIntensity());
    final SeekBarConfigurer turbScaleSeekBarConfigurer = SeekBarConfigurer.create(
            (SeekBar) dialog.findViewById(R.id.turb_scale_seek_bar), -1.0, 1.0, true,
            view.getGeneratorConfig().getTurbulenceScale());

    Button randomButton = (Button) dialog.findViewById(R.id.random_button);
    randomButton.setOnClickListener(new View.OnClickListener() {
        @Override/*  www  .  ja  v a 2  s  . c  o  m*/
        public void onClick(View v) {
            distanceFunctionSpinner.setSelection((int) (Math.random() * distanceFunctions.getSize()));
            dilationSeekBarConfigurer.setRandomValue();
            translateXSeekBarConfigurer.setRandomValue();
            translateYSeekBarConfigurer.setRandomValue();
            if (Math.random() < 0.5) {
                turbIntensSeekBarConfigurer.setRandomValue();
                turbScaleSeekBarConfigurer.setRandomValue();
            }
        }
    });

    Button okButton = (Button) dialog.findViewById(R.id.ok_button);
    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = distanceFunctionSpinner.getSelectedItemPosition();
            dialog.dismiss();
            view.setDecoratedFractal(true);
            view.setDistanceFunctionId(distanceFunctions.getId(index));
            view.setDistanceDilation(dilationSeekBarConfigurer.getValue());
            view.setDistanceTranslateX(translateXSeekBarConfigurer.getValue());
            view.setDistanceTranslateY(translateYSeekBarConfigurer.getValue());
            view.getGeneratorConfig().setTurbulenceEnabled(turbulenceCheckBox.isChecked());
            view.getGeneratorConfig().setTurbulenceIntensity(turbIntensSeekBarConfigurer.getValue());
            view.getGeneratorConfig().setTurbulenceScale(turbScaleSeekBarConfigurer.getValue());
            view.recomputeAll();
        }
    });
    Button cancelButton = (Button) dialog.findViewById(R.id.cancel_button);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
}

From source file:nf.frex.android.FrexActivity.java

private void prepareColorsDialog(final Dialog dialog) {

    //Log.d(TAG, "prepareColorsDialog() entered");

    getColorSchemes();/*from   w  w w . ja va  2  s .  c o  m*/

    //Log.d(TAG, "prepareColorsDialog: new bitmaps are being created");
    Bitmap[] colorSchemeIcons = new Bitmap[colorSchemes.getSize()];
    for (int i = 0; i < colorSchemeIcons.length; i++) {
        ColorScheme colorScheme = colorSchemes.getValue(i);
        colorSchemeIcons[i] = colorScheme.getGradientIcon();
    }

    int checkedIndex = Registries.colorSchemes.getIndex(view.getColorSchemeId());
    //Log.d(TAG, "prepareColorsDialog: checkedIndex = " + checkedIndex);
    final Spinner colorTableSpinner = (Spinner) dialog.findViewById(R.id.color_table_spinner);
    colorTableSpinner.setAdapter(new ImageArrayAdapter(this, 0, colorSchemeIcons));
    colorTableSpinner.setSelection(checkedIndex, false);
    colorTableSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View spinnerView, int position, long id) {
            view.setColorSchemeId(Registries.colorSchemes.getId(position));
            view.setColorScheme(Registries.colorSchemes.getValue(position));
            view.recomputeColors();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    final SeekBar colorFactorSeekBar = (SeekBar) dialog.findViewById(R.id.color_gain_seek_bar);
    final double colorFactorMin = -3.0;
    final double colorFactorMax = 2.0;
    final SeekBarConfigurer colorFactorSeekBarConfigurer = SeekBarConfigurer.create(colorFactorSeekBar,
            colorFactorMin, colorFactorMax, true, view.getColorGain());
    colorFactorSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            view.setColorGain((float) colorFactorSeekBarConfigurer.getValue());
            view.recomputeColors();
        }
    });

    final SeekBar colorBiasSeekBar = (SeekBar) dialog.findViewById(R.id.color_offset_seek_bar);
    final double colorBiasMin = 0;
    final double colorBiasMax = 1024;
    final SeekBarConfigurer colorBiasSeekBarConfigurer = SeekBarConfigurer.create(colorBiasSeekBar,
            colorBiasMin, colorBiasMax, false, view.getColorOffset());
    colorBiasSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            view.setColorOffset((float) colorBiasSeekBarConfigurer.getValue());
            view.recomputeColors();
        }
    });

    final CheckBox colorRepeatCheckBox = (CheckBox) dialog.findViewById(R.id.color_repeat);
    colorRepeatCheckBox.setChecked(view.isColorRepeat());
    colorRepeatCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            view.setColorRepeat(isChecked);
            view.recomputeColors();
        }
    });

    Button randomButton = (Button) dialog.findViewById(R.id.random_button);
    randomButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            colorTableSpinner.setSelection((int) (Math.random() * colorSchemes.getSize()));
            colorFactorSeekBarConfigurer.setRandomValue();
            colorBiasSeekBarConfigurer.setRandomValue();
            view.recomputeColors();
        }
    });

    Button getPalFromImgButton = (Button) dialog.findViewById(R.id.get_pal_from_img_button);
    getPalFromImgButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            dialog.dismiss();
            startActivityForResult(photoPickerIntent, SELECT_PICTURE_REQUEST_CODE);
        }
    });

    //Log.d(TAG, "prepareColorsDialog() exited");
}

From source file:gr.scify.newsum.ui.SearchViewActivity.java

@Override
public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
    showWaitingDialog();// w  w w  .j  a v  a2s.  co m
    new Thread(new Runnable() {

        @Override
        public void run() {

            String[] saTopicIDs = SearchTopicActivity.saTopicIDs;
            final Spinner spinner = (Spinner) findViewById(R.id.spinner1);
            ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
            for (Prediction prediction : predictions) {
                if (prediction.score > 1.0) {
                    // track the swipe Action on searched topic
                    if (getAnalyticsPref()) {
                        EasyTracker.getTracker().sendEvent(SWIPING_ACTION, "Search View", "", 0l);
                    }
                    if (prediction.name.contains("right")) {
                        if (spinner.getSelectedItemPosition() > 0) {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    spinner.setSelection(spinner.getSelectedItemPosition() - 1, true);

                                };
                            });

                        }

                    } else {

                        if (spinner.getSelectedItemPosition() < saTopicIDs.length - 1) {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    spinner.setSelection(spinner.getSelectedItemPosition() + 1, true);
                                };
                            });

                        }
                    }
                }
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    pd.dismiss();

                }
            });
        }
    }).start();
}

From source file:com.money.manager.ex.common.CategoryListFragment.java

/**
 * Show alter binaryDialog, for create or edit new category
 *//*from  ww w . j av  a2  s .co  m*/
private void showDialogEditSubCategoryName(final SQLTypeTransaction type, final int categoryId,
        final int subCategoryId, final CharSequence subCategName) {

    // inflate view
    View viewDialog = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_new_edit_subcategory, null);

    final EditText edtSubCategName = (EditText) viewDialog.findViewById(R.id.editTextCategName);
    final Spinner spnCategory = (Spinner) viewDialog.findViewById(R.id.spinnerCategory);
    // set category description
    edtSubCategName.setText(subCategName);
    if (!TextUtils.isEmpty(subCategName)) {
        edtSubCategName.setSelection(subCategName.length());
    }

    // Fill categories list.
    CategoryService categoryService = new CategoryService(getActivity());
    final List<Category> categories = categoryService.getList();

    ArrayList<String> categoryNames = new ArrayList<>();
    ArrayList<Integer> categoryIds = new ArrayList<>();
    for (Category category : categories) {
        categoryIds.add(category.getId());
        categoryNames.add(category.getName().toString());
    }
    ArrayAdapter<String> adapterCategory = new ArrayAdapter<>(getActivity(),
            android.R.layout.simple_spinner_item, categoryNames);
    adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnCategory.setAdapter(adapterCategory);
    //select category if present
    if (categoryId > 0) {
        spnCategory.setSelection(categoryIds.indexOf(categoryId), true);
    }

    int titleId = type.equals(SQLTypeTransaction.INSERT) ? R.string.add_subcategory
            : R.string.edit_categoryName;

    new MaterialDialog.Builder(getContext()).customView(viewDialog, true)
            .icon(FontIconDrawable.inflate(getContext(), R.xml.ic_tag)).title(titleId)
            .positiveText(android.R.string.ok).onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    // get category description
                    String name = edtSubCategName.getText().toString();
                    // check position
                    if (spnCategory.getSelectedItemPosition() == Spinner.INVALID_POSITION)
                        return;
                    // get category id
                    int categId = categories.get(spnCategory.getSelectedItemPosition()).getId();
                    ContentValues values = new ContentValues();
                    values.put(Subcategory.CATEGID, categId);
                    values.put(Subcategory.SUBCATEGNAME, name);

                    SubcategoryRepository repo = new SubcategoryRepository(getActivity());

                    // check type transaction is request
                    switch (type) {
                    case INSERT:
                        if (getActivity().getContentResolver().insert(repo.getUri(), values) == null) {
                            Toast.makeText(getActivity(), R.string.db_insert_failed, Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case UPDATE:
                        if (getActivity().getContentResolver().update(repo.getUri(), values, Subcategory.CATEGID
                                + "=" + categoryId + " AND " + Subcategory.SUBCATEGID + "=" + subCategoryId,
                                null) == 0) {
                            Toast.makeText(getActivity(), R.string.db_update_failed, Toast.LENGTH_SHORT).show();
                        }
                        break;
                    }
                    // restart loader
                    restartLoader();
                }
            }).negativeText(android.R.string.cancel).onNegative(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    dialog.cancel();
                }
            }).build().show();
}