how to get selected spinner item and send through SMS - android

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

put this sendSMS("5556", "+location [index]"); in
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]);
}

First Save Selected value in one String Variable and then Send into SMS and another option is declare int index variable outside onItemSelected() 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.
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);
}
}

Related

switch case with two spinners and audio player

In my activity i have two spinners, one image view and two buttons.
depending on first spinner the second spinners should change. and while selecting birds in first spinner the second spinner should show parrot peacock etc. while selecting parrot in second spinner i should get the image of parrot.
till this i am getting.
but now what i want is while i press a button then i should get the voice of parrot.
in this code when i am pressing the button i am getting the same voice for every picture change in second spinner
public class MainActivity extends Activity {
ImageView displayIV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
;
Button b1;
b1=(Button)findViewById(R.id.button1);
Spinner friend = (Spinner) findViewById(R.id.spinner1);
final Spinner subFriend = (Spinner) findViewById(R.id.spinner2);
displayIV=(ImageView)findViewById(R.id.imageView1);
final ArrayList<String> friend_options = new ArrayList<String>();
final ArrayList<String> subfriend_options = new ArrayList<String>();
friend_options.add("Nuz");
friend_options.add("Dur");
friend_options.add("Tara");
friend_options.add("Sama");
ArrayAdapter<String> friendAdapter = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_spinner_item,
friend_options);
friend.setAdapter(friendAdapter);
ArrayAdapter<String> subFriendAdapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_spinner_item,subfriend_options);
subFriend.setAdapter(subFriendAdapter);
friend.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
String friendName =
friend_options.get(position).toString();
resetFriend(friendName);
// subFriend.setAdapter(null);
}
private void resetFriend(String friendName) {
subfriend_options.removeAll(subfriend_options);
if (friendName.equals("Nuz")) {
subfriend_options.add("Nuz_1");
subfriend_options.add("Nuz_2");
subfriend_options.add("Nuz_3");
subfriend_options.add("Nuz_4");
} else if (friendName.equals("Dur")) {
subfriend_options.add("Dur_1");
subfriend_options.add("Dur_2");
subfriend_options.add("Dur_3");
subfriend_options.add("Dur_4");
} else if (friendName.equals("Tara")) {
subfriend_options.add("Tara_1");
subfriend_options.add("Tara_2");
subfriend_options.add("Tara_3");
subfriend_options.add("Tara_4");
} else {
subfriend_options.add("Sama_1");
subfriend_options.add("Sama_2");
subfriend_options.add("Sama_3");
subfriend_options.add("Sama_4");
}
ArrayAdapter<String> subFriendAdapter = new
ArrayAdapter<String>( getApplicationContext(),
android.R.layout.simple_spinner_item,
subfriend_options);
subFriend.setAdapter(subFriendAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
subFriend.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
arg0.getItemAtPosition(arg2);
final ImageView im =
(ImageView)findViewById(R.id.imageView1);
String s=((TextView)arg1).getText().toString();
if(s.equals("Tara_1")){
im.setImageDrawable(getResources().getDrawable(R.drawable.crow));
}
if(s.equals("Tara_2"))
im.setImageDrawable(getResources().getDrawable(R.drawable.india1));
if(s.equals("Tara_3"))
im.setImageDrawable(getResources().getDrawable(R.drawable.peacock));
if(s.equals("Tara_4"))
im.setImageDrawable(getResources().getDrawable(R.drawable.robin1));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//MediaPlayer mMediaPlayer = MediaPlayer.create(this,
R.raw.Kalimba);
MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.abc);
mp.start();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In the setOnClickListener method of your button, b1, you need to know which image has been selected on the spinner (your description is a little bit confusing so i don't have clear exactly which spinner is going to change the sound that you app has to reproduce).
Anyway, let think that the spinner is "friend_options".
In you setOnClickListener we have to know which item has been selected for the user when click on the button, right? And depending of which item was selected, we will play one specific sound.
So....
Another important poin is that you should create the variable of the mediaplayer outside of the onclickListener, like a class variable:
//Class variable:
public MediaPlayer mp;
In the onCreate method you should initialize the MediaPlayer component:
public void onCreate()
{
MediaPlayer.create(MainActivity.this, R.raw.abc); //Default sound, it is not
//importat because we are going
//to chain the value of the
//file to reproduce in the next
//step.
}
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//Here we are going to take the item selected:
String itemSelected = spinner.getSelectedItem().toString();
int soundToPlayId=0;
if(itemSelected.equals("Nuz"))
{
soundToPlayId = R.raw.Kalimba ;
}
else if(itemSelected.equals("Dur"))
{
soundToPlayId = R.raw.abc;
}
// etc etc etc --> you should put all the sound associated to all the
// friends
//And now you only have to reproduce if the item was selected properly:
if(soundToPlayId !=0) //if the id is different to 0:
{
mp = MediaPlayer.setDataSource(MainActivity.this, soundToPlayId);
mp.start();
}
}
});
Let see if it is working!!! Ask me if you have any doubt...and if the answer is correct, vote me and check as correct!!! (I need points, thanks!)

