Android Spinner: Set selected item as default

后端 未结 4 460
甜味超标
甜味超标 2021-01-14 06:22

I am making an android app that asks for the users to select a country via spinner.

When the user opens the app first time, user selects a country from list.

<
4条回答
  •  野的像风
    2021-01-14 06:53

    Try this one

     String citySelected
        final CharSequence[] items = {" abc ", " def ", " ghi ", " jkl ", " mno ",
                " pqr ", " stu ",
                " vwzyz "};
        List lanSelected = new ArrayList<>();
        final boolean[] checkedItems = new boolean[]{false, false, false, false, false, false, false, false};
        List temp = new ArrayList<>();
        for (int o = 0; o < items.length; o++) {
            temp.add(items[o].toString());
        }
        final List seletedItems = new ArrayList();
    
        if (citySelected.equals("") || citySelected == null) {
        } else
            lanSelected = Arrays.asList(citySelected.split(","));
    
        if (lanSelected.size() > 0) {
            for (int p = 0; p < lanSelected.size(); p++) {
                String x = lanSelected.get(p);
                int xpos = temp.indexOf(x);
                if (xpos != -1) {
                    checkedItems[xpos] = true;
                    seletedItems.add(xpos);
                }
            }
        }
    
        AlertDialog.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(context);
        }
        // arraylist to keep the selected items
        AlertDialog dialog = builder
                .setTitle("city")
                .setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
                        if (isChecked) {
                            // If the user checked the item, add it to the selected items
                            seletedItems.add(indexSelected);
                        } else if (seletedItems.contains(indexSelected)) {
                            // Else, if the item is already in the array, remove it
                            seletedItems.remove(Integer.valueOf(indexSelected));
                        }
                    }
                }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        String city = "";
                        for (int i = 0; i < seletedItems.size(); i++) {
                            if (i == seletedItems.size() - 1) {
                                city = city + items[Integer.parseInt(seletedItems.get(i).toString())];
                            } else {
                                city = city + items[Integer.parseInt(seletedItems.get(i).toString())] + ",";
                            }
    
                        }
                        btn_city_todisplay.setText(city);
                        dialog.dismiss();
    
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //  Your code when user clicked on Cancel
                    }
                }).create();
        dialog.show();
    

提交回复
热议问题