Why didn't my toast work? - android

#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!

Related

spinner setOnItemSelectedListener does not working

anybody can help! the spinner listener doesn't work by this fragment.
it's never called
I have tried to implement an AdapterView.OnItemSelectedListener
I try to implement the spinner in Fragment I don't know if that really matter
Java code
'''
Spinner mSpinner = (Spinner) view.findViewById(R.id.usernameSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemSelected: " + position);
//its never called
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
'''

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" />

No respond from getSelectedItem Spinner

I have a Spinner with list of category, which get the data from JSON. I want to make, when an item other than All Category get selected, another spinner of subcategory become visible, and get loaded with data from JSON according to its category.
Previously I managed to do it but after left it for a few days and have other parts done, it no longer works.
Since the first one loaded into spinner is All Category, it triggered setOnItemSelectedListener() and hid subcategorySpinner. If other item get selected, subcategorySpinner still not shown.
Is there something wrong with the code logic? Please help.
categorySpinner = (Spinner) rootView.findViewById(R.id.categorySpinner);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.spinner_item, categoryList);
adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
categorySpinner.setAdapter(adapter2);
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
categoryString = categorySpinner.getSelectedItem().toString().replace(" ", "%20");
if (categorySpinner.getSelectedItem().toString().equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}
});
change your code to
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
categoryString = parent.getItemAtPosition(position).toString();
Log.d("debug", categoryString+"..."+position);
if (categoryString.equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
categoryString = categoryString.replace(" ", "%20")
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}
Try this code snippet
Spinner dropdown = (Spinner) findViewById(R.id.type);
final String[] items = new String[]{"SELECT TYPE OF OFFER","PERCENTAGE", "AMOUNT", "ITEM"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner_item, items);
dropdown.setAdapter(adapter);
String selectedItem = dropdown.getSelectedItem().toString();
try this
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
categoryString = parent.getItemAtPosition(position).toString().replace(" ", "%20");
if (categoryString.equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}
You can direcly use the, argument(position)
Instead of,
categoryString = parent.getItemAtPosition(position).toString();
You can use like,
categoryString = position.toString();
Well I don't quite get what I have done logically because it seems nothing much different but this solved it. Oh well.. whatever floats the boat, I guess.
Using suggestion by H Raval, I moved the if else into a method and now it works just like I wanted.
categorySpinner = (Spinner) rootView.findViewById(R.id.categorySpinner);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.spinner_item, categoryList);
adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
categorySpinner.setAdapter(adapter2);
String str = categorySpinner.getSelectedItem().toString();
Log.d("debug", "str "+str);
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
categoryString = parent.getItemAtPosition(position).toString();
Log.d("debug", categoryString+"..."+position);
subCategorySpinnerView(categoryString);
}
});
private void subCategorySpinnerView(String categoryString) {
if (categoryString.equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
categoryString = categoryString.replace(" ", "%20");
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}

How to retrieve String from a spinner using array?

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();

Categories

Resources