onItemSelected method help - loading an Intent

I have tried Googling but couldn't get an answer specific to my problem.
I am trying to launch an activity when a item is clicked. The code is below:
public class ListOfCircuits extends Activity
{
String[] raceTrackList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_circuits);
raceTrackList = getResources().getStringArray(R.array.tracklist_array);
Spinner s1 = (Spinner) findViewById(R.id.circuitListSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, raceTrackList);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected: " + raceTrackList[index],
Toast.LENGTH_SHORT).show();
String intentString;
String circuitChosen = raceTrackList[index].toString();
intentString = ("net.learn2develop." + circuitChosen);
startActivity(new Intent(intentString));
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
However at the moment it loads straight into the activity at the top of the list (TopGear). How do you get around this so that a user can see the list and select it i.e. not start an intent immediately - use an if statement?
Thanks.
This happens because the method is called the first time the Activity loads. There should be a better way to handle this but you could simply set a boolean flag
public class ListOfCircuits extends Activity
{
String[] raceTrackList;
boolean go = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
then set it to true the first time it is called and check it there
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
if (go)
{
// your code
intentString = ("net.learn2develop." + circuitChosen);
startActivity(new Intent(intentString));
}
else
{ go = true; }
}
CodeMagic think I have it working like a boss now:
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
if(go)
{
int index = arg0.getSelectedItemPosition();
if(index == 0)
{
Toast.makeText(getBaseContext(),
"Please select a race crcuit.",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(),
"You have selected: " + raceTrackList[index],
Toast.LENGTH_SHORT).show();
String intentString;
String circuitChosen = raceTrackList[index].toString();
intentString = ("net.learn2develop." + circuitChosen);
startActivity(new Intent(intentString));
}
}
else
{
go = true;
}
}
I only load Intents that I want (race circuits) and if a user selects "Choose from below:" only one relevant Toast gets displayed.
Thanks.
The answer to my answer was:
Add the Boolean as codeMagic suggested and amend my array to:
<string-array name="tracklist_array">
<item>Choose from below:</item>
<item>TopGear</item>
<item>Snetterton</item>
<item>Silverstone</item>
<item>Donnington</item>
</string-array>
From
<string-array name="tracklist_array">
<item>TopGear</item>
<item>Snetterton</item>
<item>Silverstone</item>
<item>Donnington</item>
</string-array>
And the final code is for Java - see below.
Thanks again.

Application crashes on selecting spinner item?

In my application, I'm providing various Templates for the Text Message Body, as spinner list items , that can be selected and user can send them instead of typing message, But the problem is when user open the menu item to select template the application crashes. Spinner i've put in alert dialog box which is accessible through menu item.
Code for dialog box *
AlertDialog.Builder rdialog = new AlertDialog.Builder(MainActivity.this);
rdialog.setTitle("Select Message");
rdialog.setIcon(android.R.drawable.ic_input_get);
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
alertView = inflater.inflate(R.layout.rptsetting,null);
final Spinner fSpinner = (Spinner)alertView.findViewById(R.id.fSpinner);
String providers[] ={"Busy", "Good Morning", "In office"};
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 = fSpinner.getSelectedItem().toString();
if(selectedItem.equals("Busy")){
body = "Currently Busy call again later, Thanks";
}
if(selectedItem.equals("Good Morning")){
body = "A very Good Morning, Have a nice day";
}
if(selectedItem.equals("In office")){
body = "Currently in office";
}
}
#Override
public void onNothingSelected(AdapterView<?> aparent) {
}
});
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();
I've define body as global String so that it can be accessible by Sms Manager to use it as body of message to be send.
Log Cat
Thanks in advance!
First Correct this one like under onItemSelected(.....)
String selectedItem = aparent.getItemAtPosition(pos).toString();
if(selectedItem.equals("Busy")){
body = "Currently Busy call again later, Thanks";
}
if(selectedItem.equals("Good Morning")){
body = "A very Good Morning, Have a nice day";
}
if(selectedItem.equals("In office")){
body = "Currently in office";
}
And also cross check your body variable is not null
To get selected item from Spinner try using getItemAtPosition method of AdapterView. as:
#Override
public void onItemSelected(AdapterView<?> aparent, View arg1,
int pos, long arg3) {
String selectedItem = aparent.getItemAtPosition(pos).toString();
//...your code...
}

