How to know item selected on spinner? - android

I have the following code:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " +item.toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(this, "You didn't select any preset", Toast.LENGTH_SHORT).show();
}
And the following array:
<string-array name="presets_array">
<item>Light Car</item>
<item>Medium Car</item>
<item>Heavy Car</item>
<item>Van</item>
</string-array>
It's working fine, when I select Light car it displays the correct text and so on with the others.
But I want to do more things, if selected thing is Van, change weight value, if medium car, other cost, etc.
I think I have to use a switch but I'm not sure how it works.

There are some options. You should choose the best which fits to you...
Just checking position
Good performance but if you change array order, you must re-order the switch statement
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " + item.toString(), Toast.LENGTH_SHORT).show();
swith(position) {
case 0:
// CODE for Light Car
break;
case 1:
// CODE for Medium Car
break;
case 2:
// CODE for Heavy Car
break;
case 3:
// CODE for Van
break;
}
}
String Checking
Low performance since you have to compare Strings several times in the worst case.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
Toast.makeText(this, "You selected " + item.toString(), Toast.LENGTH_SHORT).show();
if(item.toString().equals("Light Car")) {
// CODE
} else if(item.toString().equals("Medium Car")) {
// CODE
} else if(item.toString().equals("Heavy Car")) {
// CODE
} else if(item.toString().equals("Van")) {
// CODE
}
}

you can write code similar to this code inside onItemSelected method:
switch (item)
{
case Light Car:
// TODD
break;
case Van:
// TODO
break;
.......
}

Related

FirebaseTranslatorOptions.Builder object is not taking input from dropdownmenu (Spinner) Firebase Translate text in Android

I have developed an App in Android which translates text (from EditText), Application uses Firebase API. it translates successfully when I select Source and Target Language in hard code. (FirebaseTranslatorOption's setSource and setTarget language settings even integer code i.e. EN (11), FR(17) etc. but it is not taking any dynamic input from Spinner object's setOnItemSelectedListener.
'''
//Spinners itemselection events
input_lang_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
selected_source_lang = parent.getItemAtPosition(position).toString();
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
case 1:
SourceLangCode=11;
break;
case 2:
SourceLangCode=17;
break;
case 3:
SourceLangCode=56;
break;
case 4:
SourceLangCode=48;
break;
case 5:
SourceLangCode=9;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
}
});
//end of input language selection
output_lang_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
selected_target_lang = parent.getItemAtPosition(position).toString();
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
case 1:
targetLangCode=11;
break;
case 2:
targetLangCode=17;
break;
case 3:
targetLangCode=56;
break;
case 4:
targetLangCode=48;
break;
case 5:
targetLangCode=9;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
}
});//end of output language selection
//firebaseTranslatorOptions object
FirebaseTranslatorOptions options =
new FirebaseTranslatorOptions.Builder()
.setSourceLanguage(SourceLangCode) //this is not taken as I select item
.setTargetLanguage(targetLangCode) //item selection is working
.build();
final FirebaseTranslator languageTranslator =
FirebaseNaturalLanguage.getInstance().getTranslator(options);
'''

Get which chart is selected on activity

I am using latest release of mpandroidchart library. I have 2 bar charts on single activity. chart1 & chart2 are the id's in XML (I don't want to use barchart list view). chart1 cosnist Counts value & chart2 consist dollars value. I am getting the values already. But I want to know that is it a dollar value or counts value. so I can display the toast according to chart selected.
This is my Sample code.
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
View view;
TextView text;
switch (e.getXIndex()) {
case 0:
if (toast != null)
toast.cancel();
toast = Toast.makeText(getActivity(), "All Other Year Defectors: " +e.getVal(), Toast.LENGTH_SHORT);
view = toast.getView();
view.setBackgroundResource(R.color.all_odr_yr);
toast.setGravity(Gravity.TOP, 0, 950);
toast.show();
break;
case 1:
if (toast != null)
toast.cancel();
toast = Toast.makeText(getActivity(), "Last Year Defectors: " + e.getVal(), Toast.LENGTH_SHORT);
view = toast.getView();
view.setBackgroundResource(R.color.lst_yr_df);
toast.setGravity(Gravity.TOP, 0, 950);
toast.show();
break;
That seems to be quite difficult and hard to acheive with the library alone.
But what you could do is inline the listeners and use a separate listener for each chart, like this:
countChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// COUNT CHART VALUE SELECTED
}
#Override
public void onNothingSelected() { }
});
dollarChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// DOLLAR CHART VALUE SELECTED
}
#Override
public void onNothingSelected() { }
});
In that way you can differentiate between the different charts.
If you do not want to use inline listeners as Philip mentioned in his answer you can create a class implementing onChartValueSelectedListener and identify each chart with an ID.
private class CustomOnValueSelectedListener implements OnChartValueSelectedListener {
private int CHART_ID;
public CustomOnValueSelectedListener() {}
public CustomOnValueSelectedListener(int chart_id) {
CHART_ID = chart_id;
}
#Override
public void onValueSelected(Entry e, Highlight h) {
switch (CHART_ID) {
case PIE_CHART_ID:
break;
case BAR_CHART_ID:
break;
case LINE_CHART_ID:
break;
default:
//common code
break;
}
}
#Override
public void onNothingSelected() {
}
}
You can now do this -
pieChart.setOnChartValueSelectedListener(new CustomOnValueSelectedListener(PIE_ID));
barChart.setOnChartValueSelectedListener(new CustomOnValueSelectedListener(BAR_ID));
lineChart.setOnChartValueSelectedListener(new CustomOnValueSelectedListener(LINE_ID));
someOtherChart.setOnChartValueSelectedListener(new CustomValueSelectedListener());
where PIE_ID, LINE_ID and BAR_ID are some unique integers.
This way your code is concise in case you have more than 3-4 charts to deal with else inline listeners are better.

