简体   繁体   English

将数据加载到android Spinner,选择项目时崩溃

[英]Loading the data to android spinner and it crashes when selecting an item

I have this method and and it returns three values as shown in the screenshot below, 我有此方法,它返回三个值,如下面的屏幕快照所示,

List<String> resultSizes = getFilteredSizes(crustSelectedItem);

在此处输入图片说明

And I'm loading these 3 data to android spinner, then I'm using this code of piece to get the spinner value I have selected, 然后将这3个数据加载到android Spinner中,然后使用这段代码来获取我选择的Spinner值,

String StrSizeSPPosition = String.valueOf(sizeSP.getSelectedItemPosition()); 

it says 0 . 它说0

Then I have a string list as sizePrice , and it returns the below shown values, 然后我有一个字符串列表,如sizePrice ,它返回下面显示的值,

在此处输入图片说明

Im using this piece of code to get the selected item's respected value using this, 我使用这段代码来使用此代码来获得所选项目的尊敬值,

StrPriceSize = sizePrice.get(SizeSPPosition); // crashes here
PriceSize = Double.parseDouble(StrPriceSize);
System.out.println("PriceSize = " + PriceSize);

My problem is when I select pan large then it should give the output as 1560 because the I'm getting the index positions, but it gives 450. and when I select other value it crashes giving an exception. 我的问题是,当我选择pan large时,它应该给出1560的输出,因为我正在获取索引位置,但是它给出了450。当我选择其他值时,它崩溃并给出异常。

this is the exception, 这是例外

在此处输入图片说明

this is the logcat output 这是logcat的输出

在此处输入图片说明

Complete code 完整的代码

                        List<String> resultSizes = getFilteredSizes(crustSelectedItem);

                        ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
                                Activity.this,
                                android.R.layout.simple_spinner_item,
                                resultSizes);
                        dataAdapterDes
                                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        sizeSP.setAdapter(dataAdapterDes);

                        sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
                                Activity.this) {

                            public void onNothingSelected(AdapterView<?> arg0) {
                            }

                            @Override
                            public void onItemSelected(AdapterView<?> parent,
                                    View view, int position, long id) {

                                String sizeSelectedItem = sizeSP
                                        .getSelectedItem().toString();

                                String StrSizeSPPosition = String
                                        .valueOf(sizeSP
                                                .getSelectedItemPosition());

                                System.out.println("sizeSelectedItem = "
                                        + sizeSelectedItem);
                                System.out.println("StrSizeSPPosition = "
                                        + StrSizeSPPosition);

                                SizeSPPosition = Integer
                                        .parseInt(StrSizeSPPosition);

                                String StrPriceSize = sizePrice
                                        .get(SizeSPPosition);
                                PriceSize = Double.parseDouble(StrPriceSize);

(I have another option it has only 1 value when I'm selecting that it work properly without crashing, i think I'm getting this issue only when I have data mora that 1) I hope my question is clear enough to understand. (我还有另一种选择,当我选择它可以正常工作而不会崩溃时,它只有1个值,我认为只有当我的数据量为1时,我才会遇到此问题。)希望我的问题很清楚可以理解。 If you need more details please ask to make the question clear. 如果您需要更多详细信息,请要求将问题弄清楚。 Any help would be appreciated to sort out this. 任何帮助将不胜感激理清这一点。

Instead of this line of code :- 代替这一行代码:-

String StrPriceSize = sizePrice.get(SizeSPPosition);
PriceSize = Double.parseDouble(StrPriceSize);

Put this code:- 输入以下代码:-

String StrPriceSize = resultSizes.get(SizeSPPosition);
 PriceSize = Double.parseDouble(StrPriceSize);

or try this:- 或尝试以下方法:

try to print Log.d("sizePriceList", sizePrice+""); 尝试打印Log.d(“ sizePriceList”,sizePrice +“”); in the code and check how many elements you are getting when selecting item from spinner. 在代码中,检查从微调器中选择项目时获得了多少个元素。 your logcat showing an exception of indexoutofbound that means you are getting only one item in sizePrice 您的logcat显示indexoutofbound的异常,这意味着您仅获得sizePrice的一项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM