Java Code Examples for android.widget.Spinner#setSelection()
The following examples show how to use
android.widget.Spinner#setSelection() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpinnerActivity.java From codeexamples-android with Eclipse Public License 1.0 | 6 votes |
/** * Restores the current state of the spinner (which item is selected, and * the value of that item). Since onResume() is always called when an * Activity is starting, even if it is re-displaying after being hidden, it * is the best place to restore state. * * Attempts to read the state from a preferences file. If this read fails, * assume it was just installed, so do an initialization. Regardless, change * the state of the spinner to be the previous position. * * @see android.app.Activity#onResume() */ @Override public void onResume() { /* * an override to onResume() must call the super constructor first. */ super.onResume(); /* * Try to read the preferences file. If not found, set the state to the * desired initial values. */ if (!readInstanceState(this)) setInitialState(); /* * Set the spinner to the current state. */ Spinner restoreSpinner = (Spinner) findViewById(R.id.Spinner01); restoreSpinner.setSelection(getSpinnerPosition()); }
Example 2
Source File: TGTimeSignatureDialog.java From tuxguitar with GNU Lesser General Public License v2.1 | 6 votes |
@SuppressLint("InflateParams") public void onPostInflateView() { TGMeasureHeader header = getAttribute(TGDocumentContextAttributes.ATTRIBUTE_HEADER); ArrayAdapter<TGSelectableItem> numeratorAdapter = new ArrayAdapter<TGSelectableItem>(getActivity(), android.R.layout.simple_spinner_item, createNumeratorValues()); numeratorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner numerator = (Spinner) this.getView().findViewById(R.id.time_signature_dlg_ts_numerator_value); numerator.setAdapter(numeratorAdapter); numerator.setSelection(numeratorAdapter.getPosition(new TGSelectableItem(header.getTimeSignature().getNumerator(), null))); ArrayAdapter<TGSelectableItem> denominatorAdapter = new ArrayAdapter<TGSelectableItem>(getActivity(), android.R.layout.simple_spinner_item, createDenominatorValues()); denominatorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner denominator = (Spinner) this.getView().findViewById(R.id.time_signature_dlg_ts_denominator_value); denominator.setAdapter(denominatorAdapter); denominator.setSelection(denominatorAdapter.getPosition(new TGSelectableItem(header.getTimeSignature().getDenominator().getValue(), null))); CheckBox applyToEnd = (CheckBox) this.getView().findViewById(R.id.time_signature_dlg_options_apply_to_end); applyToEnd.setChecked(true); }
Example 3
Source File: EncodingConfigFragment.java From PLDroidMediaStreaming with Apache License 2.0 | 6 votes |
private void initAudioQualityPanel(final View root) { final Spinner presetSpinner = (Spinner) root.findViewById(R.id.audio_quality_presets); ArrayAdapter<String> data = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, AUDIO_QUALITY_PRESETS); presetSpinner.setAdapter(data); presetSpinner.setSelection(3); RadioGroup audioGroup = (RadioGroup) root.findViewById(R.id.audio_quality_radio_group); audioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { View customPanel = root.findViewById(R.id.audio_quality_custom_panel); presetSpinner.setVisibility(checkedId == R.id.audio_quality_preset ? View.VISIBLE : View.GONE); customPanel.setVisibility(checkedId == R.id.audio_quality_preset ? View.GONE : View.VISIBLE); } }); }
Example 4
Source File: SelectElement.java From sana.mobile with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * {@inheritDoc} */ @Override protected View createView(Context c) { View v = ((Activity) c).getLayoutInflater().inflate(R.layout.widget_element_select, null); spin = (Spinner) v.findViewById(R.id.answer); //new Spinner(c); adapter = new ArrayAdapter<String>(c, R.layout.simple_spinner_item, labels); adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item); spin.setAdapter(adapter); int selected = adapter.getPosition(getLabelFromValue(answer)); if (selected != -1) spin.setSelection(selected); return encapsulateQuestion(c, v); }
Example 5
Source File: DNSFormActivity.java From androdns with Apache License 2.0 | 6 votes |
@Override public void onFocusChange(View view, boolean hasFocus) { EditText txtQtype = (((EditText) findViewById(R.id.txtQTYPE))); Spinner spKnownTypes = (Spinner) findViewById(R.id.spinnerKnownTypes); // set spinner qtype from number if (view == txtQtype && !hasFocus) { try { int qtype = gettxtQTYPEContent(); String qnameString = Type.string(qtype); int ind = getIndex(spKnownTypes, qnameString, -1); if (ind > -1) { spKnownTypes.setSelection(ind); } } catch (Exception e) { } } updateBookmarkImageState(); }
Example 6
Source File: SpinnerDialog.java From iSCAU-Android with GNU General Public License v3.0 | 6 votes |
public AlertDialog.Builder createBuilder() { final Spinner mSpinner = new Spinner(mContext); ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_dropdown_item, mList); mSpinner.setAdapter(adapter); mSpinner.setSelection(defaultPosition); setTitle(mText); setView(mSpinner); setNegativeButton(mContext.getString(R.string.btn_cancel), null); setPositiveButton(mContext.getString(R.string.btn_retry), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { int n = mSpinner.getSelectedItemPosition(); dialogListener.select(n); } }); return this; }
Example 7
Source File: ViewMatchersTest.java From android-test with Apache License 2.0 | 6 votes |
@UiThreadTest @Test public void withSpinnerTextString() { Spinner spinner = new Spinner(this.context); List<String> values = Lists.newArrayList(); values.add("Hello World"); values.add("Goodbye!!"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.context, android.R.layout.simple_spinner_item, values); spinner.setAdapter(adapter); spinner.setSelection(0); spinner.setTag("spinner"); assertTrue(withSpinnerText(is("Hello World")).matches(spinner)); assertFalse(withSpinnerText(is("Goodbye!!")).matches(spinner)); assertFalse(withSpinnerText(is("")).matches(spinner)); }
Example 8
Source File: TGHarmonicDialog.java From tuxguitar with GNU Lesser General Public License v2.1 | 5 votes |
public void fillData(int type, int selection) { TGSelectableItem[] selectableItems = createDataValues(type); ArrayAdapter<TGSelectableItem> arrayAdapter = new ArrayAdapter<TGSelectableItem>(getActivity(), android.R.layout.simple_spinner_item, selectableItems); arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner spinner = (Spinner) this.getView().findViewById(R.id.harmonic_dlg_data_value); spinner.setAdapter(arrayAdapter); spinner.setEnabled(selectableItems.length > 0); spinner.setVisibility(selectableItems.length > 0 ? View.VISIBLE : View.GONE); if( selectableItems.length > 0 ) { spinner.setSelection(arrayAdapter.getPosition(new TGSelectableItem(Integer.valueOf(selection), null))); } }
Example 9
Source File: SettingsDialogActivity.java From medic-gateway with GNU Affero General Public License v3.0 | 5 votes |
private void spinnerVal(int componentId, String val) { Spinner spinner = (Spinner) findViewById(componentId); for(int i=spinner.getCount()-1; i>=0; --i) { if(val.equals(spinner.getItemAtPosition(i).toString())) { spinner.setSelection(i); return; } } }
Example 10
Source File: TGTrackChannelDialog.java From tuxguitar with GNU Lesser General Public License v2.1 | 5 votes |
public void fillTrackInstrument() { ArrayAdapter<TGSelectableItem> arrayAdapter = new ArrayAdapter<TGSelectableItem>(getActivity(), android.R.layout.simple_spinner_item, createSelectableChannels()); arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner spinner = (Spinner) this.getView().findViewById(R.id.track_channel_dlg_channel_value); spinner.setAdapter(arrayAdapter); spinner.setSelection(arrayAdapter.getPosition(new TGSelectableItem(this.songManager.getChannel(this.song, this.track.getChannelId()), null))); }
Example 11
Source File: MyElectricSettingsFragment.java From AndroidApp with GNU Affero General Public License v3.0 | 5 votes |
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View result=inflater.inflate(R.layout.page_settings, container, false); powerFeedPreference = (Spinner) result.findViewById(R.id.powerFeedSpinner); kWhFeedPreference = (Spinner) result.findViewById(R.id.useFeedSpinner); scalePreference = (Spinner) result.findViewById(R.id.escaleSpinner); ArrayAdapter<CharSequence> scaleArray = ArrayAdapter.createFromResource(result.getContext(),R.array.escale_text, R.layout.support_simple_spinner_dropdown_item); scalePreference.setAdapter(scaleArray); scalePreference.setSelection(scaleValueToIndex(settings.getPowerScale())); namePreference = (EditText) result.findViewById(R.id.page_name); currencyPreference = (Spinner) result.findViewById(R.id.currency); unitCostPreference = (EditText) result.findViewById(R.id.costUnit); customCurrencyPreference = (EditText) result.findViewById(R.id.customCurrency); ArrayAdapter<CharSequence> costUnitArray = ArrayAdapter.createFromResource(result.getContext(),R.array.me_cost_text, R.layout.support_simple_spinner_dropdown_item); currencyPreference.setAdapter(costUnitArray); currencyPreference.setSelection(costSymbolToIndex(settings.getCostSymbol())); unitCostPreference.setText(""+settings.getUnitCost()); customCurrencyPreference.setText(settings.getCustomCurrencySymbol()); namePreference.setText(settings.getName()); return(result); }
Example 12
Source File: CustomInputStylePreference.java From AOSP-Kayboard-7.1.2 with Apache License 2.0 | 5 votes |
private static void setSpinnerPosition(final Spinner spinner, final Object itemToSelect) { final SpinnerAdapter adapter = spinner.getAdapter(); final int count = adapter.getCount(); for (int i = 0; i < count; i++) { final Object item = spinner.getItemAtPosition(i); if (item.equals(itemToSelect)) { spinner.setSelection(i); return; } } }
Example 13
Source File: GTwoConnectedComboView.java From geopaparazzi with GNU General Public License v3.0 | 4 votes |
/** * @param context the context to use. * @param attrs attributes. * @param parentView parent * @param label label * @param value _value * @param dataMap the map of the data. * @param constraintDescription constraints */ public GTwoConnectedComboView(Context context, AttributeSet attrs, LinearLayout parentView, String label, String value, LinkedHashMap<String, List<String>> dataMap, String constraintDescription) { super(context, attrs); this.dataMap = dataMap; if (value == null) value = ""; String[] valueSplit = value.split(SEP); String _combo1Value; String _combo2Value; if (valueSplit.length == 2) { _combo1Value = valueSplit[0]; _combo2Value = valueSplit[1]; } else { _combo1Value = ""; _combo2Value = ""; } TextView textView = new TextView(context); LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); textViewParams.setMargins(15, 25, 15, 15); textView.setLayoutParams(textViewParams); textView.setPadding(2, 2, 2, 2); textView.setText(label.replace(UNDERSCORE, " ").replace(COLON, " ") + " " + constraintDescription); textView.setTextColor(Compat.getColor(context, R.color.formcolor)); parentView.addView(textView); LinearLayout combosLayout = new LinearLayout(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); layoutParams.setMargins(10, 10, 10, 10); combosLayout.setLayoutParams(layoutParams); combosLayout.setOrientation(LinearLayout.VERTICAL); combosLayout.setBackground(Compat.getDrawable(context, R.drawable.thin_background_frame)); parentView.addView(combosLayout); combo1Spinner = new Spinner(context); LinearLayout.LayoutParams titleSpinnerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); titleSpinnerParams.setMargins(15, 25, 15, 15); combo1Spinner.setLayoutParams(titleSpinnerParams); Set<String> titlesSet = dataMap.keySet(); ArrayList<String> combo1Items = new ArrayList<>(titlesSet); combo1Items.add(0, ""); ArrayAdapter<String> titleListAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, combo1Items); titleListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); combo1Spinner.setAdapter(titleListAdapter); combo1Spinner.setPopupBackgroundDrawable(Compat.getDrawable(context, R.drawable.thin_background_frame)); combo1Spinner.setBackground(Compat.getDrawable(context, R.drawable.thin_background_frame)); int minHeight = getMinComboHeight(context); combo1Spinner.setMinimumHeight(minHeight); combo2Spinner = new Spinner(context); LinearLayout.LayoutParams valueSpinnerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); valueSpinnerParams.setMargins(15, 25, 15, 15); combo2Spinner.setLayoutParams(valueSpinnerParams); combo2Spinner.setPopupBackgroundDrawable(Compat.getDrawable(context, R.drawable.thin_background_frame)); combo2Spinner.setBackground(Compat.getDrawable(context, R.drawable.thin_background_frame)); combo2Spinner.setMinimumHeight(minHeight); List<String> combo2ItemsList = new ArrayList<>(); if (_combo1Value.length() > 0) { combo2ItemsList = dataMap.get(_combo1Value); int indexOf = combo1Items.indexOf(_combo1Value.trim()); if (indexOf != -1) { combo1Spinner.setSelection(indexOf, false); } } ArrayAdapter<String> combo2ListAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, combo2ItemsList); combo2ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); combo2Spinner.setAdapter(combo2ListAdapter); combosLayout.addView(combo1Spinner); combosLayout.addView(combo2Spinner); if (_combo2Value.length() > 0) { int position = combo2ListAdapter.getPosition(_combo2Value); combo2Spinner.setSelection(position, false); } combo1Spinner.setOnItemSelectedListener(this); }
Example 14
Source File: MainActivity.java From BotLibre with Eclipse Public License 1.0 | 4 votes |
public void resetLast() { String type = MainActivity.defaultType; Spinner spin = (Spinner) findViewById(R.id.typeSpin); if (spin != null) { type = (String) spin.getSelectedItem(); if (type.equals("Chat Bot Wars")) { spin.setSelection(0); war(null); return; } } if (type == null) { type = MainActivity.defaultType; } Button button = (Button) findViewById(R.id.lastButton); if (button != null) { SharedPreferences cookies = getPreferences(Context.MODE_PRIVATE); String last = null; if (type.equals("Forums")) { last = cookies.getString("forum", null); } else if (type.equals("Live Chat")) { last = cookies.getString("channel", null); } else if (type.equals("Domains")) { last = cookies.getString("domain", null); } else if (type.equals("Avatars")) { last = cookies.getString("avatar", null); } else if (type.equals("Scripts")) { last = cookies.getString("script", null); } else if (type.equals("Graphics")) { last = cookies.getString("graphic", null); } else { last = cookies.getString("instance", null); } if (last != null) { button.setText(last); button.setVisibility(View.VISIBLE); } else { button.setVisibility(View.GONE); } } }
Example 15
Source File: RTCStreamingEntryActivity.java From PLDroidRTCStreaming with Apache License 2.0 | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rtc_streaming_config); setTitle(R.string.rtc_rtmp); mCaptureRadioGroup = (RadioGroup) findViewById(R.id.CaptureRadioGroup); mCodecRadioGroup = (RadioGroup) findViewById(R.id.CodecRadioGroup); mRTCModeRadioGroup = (RadioGroup) findViewById(R.id.RTCModeGroup); mBitrateControlRadioGroup = (RadioGroup) findViewById(R.id.bitrate_control_group); mCheckBoxBeauty = (CheckBox) findViewById(R.id.CheckboxBeauty); mCheckBoxWatermark = (CheckBox) findViewById(R.id.CheckboxWatermark); mCheckBoxQuic = (CheckBox) findViewById(R.id.CheckboxQuic); mCheckBoxDebugMode = (CheckBox) findViewById(R.id.CheckboxDebugMode); mCheckBoxCustomSetting = (CheckBox) findViewById(R.id.CheckboxCustomSetting); mCheckBoxAudioLevel = (CheckBox) findViewById(R.id.CheckboxAudioLevel); mCheckboxEnableStats = (CheckBox) findViewById(R.id.CheckboxEnableStats); mPreviewSizeRatioSpinner = (Spinner) findViewById(R.id.PreviewSizeRatioSpinner); mPreviewSizeLevelSpinner = (Spinner) findViewById(R.id.PreviewSizeLevelSpinner); mEncodingSizeRatioSpinner = (Spinner) findViewById(R.id.EncodingSizeRatioSpinner); mEncodingSizeLevelSpinner = (Spinner) findViewById(R.id.EncodingSizeLevelSpinner); mEncodingConfigSpinner = (Spinner) findViewById(R.id.EncodingConfig); mVideoProfileSpinner = (Spinner) findViewById(R.id.VideoProfileSpinner); mYuvFilterModeSpinner = (Spinner) findViewById(R.id.YuvFilterModeSpinner); mVideoProfileText = (TextView) findViewById(R.id.VideoProfileText); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, StreamingSettings.PREVIEW_SIZE_RATIO_TIPS_ARRAY); mPreviewSizeRatioSpinner.setAdapter(adapter); mPreviewSizeRatioSpinner.setSelection(1); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, StreamingSettings.PREVIEW_SIZE_LEVEL_TIPS_ARRAY); mPreviewSizeLevelSpinner.setAdapter(adapter); mPreviewSizeLevelSpinner.setSelection(1); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, StreamingSettings.ENCODING_SIZE_RATIO_TIPS_ARRAY); mEncodingSizeRatioSpinner.setAdapter(adapter); mEncodingSizeRatioSpinner.setSelection(1); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, StreamingSettings.ENCODING_SIZE_LEVEL_TIPS_ARRAY); mEncodingSizeLevelSpinner.setAdapter(adapter); mEncodingSizeLevelSpinner.setSelection(1); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, StreamingSettings.ENCODING_TIPS_ARRAY); mEncodingConfigSpinner.setAdapter(adapter); mEncodingConfigSpinner.setSelection(0); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, StreamingSettings.VIDEO_QUALITY_PROFILES); mVideoProfileSpinner.setAdapter(adapter); mVideoProfileSpinner.setSelection(0); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, StreamingSettings.YUV_FILTER_MODE); mYuvFilterModeSpinner.setAdapter(adapter); mYuvFilterModeSpinner.setSelection(0); mCheckBoxCustomSetting.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { mVideoProfileSpinner.setVisibility(isChecked ? View.VISIBLE : View.GONE); mVideoProfileText.setVisibility(isChecked ? View.VISIBLE : View.GONE); } }); mRoomEditText = (EditText) findViewById(R.id.RoomNameEditView); SharedPreferences preferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE); mRoomEditText.setText(preferences.getString("roomName", "")); MultiDex.install(this); try { PLNetworkManager.getInstance().startDnsCacheService(this); } catch (UnknownHostException e) { e.printStackTrace(); } mProgressDialog = new ProgressDialog(this); }
Example 16
Source File: TGMeasureCleanDialog.java From tuxguitar with GNU Lesser General Public License v2.1 | 4 votes |
@SuppressWarnings("unchecked") public void updateSpinnerSelection(Spinner spinner, int selection) { ArrayAdapter<TGSelectableItem> adapter = (ArrayAdapter<TGSelectableItem>) spinner.getAdapter(); spinner.setSelection(adapter.getPosition(new TGSelectableItem(Integer.valueOf(selection), null)), false); }
Example 17
Source File: DoTranslate.java From ArscEditor with Apache License 2.0 | 4 votes |
@SuppressLint("InflateParams") public void init(int _position) { // 获取需要翻译的条目所在位置 position = _position; LayoutInflater factory = LayoutInflater.from(mContext); // 得到自定义对话框 View DialogView = factory.inflate(R.layout.translate, null); // 创建对话框 new AlertDialog.Builder(mContext).setView(DialogView)// 设置自定义对话框的样式 .setTitle(R.string.translate) // 设置进度条对话框的标题 .setNegativeButton(R.string.translate, new DialogInterface.OnClickListener() // 设置按钮,并监听 { @SuppressWarnings("unchecked") @Override public void onClick(DialogInterface p1, int p2) { // 开启一个翻译线程 new translate_task().execute(source_list, target_list); } }).create()// 创建 .show(); // 显示对话框 // 找到显示源语言的Spinner控件 src_type = (Spinner) DialogView.findViewById(R.id.src_type); // 找到显示目标语言的Spinner控件 translate_to = (Spinner) DialogView.findViewById(R.id.translate_to); // 找到翻译商的选项的Spinner控件 translator = (Spinner) DialogView.findViewById(R.id.translator); // 找到显示跳过选项的CheckBox控件 skip_already_translate = (CheckBox) DialogView.findViewById(R.id.skip_already_translate); // 源语言默认自动识别 src_type.setSelection(0); // 翻译为默认选择中文 translate_to.setSelection(1); // 默认选择百度翻译 translator.setSelection(0); // 如果是翻译单个条目,需要隐藏“跳过已翻译的内容”的选项,否则需要显示该选项 skip_already_translate.setVisibility(translate_all ? View.VISIBLE : View.GONE); skip_already_translate.setOnCheckedChangeListener(this); }
Example 18
Source File: ExchangeOtherEditText.java From xmrwallet with Apache License 2.0 | 4 votes |
@Override void setInitialSpinnerSelections(Spinner baseSpinner, Spinner quoteSpinner) { baseSpinner.setSelection(0, true); quoteSpinner.setSelection(1, true); }
Example 19
Source File: DeleteCategory.java From Passbook with Apache License 2.0 | 4 votes |
@Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView; if(Application.getInstance()==null || Application.getInstance().getAccountManager() == null) { return null; } if(savedInstanceState != null) { mPosition = savedInstanceState.getInt("category"); } rootView = inflater.inflate(R.layout.dialog_delete_category, container); Button button = rootView.findViewById(R.id.ok); button.setOnClickListener(this); button = rootView.findViewById(R.id.cancel); button.setOnClickListener(this); Spinner spinner = rootView.findViewById(R.id.spinner); String[] allNames = Application.getInstance().getSortedCategoryNames(); String[] deletableNames = new String[allNames.length-1]; int i, j = 0; for(i = 1; i < allNames.length; ++i) { deletableNames[j++] = allNames[i]; } if(mPosition < 0) { int[] allIds = Application.getInstance().getSortedCategoryIds(); for (i = 1; i < allIds.length; ++i) { if (mCategory == allIds[i]) { mPosition = i; break; } } mPosition -= 1; if(mPosition < 0) { mPosition = 0;} } ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_dropdown_item , deletableNames); spinner.setAdapter(spinnerAdapter); spinner.setSelection(mPosition); spinner.setOnItemSelectedListener(this); mCheckBox = rootView.findViewById(R.id.checkbox); return rootView; }
Example 20
Source File: SpinnerWidget.java From commcare-android with Apache License 2.0 | 4 votes |
public SpinnerWidget(Context context, FormEntryPrompt prompt) { super(context, prompt); mItems = getSelectChoices(); spinner = new Spinner(context); choices = new String[mItems.size()]; for (int i = 0; i < mItems.size(); i++) { choices[i] = prompt.getSelectChoiceText(mItems.get(i)); } // The spinner requires a custom adapter. It is defined below SpinnerAdapter adapter = new SpinnerAdapter(getContext(), android.R.layout.simple_spinner_item, getChoicesWithEmptyFirstSlot(choices), TypedValue.COMPLEX_UNIT_DIP, mQuestionFontSize); spinner.setAdapter(adapter); spinner.setPrompt(prompt.getQuestionText()); spinner.setEnabled(!prompt.isReadOnly()); spinner.setFocusable(!prompt.isReadOnly()); // Fill in previous answer String s = null; if (prompt.getAnswerValue() != null) { s = ((Selection)prompt.getAnswerValue().getValue()).getValue(); } if (s != null) { for (int i = 0; i < mItems.size(); ++i) { String sMatch = mItems.get(i).getValue(); if (sMatch.equals(s)) { // Workaround to a really weird spinner bug that triggers onItemSelected // in the initial selection of spinner even though no listener is set. // https://stackoverflow.com/a/17336944/6671572 spinner.setSelection(i+1, false); } } } spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { widgetEntryChanged(); } @Override public void onNothingSelected(AdapterView<?> parent) { //do nothing here } }); addView(spinner); }