How to get selected item in Spinner

I keep going round in circles with this one. I have managed to set the spinner to show item in the list if it matches a record in the database, but now have an issue with getting the selected item from the spinner when I save the record. I instead get something like 'android.database.sqlite.SQLiteCursor#44fa41b0'.
In my saveInspection() method, I am using inspectedBySpinner.getSelectedItem().toString(); (as detailed in second answer in this post How do you get the selected value of a Spinner?) with no success.. (so close yet no banana!).
I'm sure this is something flippin obvious, but help much appreciated:
public class InspectionEdit extends Activity {
final Context context = this;
private EditText inspectionReferenceEditText;
private EditText inspectionCompanyEditText;
private Button inspectionDateButton;
private Spinner inspectedBySpinner;
private Button saveButton;
private Button cancelButton;
protected boolean changesMade;
private AlertDialog unsavedChangesDialog;
private Button addInspectorButton;
private int mYear;
private int mMonth;
private int mDay;
private StringBuilder mToday;
private RMDbAdapter rmDbHelper;
private long inspectionId;
private String inspectedBySpinnerData;
//private String inspectors;
static final int DATE_DIALOG_ID = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
Intent i = getIntent();
inspectionId = i.getLongExtra("Intent_InspectionID", -1);
setContentView(R.layout.edit_inspection);
setUpViews();
populateFields();
fillSpinner();
setTextChangedListeners();
}
private void setUpViews() {
inspectionReferenceEditText =(EditText)findViewById(R.id.inspection_reference);
inspectionCompanyEditText =(EditText)findViewById(R.id.inspection_company);
inspectionDateButton =(Button)findViewById(R.id.inspection_date);
inspectedBySpinner =(Spinner)findViewById(R.id.inspected_by_spinner);
addInspectorButton = (Button)findViewById(R.id.add_inspector_button);
saveButton = (Button)findViewById(R.id.inspection_save_button);
cancelButton = (Button)findViewById(R.id.inspection_cancel_button);
}
private void populateFields() {
if (inspectionId > 0) {
Cursor inspectionCursor = rmDbHelper.fetchInspection(inspectionId);
startManagingCursor(inspectionCursor);
inspectionReferenceEditText.setText(inspectionCursor.getString(
inspectionCursor.getColumnIndexOrThrow(RMDbAdapter.INSPECTION_REF)));
inspectionCompanyEditText.setText(inspectionCursor.getString(
inspectionCursor.getColumnIndexOrThrow(RMDbAdapter.INSPECTION_COMPANY)));
inspectionDateButton.setText(inspectionCursor.getString(
inspectionCursor.getColumnIndexOrThrow(RMDbAdapter.INSPECTION_DATE)));
inspectedBySpinnerData = inspectionCursor.getString(
inspectionCursor.getColumnIndexOrThrow(RMDbAdapter.INSPECTION_BY));
Toast.makeText(getApplicationContext(), inspectedBySpinnerData,
Toast.LENGTH_LONG).show();
}
}
private void fillSpinner() {
Cursor inspectorCursor = rmDbHelper.fetchAllInspectors();
startManagingCursor(inspectorCursor);
// create an array to specify which fields we want to display
String[] from = new String[]{RMDbAdapter.INSPECTOR_NAME};
//INSPECTOR_NAME = "inspector_name"
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter spinnerAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, inspectorCursor, from, to );
spinnerAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
inspectedBySpinner.setAdapter(spinnerAdapter);
if (inspectionId > 0) {
int spinnerPosition = 0;
for (int i = 0; i < inspectedBySpinner.getCount(); i++)
{
Cursor cur = (Cursor)(inspectedBySpinner.getItemAtPosition(i));
//--When your bind you data to the spinner to begin with, whatever columns you
//--used you will need to reference it in the cursors getString() method...
//--Since "getString()" returns the value of the requested column as a String--
//--(In my case) the 4th column of my spinner contained all of my text values
//--hence why I set the index of "getString()" method to "getString(3)"
String currentSpinnerString = cur.getString(1).toString();
if(currentSpinnerString.equals(inspectedBySpinnerData.toString()))
{
//--get the spinner position--
spinnerPosition = i;
break;
}
}
inspectedBySpinner.setSelection(spinnerPosition);
}
}
private void addInspector() {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompt_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
String inspector = userInput.getText().toString();
rmDbHelper.createInspector(inspector);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
private void setTextChangedListeners() {
changesMade = false;
inspectionReferenceEditText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
changesMade = true;
}
});
inspectionCompanyEditText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
changesMade = true;
}
});
inspectionDateButton.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
changesMade = true;
}
});
inspectionDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
addInspectorButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addInspector();
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
saveInspection();
finish();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
cancel();
}
});
}
protected void saveInspection() {
String reference = inspectionReferenceEditText.getText().toString();
String companyName = inspectionCompanyEditText.getText().toString();
String inspectionDate = RMUtilities.compareTwoStringsNullIfSame(inspectionDateButton.getText().toString(), "Click to add");
String inspectedBy = inspectedBySpinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), inspectedBy,
Toast.LENGTH_LONG).show();
if (inspectionId > 0) {
rmDbHelper.updateInspection(inspectionId, reference, companyName, inspectionDate, inspectedBy);
Toast.makeText(getApplicationContext(), "Inspection updated",
Toast.LENGTH_LONG).show();
}
else {
rmDbHelper.createInspection(reference, companyName, inspectionDate, inspectedBy);
Toast.makeText(getApplicationContext(), "Inspection created",
Toast.LENGTH_LONG).show();
}
}
As you use a CursorAdapter and not an Adapter based on a List or Array of String, you'll have to use the Cursor to fetch the value of the selected item. The Spinner's getSelectedItem will call the CursorAdapter's getItem(position) which will return the Cursor object. So instead to using toString(), first cast the returned object to a Cursor and then use Cursor's get... methods to fetch the required data of the selected item.
EDIT
Based on how you fill your spinner you'll probably need this:
String inspectedBy = ((Cursor)inspectedBySpinner.getSelectedItem())
.getString(1).toString();

