【问题标题】:Android spinner, get selectedItemAndroid 微调器,获取 selectedItem
【发布时间】:2016-03-15 19:21:04
【问题描述】:

正如我所说,我正在开发一个“预约”应用程序。在其中,它有一个微调器,用户可以在其中选择位置。 在BookAppointment.java 这是微调器代码:

public void addListenerOnSpinnerItemSelection() {
    pspinner = (Spinner) findViewById(R.id.spinner1);
    pspinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

CustomOnItemSelectedListener.java:

public class CustomOnItemSelectedListener implements OnItemSelectedListener {
BookAppointment ba = new BookAppointment();
String place;
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
    Toast.makeText(parent.getContext(),
            "OnItemSelectedListener: " + parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
    place = parent.getItemAtPosition(pos).toString(); //the actual selected item of spinner
    ba.setPlace(place);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}
}

BookAppointment.java 中我想到达所选项目,所以我尝试了

public void setPlace(String place){
    this.place = place;
}

submit.onClick 中有一个BookApp(user, place, completeDate); 在调试模式下,为什么在 onClick 中place 将是null。但即使我更改了spinnersetPlace 也在运行,this.place 将等于原来的place,但在它之后不知何故place 等于null。在 BookAppointment.java 中有一个全局 public String place; ,而我在 BookAppointment.java 中无处使用 place。请帮助我,我错过了什么?我做错了什么?

点击:

public void addListenerOnButton() {
    btnSubmitP = (Button) findViewById(R.id.btnSubmitP);
    pspinner = (Spinner) findViewById(R.id.spinner1);
    btnSubmitP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*May I should place here the spinner's findViewById ?*/
            place = pspinner.getSelectedItem().toString(); //to get the actual selected place
            String completeDate = date + " " + itemValue;
            Toast.makeText(BookAppointment.this,
                    "OnClickListener : " +
                            "\nHelyszín: " + place + //it prints the actual selected item
                            "\nDátum: " + completeDate,
                    Toast.LENGTH_SHORT).show();


            User user = userLocalStore.getLoggedInUser();
            if (user == null) {
                Toast.makeText(BookAppointment.this,
                        "user = null",
                        Toast.LENGTH_SHORT).show();
            }
            BookApp(user, place, completeDate); //Here the debug not display the place, nothin like place=null, nothing, only the user and completeDate's value
        }

    });
}

【问题讨论】:

    标签: java android spinner onitemselectedlistener


    【解决方案1】:

    尝试在 submit.onClick 中添加这个:

    String places= spinnerReferenceName.getSelectedItem().toString();
    

    这应该会从微调器返回您选择的字符串。

    【讨论】:

    • 我试过了,但不知为何它只在活动开始时才有效。在微调器中的任何编辑(如选择另一个项目)不显示后,place 保持原来的第一项:S。
    • 我把这个地方放在了 onClick 外面,现在它在里面,但不知何故应用程序只是跳过它,好像永远不会在那里 O.o
    • 现在,它可以工作了,谢谢!不过debug越过地方,不过在BookApp()这个地方就对了!我只是应该将 findviewbyid 放在 onclick 之外!谢谢!
    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多