Spinner with on Click Listener - java

I am using spinner that shows error when i am trying to extract the item id of the selected spinner item.
My Code goes here:
public void dispspi()
{
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String> adap= new ArrayAdapter(this, android.R.layout.simple_spinner_item, level);
spinner.setAdapter(adap);
spinner.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int item = spinner.getSelectedItemPosition();
p=item;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
How to get the item id of the spinner? Any help is appreciated..Thanks in advance

IIRC, you should be using a selected listener, not click:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
Then you can add the override tag to your selected method.

private String selecteditem;
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
selecteditem = adapter.getItemAtPosition(i).toString();
//or this can be also right: selecteditem = level[i];
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});

spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Yes you can use some OnItemSelectedListener for work with selected item. But sometimes we would like to handle exactly click for spinner. For example hide keyboard or send some analytics etc. In this case we should use TouchListener because OnClickListener doesn't work properly with Spinner and you can get error. So I suggest to use TouchListener like:
someSpinner.setOnTouchListener { _, event -> onTouchSomeSpinner(event)}
fun onTouchSomeSpinner(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_UP) {
view.hideKeyBoard()
...
}
return false
}

you should have this in the listener(OnItemSelectedListener)
public void onNothingSelected(AdapterView<?> arg0) {
}
It might works without it but put it to be consistent
but there might be other errors also, can you provide the error log ?

Related

Set the imageView src based on the value selected in a Spinner

I've been trying to set an ImageView (Default empty) based on the value selected from a Spinner.
So in the mainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp_home = (Spinner) findViewById(R.id.spinner_home_team);
sp_away = (Spinner) findViewById(R.id.spinner_away_team);
sp_home.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (!sp_home.getSelectedItem().toString().equals("Seleziona Squadra")) {
setTeamLogo(sp_home.getSelectedItem().toString(), "home");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp_away.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (!sp_away.getSelectedItem().toString().equals("Seleziona Squadra")) {
setTeamLogo(sp_away.getSelectedItem().toString(), "away");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
The setOnItemSelectedListener method works perfectly the problem comes when I execute the code inside setTeamLogo.
private void setTeamLogo(String teamName, String home_or_away_team){
int resId = getResources().getIdentifier(teamName, "drawable", getPackageName());
Toast.makeText(getBaseContext(), resId,
Toast.LENGTH_LONG).show();
}
The exception that has been thrown is:
android.content.res.Resources$NotFoundException: String resource ID #0x0
Any idea why if I use the name of the image instead of the variable teamName everything works perfectly?
Use String.valueOf()
If you pass it an integer it will try to look for the corresponding string resource id - which it can't find, which is your error.
check out this post:-
android.content.res.Resources$NotFoundException: String resource ID #0x0

Click on spinner element android

I created a spinner in my main.xml:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/categoria_arrays"
android:prompt="#string/categoria_prompt" />
And in the strings.xml the values:
<string name="categoria_prompt">Choose</string>
<string-array name="categoria_arrays">
<item>All</item>
<item>One</item>
<item>Two</item>
<item>Three</item>
</string-array>
I can display it normally but actually there are no interactions.. I need that onClick over a item open a new activity for example. So if i click the item at position 2 i need go in the activity One. Is it possible?
I tried to create a toast when i click a item but not works:
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " +
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
I How could i do?
Simply call something like this in your onCreate after your setContentView:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new 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();
//HERE CHANGE ACTIVITY
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes it is possible. Do it like this
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
if (pos == 1){
Intent i = new Intent(currentActivity.this, One.class);
startActivity(i);
}else if (pos == 2)
{
Intent i = new Intent(currentActivity.this, Two.class);
startActivity(i);
}else if (pos == 3){
Intent i = new Intent(currentActivity.this, Three.class);
startActivity(i);
}
}
and so on...
You need to set item selected listener like this:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
You need to explicitly set the listener for the spinner.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
// your code here
}
public void onNothingSelected(AdapterView<?> arg0) {
// your code here
}
});
Did you just added onItemSelected method, or setted onItemSelectListener? onItemSelected and onNothingSelected are just methods, you need to use onItemSelectListener.

onItemLongClick is undefined?

I'm trying to implement a long click in my listview item but it doesn't work and i get an error that says is undefined. Here's the code:
protected void setOnItemLongClickListener(ListView l, View v, int position, long id) {
super.onItemLongClick(l, v, position, id);// Error
ApplicationInfo app = applist.get(position);
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
Someone has an idea how solve the problem?Thanks
The reason for this is most likely that you don't implement the listener. Something like
public class ActivityName extends Activity implements OnItemLongClickListener{
Try changing
protected void setOnItemLongClickListener
to
protected boolean setOnItemLongClickListener{
// your code
return true;
You need to use the proper return type for the method which is boolean then return true so the listener knows it was a success.
Docs
try this listener for Listview :
istView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(arg0.getContext(), ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
return false;
}
});
use this code
yourListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//YOUR_CODE_HERE
return false;
}
});
Please replace
public class MainActivity extends Activity implements OnItemLongClickListener
and add unimplemented method
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
by default you can do this by right clicking on OnItemLongClickListener select Quick fix
try to add this lines to your list adapter
view.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
and the method is try to overwrite your method
#Override
public boolean onItemLongClick(
AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
return false;
}

spinner that trigger another spinenr

mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// my code here, but I don't know how..
}
}
I think it will be something like this:
get the selected item position of mySpinner
switch(pos)
Update spinner entries? I don't know...
I'm not sure what to do for the last step. Help?
try in this way
car.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
itemSelected = arg2;
// add items to the cardetails spinner's adapter using the itemselected and refresh the adapter using nofifyDataSetChanged()
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
cardetails.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
here car and cardetails are spinners
you need to refresh the adapter that is related to the car spinner so that the spinner also gets refreshed

how to make radio button disappear on item select from spinner

rb1=(RadioButton)findViewById(R.id.radioButton1);
rb2=(RadioButton)findViewById(R.id.radioButton2);
rb3=(RadioButton)findViewById(R.id.radioButton3);
rb4=(RadioButton)findViewById(R.id.radioButton4);
rb5=(RadioButton)findViewById(R.id.radioButton5);
spiner1=(Spinner)findViewById(R.id.spinner1);
spiner2=(Spinner)findViewById(R.id.spinner1);
spiner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
This is my code there are 5 value in my spiner if i select first one then rb1,rb2,rb3,rb5 shud display and rest disappear if i select 2,3,4,5 value from spinner then it hud display 1st,4,5 plz tell me how to write code for this i m new in android try to implement some apps. i need code where and how to apply logic for this.
There are some demos about spinner
Here is an example.
you can set dropdown styles in this method
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Based on your condition or logic you can show/hide buttons.
rb1.setVisibility(View.VISIBLE); // show
rb2.setVisibility(View.INVISIBLE); // Hide
You can do this :
spiner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adView, View view,int pos, long id) {
selectedoption = adapter.get(pos);
if(selectedoption == rb1)
{
rb1.setVisibility(View.INVISIBLE);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Categories