How to retrieve String from a spinner using array? - android

I added arrays on the string.xml, and these strings are visible in my Spinner when I run the app. The problem is that the Toast does not display the value of the gender variable.
I made a separate class for this because I will also use Spinners for birthmoth, birthdate and birthyear.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
//SPINNERS
spinner_gender = (Spinner) findViewById(R.id.reg_gender);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.array_gender, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_gender.setAdapter(adapter);
spinner_gender.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id)
{
genderString = parent.getItemAtPosition(pos).toString();
Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG);
}
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing.
}
}

The Toast will not appear if you don't call the method show() on it:
Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

Try this..
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
OR.. Simply you can use
Toast.makeText(getApplicationContext(), " Your Toast message in here",Toast.LENGTH_LONG).show();

Related

How do I update TextView using 2 seperate spinner not independent of each other?

In this app, each spinner is used to make an API call.
Now, spinner 1 is for the coin(crypto) and 2 is for currency(USD, YEN etc) in which coin is to be displayed. The spinner 1 works perfectly fine! But spinner 2 wont update the TextView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPriceTextView = findViewById(R.id.priceLabel);
Spinner spinner1 = findViewById(R.id.currency_spinner);
Spinner spinner2 = findViewById(R.id.coin_spinner);
// Create an ArrayAdapter using the String array and a spinner layout
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.currency_array, R.layout.spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.coin_array, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter1.setDropDownViewResource(R.layout.spinner_dropdown_item);
adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
// TODO: Set an OnItemSelected listener on the spinner
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("Bitcoin-Ticker", "" + parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "Spinner has been set with listener " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
Log.d("Bitcoin-Ticker", "Final url1 is: " + parent.getItemAtPosition(position).toString());
finalUrl1 = parent.getItemAtPosition(position).toString();
update();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("Bitcoin-Ticker", "No item selected.");
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("Bitcoin-Ticker", "" + parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "Spinner has been set with listener" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
Log.d("Bitcoin-Ticker", "Spinner choice is: " + parent.getItemAtPosition(position));
String spinItem = parent.getItemAtPosition(position).toString();
finalUrl2 = parent.getItemAtPosition(position).toString();
if (spinItem.equals("BTC")) {
finalUrl2 = BASE_URL1 + finalUrl1;
} else {
finalUrl2 = BASE_URL2 + finalUrl1;
}
Log.d("Bitcoin-Ticker", "Final url2 is: " + finalUrl2);
update();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

ArrayList doesn't show up on Spinner

I was trying to create a spinner which display a list of data from array list.
When I clicked on the dropdown it displays the list, but when I clicked on an item inside the dropdown list it doesn't show up the value on the spinner.
Am I missing something here?
Note: Yesterday I have tried to check using Log.d() and System.out.println, itemOnSelected() doesn't but today it works fine. Maybe I rebuilding the project or I have changed something in the code but the value on the spinner still doesn't show up after I clicked on the item inside the spinner.
Spinner spnSubjectIDInfo;
ArrayList<String> subjectList;
ArrayAdapter<String> adpSubj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subjectList = new ArrayList<String>();
subjectList .add("John");
subjectList .add("Maxi");
subjectList .add("Jeni");
spnSubjectIDInfo = (Spinner) v.findViewById(R.id.spnSubjectIDInfo);
adpSubj = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_spinner_item, subjectList);
adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSubjectIDInfo.setAdapter(adpSubj);
spnSubjectIDInfo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
XML
<Spinner
android:id="#+id/spnSubjectIDInfo"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true" />
Try this
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ActivityName.this, subjectList.get(position).toString() " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
You are passing the wrong context to Toast instance:
If your code is in Activity then replace this by YourActivity.this or if it's in Fragment then use getActivity() or use Application context getApplicationContext(). Because currently, this is representing Spinner's onItemSelected listener context.
Do :
Toast.makeText(/*Your activity/application context*/, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
instead of:
Toast.makeText(this, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
Change the "this" to "ActvityName.this" or use "getApplicationContext()" in the Toast, you are passing anonymous class context in the toast.
Like below:
spnSubjectIDInfo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ActivityName.this,parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Hope this will help you.
public class SpinnerTest extends AppCompatActivity {
private ArrayList<String> subjectList;
private ArrayAdapter<String> adpSubj;
private Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spinner= (Spinner) findViewById(R.id.spnSubjectIDInfo);
subjectList = new ArrayList<String>();
subjectList .add("John");
subjectList .add("Maxi");
subjectList .add("Jeni");
adpSubj = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, subjectList);
adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adpSubj);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), spinner.getItemAtPosition(position).toString() + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
in Spinner setOnItemSelectedListener methord not work. if you want to get selected value show in toast use this line
String Text = mySpinner.getSelectedItem().toString();
Toast.makeText(this,Text,Toast.LENGTH_LONG).show();
it will return the selected value and will be displayed on Toast.
After few more searching I realized that I have to set the width size of the spinner to wrap_content as the length of the value doesn't support to display it on the spinner after the item being clicked at.
<Spinner
android:id="#+id/spnSubjectIDInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true" />

Why didn't my toast work?

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_listview);
myspinner = (Spinner)findViewById(R.id.spinnertype);
String provider = myspinner.getSelectedItem().toString();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, type_arrays,R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(adapter);
myspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(parent.getContext(), "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
i tried different kinds of toast but still it wont appear. what went wrong?
I viewed some posts and it says i cant use onclicklistener to a spinner
Have yout try this, that might help you
Toast.makeText(getApplicationContext(), "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
or
Toast.makeText(YourActivity.this, "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
Try using getContext() instead of getApplicationContext.
Ok, Let's check the codes again ;
Like i said, You'll need to use the current Activity Context.
For example, My Activity name is, MainActivity so, Use:
MainActivity.this This should be the context of that Activity.But i
think if you use your own code(parent.getContext()) inside the another
activity, It won't show up because it's somewhere else actually you
know
Also, You did something wrong here i guess (Something similiar : ArrayAdapter.createFromResource issue)
I just quote the answer:
Query the data, put it in a List or Array and use this constructor of
ArrayAdapter:
ArrayAdapter<CharSequence> dataAdapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, yourArrayOrList);
Update: So here it is:
Spinner myspinner = (Spinner)findViewById(R.id.spinnertype);
String provider = myspinner.getSelectedItem().toString();
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this, type_arrays, R.layout.support_simple_spinner_dropdown_item);
/*ArrayAdapter.createFromResource(this, type_arrays,R.layout.support_simple_spinner_dropdown_item);*/
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(adapter);
myspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(MainActivity.this, "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Now it should work!

Can't get string from spinner

I have spinner that have 5 text string. I want to get a string from the spinner, but I only get the first string (i can't get the second, third.....).
i use this syntax(below) but still failed:
Spinner spinner = (Spinner) findViewById(R.id.spinnerItem);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.SpinnerArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
String SpinnerText = myspinner.getSelectedItem().toString();
By using onItemSelectedLIstener() method you can get each spinner value into string.
Main.java
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selection=spinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "Selected" + selection, 30).show();
}
To get the selected item from the spinner first you need to set the listener for spinner using
spinner.setOnItemSelectedListener(this);
and u need to implement the interface OnItemSelectedListener
and finally override the methods
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selection.setText(items[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
try this code
in onclick listener of spinner use this code to get String of selected item
String s = spinneradapter.getItemAtPosition(Integer.parseInt(position));
hope this help
Use this :
String mySpinner = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
spinner.getItemAtPosition(0).toString()//First string
spinner.getItemAtPosition(1).toString()//second string
spinner.getItemAtPosition(2).toString()//third string
see below code it may help you.
spin_search.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int id,
long arg3) {
Toast.makeText(Sms_logs.this, "you select : " + adapter.getItem(id), 2000).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Spinner Switch Case Problem

EDIT: i have added in all of my code (excluding package and imports.....) and if i try to run it it crashes...... any ideas why?
public class BaseConverter extends Activity {
/** Called when the activity is first created. */
int inputBase;
int outputBase;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner input_spinner = (Spinner) findViewById(R.id.InputSpinner);
Spinner output_spinner = (Spinner) findViewById(R.id.OutputSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.base_numbers_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
input_spinner.setAdapter(adapter);
output_spinner.setAdapter(adapter);
input_spinner.setOnItemSelectedListener(new InputItemSelectedListener());
output_spinner.setOnItemSelectedListener(new OutputItemSelectedListener());
}
public class InputItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id)
{
/* switch (Integer.parseInt(parent.getItemAtPosition(pos).toString())
case ((Integer)parent.getItemAtPosition(pos)).intValue();
inputBase = 2;
break;
case 8:
inputBase = 8;
break;
case 10;
inputBase = 10;
break;
case 16;
inputBase = 16;
break;
*/
Toast.makeText(parent.getContext(), "You selected input base " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
public class OutputItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "You selected output base " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
and now need to have a switch - case scenario that all revolves around what the value they selected it. they are all numbers (the choices) and are stored in an Integer array. How do i set up that switch-case correctly? i tried doing a simple thing like
case ((Integer.parseInt(parent.getItemAtPosition(pos).toString())
So i figured it out. you need to make the array a STRING array and the use:
ArrayAdapter adapter = new ArrayAdapter
etc.....
then use
Integer.parseInt(parent.getItemAtPosition(position).toString());
to find the numerical value of whatever you selected. NOTE: it must be all numerical or it will give you an error.

Categories

Resources