Marroquino Marroquino - 21 days ago 8
Android Question

I get error in spinner selected value

I'm trying to make an app to send messages with default or user predefined messages and phone numbers I will put here my code

private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;
String phoneNo, message;
List<String> pn, txt;
Spinner sp1, sp2;
Button b, b1, b2, b3, b4;
EditText et, et1;

ArrayAdapter<String> adp1, adp2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setupSpinners();

b = (Button) findViewById(R.id.button);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
et = (EditText) findViewById(R.id.editText);
et1 = (EditText) findViewById(R.id.editText1);

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp1.add(et.getText().toString());
et.setText(null);
}

});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp2.add(et1.getText().toString());
et1.setText(null);
}

});
}

public void setupSpinners() {

sp1 = (Spinner) findViewById(R.id.spinner);
sp2 = (Spinner) findViewById(R.id.spinner1);
pn = new ArrayList<String>();
txt = new ArrayList<String>();
txt.add("Sorry Boss I'cant go today i'm sick");
pn.add("123456789");

adp1 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, pn);
adp2 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, txt);

sp1.setAdapter(adp1);
sp1.setSelection((pn.size() - 1));
sp2.setAdapter(adp2);
sp2.setSelection((txt.size() - 1));

sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
sp1.setSelection(arg2);
}

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


});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
sp2.setSelection(arg2);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

b4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}

protected void sendSMSMessage() {
phoneNo = sp1.getSelectedItem().toString();
message = sp2.getSelectedItem().toString();

if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
}
}

}
});
}
});
}


The part where it gives the error is the (this,

if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);


I need help to know how to select the spinner selected value to send trough text.

Answer

Here is one line code for get selected value in spinner.

String text = mySpinner.getSelectedItem().toString();

In your code

protected void sendSMSMessage() {
                    phoneNo =  sp1.getSelectedItem().toString();
                    message =  sp2.getSelectedItem().toString();