Android Spinner is working -- but can't parse and pass the selected value

I'm trying to pass a value from an android spinner selection into a url. All of my other vars are passing and the toast on the spinner in displaying the correct spinner choice when selected. (for purposes here, the code doesn't show all vars.)
My log shows the country VAR as NULL. Need to get the "country0" value to pass like the others. How can I make this happen?
thanks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(DEBUG_TAG, "onCreate");
mContext = this; //TODO legacy may not be needed
SERVER = this.getString(R.string.mygallerist_server_base);
settings = getSharedPreferences(PREFS_NAME, 0);
uid = settings.getString("uid", NOT_SET);
//SPINNER
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mOver35CheckBox = (CheckBox) findViewById(R.id.Over35);
class MyOnItemSelectedListener implements OnItemSelectedListener {
//SPINNER PARSING
#Override
public void onItemSelected(AdapterView<?> country0,
View view, int pos, long id) {
Toast.makeText(country0.getContext(), "The country is " +
country0.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView country0 ) {
// Do nothing.
}
}
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
final String country = country0;///MAYBE THIS IS WHAT IS WRONG
setPassword.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// If all fields filled in then go to server
String country1 = country; /// OR MAYBE THIS IS WHAT IS WRONG
String userName = mUserNameEditText.getText().toString();
Perhaps I'm missing something in your question, but when you select the value for the Toast, you could just set the value of a private member, then read this value in your onClick, (being as you know you're getting the correct value in the onItemSelected)
private String countrySelection;
public void onItemSelected(AdapterView<?> country0, View view, int pos, long id) {
countrySelection = country0.getItemAtPosition(pos).toString();
Toast.makeText(country0.getContext(), "The country is " + countrySelection, Toast.LENGTH_LONG).show();
}
...
final String country = countrySelection;
setPassword.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
sendDataToServer(country, ...);
Does this solve your issue?
Edit...
What type is country0 in this context? (I don't have comment privileges yet)
final String country = country0;///MAYBE THIS IS WHAT IS WRONG

Categories

Resources