android spinner onselectitemclick listener no triger - android

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
}

Related

onclick of 1st button activity it do not wait for 2nd spinner activity directly goes to 3rd activity

spCountries.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
switch (position)
{
case 0:
startActivity(new Intent(NagpurSem.this, ComputerWorkshop.class));
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I want it to wait until the user will select something from the spinner, but instead, it directly goes to the third activity.
Please help me.

How to know item selected on spinner?

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;
.......
}

using switch case statement for spinner to display images in android programming

I have a spinner which has a list of attractions. I want to use switch case statements to change the image displayed depending on which attraction the user selects.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attractions_layout);
ArrayAdapter<String> attractionsAdapter = new ArrayAdapter<String> (Attractions.this, android.R.layout.simple_spinner_item, attractionEntries);
attractionsSpinner = (Spinner) findViewById (R.id.spinnerAttractions);
attractionsSpinner.setAdapter(attractionsAdapter);
attractionsSpinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
// TODO Auto-generated method stub
int pos = attractionsSpinner.getSelectedItemPosition();
ImageView imageView = (ImageView) findViewById(R.id.imageViewAttraction);
String[] information = getResources().getStringArray(R.array.attractions_information_collection);
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
imageView.setImageResource(image);
break;
case 1:
image = R.drawable.disneyland_fountain;
imageView.setImageResource(image);
break;
}
}
i get an error message saying that "image cannot be resolved as a variable"
You don't have a type declared for image. Before you start the switch declare it
int image = 0;
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
imageView.setImageResource(R.drawable.avenue_of_stars;);
break;
case 1:
image = R.drawable.disneyland_fountain;
imageView.setImageResource(R.drawable.disneyland_fountain);
break;
}
}
Or just use the value for setImageResource()
switch (position)
{
case 0:
imageView.setImageResource(R.drawable.avenue_of_stars;);
break;
case 1:
imageView.setImageResource(R.drawable.disneyland_fountain);
break;
}
}
Or simplify it with
int image = 0;
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
break;
case 1:
image = R.drawable.disneyland_fountain;
break;
}
imageView.setImageResource(R.drawable.disneyland_fountain);
}
The error message is telling you that you have not declared a variable called "image" and thus it doesn't know what you are trying to do when you use that word.
Change your code to be like this:
private int image = -1; // <-- you have to declare a variable to be able to use it (in java).
private imageView; // <-- declare this ImageView up here too, while your at it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attractions_layout);
imageView = (ImageView) findViewById(R.id.imageViewAttraction); //Move findViewById() to here, calling it more than once is wasteful.
ArrayAdapter<String> attractionsAdapter = new ArrayAdapter<String> (Attractions.this, android.R.layout.simple_spinner_item, attractionEntries);
attractionsSpinner = (Spinner) findViewById (R.id.spinnerAttractions);
attractionsSpinner.setAdapter(attractionsAdapter);
attractionsSpinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
// TODO Auto-generated method stub
int pos = attractionsSpinner.getSelectedItemPosition();
String[] information = getResources().getStringArray(R.array.attractions_information_collection);
switch (position)
{
case 0:
image = R.drawable.avenue_of_stars;
imageView.setImageResource(image);
break;
case 1:
image = R.drawable.disneyland_fountain;
imageView.setImageResource(image);
break;
}
}
You must declare an int called image in order to be able to store an id into it.
Also note that calling findViewById() to get a reference to your image view every time the user selects something is wasteful, you should be getting your reference inside of onCreate() and then just using that reference for each subsequent call to setImageResource()
One last tip: It seems like you are lacking a grasp on some of the basics of Java programming and syntax. I strongly suggest you take some time now to go back and do some work familiarizing yourself with the java language a bit better before you dive into a complex Android project. Doing so will make your life much easier =).

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