I am Android Newbie here I do not know how to pass the selected spinner text to pass to SMS as SMS text to send to selected number by pressing a button. I am happy if someone can teach me here.
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);
}
}
//-must end here
First Save Selected value in one String Variable and then Send into SMS and another option is declare
int indexvariable outsideonItemSelected()function, sorry for my bad english communication but it will solve your problem, please see below link for more information.Spinners in Android
and Use below code instead of your code.