android spinner onselectitemclick listener no triger

This is how i define the spinner
s_province = (Spinner) findViewById(R.id.s_province);
ArrayAdapter<String> provinceAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Data.provinces);
provinceAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_province.setAdapter(provinceAdapter);
s_province.setOnItemSelectedListener(this);
my class is implement from OnItemSelectedListener and i override this methods
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch (arg1.getId()) {
case R.id.s_province:
Log.d("here", "there");
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
but the onItemSelect is not triged , why please?
Two things:
If you actually want to check if your method is working put a Log statement outside the switch or in the default case so that you can be sure the method is being called.
You should be using arg2 since that represents position. Make your switch work with positions instead of whatever View is passed. Also rename your variables from the default names Eclipse assigns. arg0,1,2, etc are not helpful for you nor for anyone else looking at your code.
Eg
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(view.getContext(),"onItemSelected called", Toast.LENGTH_LONG).show();
int spinnerId = parent.getId();
if (spinnerId == R.id.s_province)
{
switch (position)
{
case 0:
//do something if first position was clicked
break:
case 1:
//do something else
break;
default:
//if for any reason no position matches.
break;
}
}
else if (spinnerId == R.other_id_in_xml)
{
switch (position)
{
case 0:
//do something if first position was clicked
break:
case 1:
//do something else
break;
default:
//if for any reason no position matches.
break;
}
}
//etc
}

How to detect on what ListView onItemClick was happened?

In Activity I have two ListViews but now I must to detect on what ListView user clicked.
I have added adapters and setOnItemClickListener(this); for each ListView.
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
switch (v.getId()) {
case R.id.list_1:
Toast.makeText(this, "111111111", 0).show();
break;
case R.id.list_2:
Toast.makeText(this, "222222222", 0).show();
break;
}
}
But v.getId() returns -1
use a.getId() instead of v.getId()
i mean use AdapterView<?> a
switch (a.getId()) {
case R.id.list_1:
Toast.makeText(this, "111111111", 0).show();
break;
case R.id.list_2:
Toast.makeText(this, "222222222", 0).show();
break;
}
in onCreate(), get references to your listviews:
listview1 = findViewById(R.id.list_1);
listview2 = findViewById(R.id.list_2);
then, in onItemClicked() you can test like this:
if (a == listview1){
//something
}
else if(a==listview2){
//something
}

regarding showing different view on each item click in android application

I am developing an android application in which i have 5 items in a listview,I have to show different view on each click at the item,,,How to do this,also i have to put an arrow on the left and right side of the each item,,,
Here is my listview java code
http://pastebin.com/LsunQU9z
and here is my listview xml
http://pastebin.com/1S4uD5mH
Thanks in advance
Tushar
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
switch(position)
case 0:
//write for first item
break;
case 1:
//second
break;
case 2:
//third
break;
case 3:
//for
break;
}
});
Well it all depends on what you are going to do or show in those 5 items.
One way would be to only use one class, let's call it ShowActivity and then pass extras to that activity to see what it should show. And then just fetch that in onCreate in the ShowActivity.
One way would just to show it in the activity that you are in.
Two different examples are below:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Intent intent = new Intent(this,ShowActivity.class);
intent.putExtra("ITEM_INDEX",position);
startActivity(intent);
}
});
With the one above use something similar in the onCreate # ShowActivity
int id = getIntent().getIntExtra("ITEM_INDEX", 0);
switch(id) {
case 0:
//Show item 0
break;
case 1:
//Show item 1
break;
case 2:
//Show item 2
break;
case 3:
//Show item 3
break;
case 4:
//show item 4
break;
}
This is the second example, use this in your list activity.
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
switch(position) {
case 0:
//Show item 0
break;
case 1:
//Show item 1
break;
case 2:
//Show item 2
break;
case 3:
//Show item 3
break;
case 4:
//show item 4
break;
}
}
});

Categories

Resources