0

私は Android 初心者です。選択したスピナー テキストを SMS テキストとして SMS に渡し、ボタンを押して選択した番号に送信する方法がわかりません。ここで教えてくれる人がいたら嬉しいです。

public class MainActivity extends Activity { //all starts here
    String[] location;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location = getResources().getStringArray(R.array.location_array);
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, location);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                int index = arg0.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), "You have selected " + location[index], Toast.LENGTH_SHORT).show();

            }

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

        });
    }

        public void onClick(View v) {          //<--**HERE IS THE PROBLEM**
        sendSMS("5556", "+location [index]"); //<--**HERE IS THE PROBLEM**
    }



    //?sends an SMS message to another device?
    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

}

//-ここで終了する必要があります

4

2 に答える 2

1

sendSMS("5556", "+location [index]");これを入れる

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {

                Toast.makeText(getBaseContext(), "You have selected " + location[arg2], Toast.LENGTH_SHORT).show();
                sendSMS("5556", location[arg2]);
            }
于 2012-10-25T07:33:21.767 に答える
0

int index最初に選択した値を 1 つの文字列変数に保存し、次に SMS に送信します。別のオプションは、関数外で変数 を宣言することですonItemSelected()。英語のコミュニケーションが下手で申し訳ありませんが、問題は解決します。詳細については、以下のリンクを参照してください。

Android のスピナー

コードの代わりに以下のコードを使用します。

public class MainActivity extends Activity { //all starts here
    String[] location;
    int index;
    String mSelectedItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location = getResources().getStringArray(R.array.location_array);
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, location);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                index = arg0.getSelectedItemPosition();
                //OR you can also store selected item using below line.
                mSelectedItem=arg0.getSelectedItem().toString();
                Toast.makeText(getBaseContext(), "You have selected " + location[index], Toast.LENGTH_SHORT).show();
            }

            public void onNothingSelected(AdapterView<?> arg0){

            }

        });
    }

    public void onClick(View v) {
        sendSMS("5556", location [index]);
        //OR you can also send sms using below code.
        sendSMS("5556", mSelectedItem);
    }    

    //?sends an SMS message to another device?
    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}
于 2012-10-25T07:33:17.677 に答える