Android:无法从Spinner中检索所选项目

时间:2014-03-10 11:25:44

标签: android list menu sms

在我的应用程序中,我尝试发送received inbox sms's个详细信息 - 发件人的地址,使用Sms Manager to send message作为新邮件接收时间。另外,我提供不同的格式,以便根据格式设置这些细节。使用复选框选择多条消息。我在application menu as setting menuItem中提供了这些格式问题是sent消息未显示Sender's No

在主要活动中:

String  address="",date="",body="", phoneNo;
Spinner fSpinner,phSpinner;

当打开菜单中的设置时,弹出包含两个微调器的alert dialog - 用于正文格式的fSpinner和用于电话号码的phSpinner。

case R.id.action_settings:

    AlertDialog.Builder rdialog = new AlertDialog.Builder(MainActivity.this);
    rdialog.setTitle("Select Format");
    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
    alertView = inflater.inflate(R.layout.rptsetting,null);

    final Spinner fSpinner = (Spinner)alertView.findViewById(R.id.fSpinner);
    String providers[] ={"Address","Time"};
    ArrayAdapter<String> adp = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,providers);
    fSpinner.setAdapter(adp);
    fSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> aparent, View arg1,
                int pos, long arg3) {

            String selectedItem = aparent.getSelectedItem().toString();
            if(selectedItem.equals("Address")){
                body = "Sender's Address -"+address+", Message Body -"+reltxt.getText().toString();  
            }

            else if(selectedItem.equals("Time")){
                    body = "Message Time -"+time.get(i)+", Message Body -"+reltxt.getText().toString();
            }}

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

    final Spinner phSpinner = (Spinner)alertView.findViewById(R.id.phSpinner);

    String number[] ={"1234","2222","0000"};
    ArrayAdapter<String> adp1 = new ArrayAdapter<String> (MainActivity.this, android.R.layout.simple_spinner_dropdown_item,number);
    phSpinner.setAdapter(adp1);
    phSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> phParent, View item,
                int post, long arg3) {
            String phNo = phParent.getSelectedItem().toString();
            phlabel = phParent.getSelectedItem().toString();

            if(phNo.equals("1234")){
                phoneNo = "1234";
            }
            else if(phNo.equals("2222")){
                phoneNo="2222";
            }
            else if(phNo.equals("0000")){
                phoneNo="0000";
            }}

        @Override
        public void onNothingSelected(AdapterView<?> phParent) {
        }
    });
    rdialog.setView(alertView);
    rdialog.setNeutralButton("SUBMIT", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();   
            }
        });

    AlertDialog rdialog1 = rdialog.create();
    rdialog1.show();

已检查的消息将存储在数组list中。并且Sms管理器将发送这些所选消息的详细信息。 phoneNo由phSpinner和body选择,使用fSpinner选择。

if(list.size()>0){
     for(int i=0;i<list.size();i++)
     {
         if(list.get(i).isSelected())
         {
             if(address.equals(""))
                 address =list.get(i).getAddress();
             else
                 address =list.get(i).getAddress();
             try 
             {
                 SmsManager smsMgr = SmsManager.getDefault();
                 smsMgr.sendTextMessage(phoneNo, null, body, null, null);
             }
             catch (Exception e) 
             {
                 Toast.makeText(this, e.getMessage()+"!\n"+"SMS failed, please try again", Toast.LENGTH_SHORT).show();
                 e.printStackTrace();
             }
         }
     }
 }

1 个答案:

答案 0 :(得分:0)

试试这个..

String selectedItem = fSpinner.getSelectedItem().toString();

而不是这个..

String selectedItem = aparent.getSelectedItem().toString();

还有。

String phNo = phSpinner.getSelectedItem().toString();
phlabel = phSpinner.getSelectedItem().toString();

而不是

String phNo = phParent.getSelectedItem().toString();
phlabel = phParent.getSelectedItem().toString();