i want a spinner when select an item from the spinner its should load the corresponding java page that has been set.can we load a java page on selecting an item from spinner in android if so how can we implement this can any one provide some sample code
You can
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
switch(position) {
//Use cases to set Intents
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do Nothing
}
});
String[] items ={“One”,“Two”,“Three”,“Four”,“Five”};
Spinner sp = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item,items);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
//call first class
break;
case 1:
//call second class
break;
case 2:
//call third class
break;
case 3:
//call fourth class
break;
default:
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//nothing selected from spinner
}
});
just use spinner.onItemSelectedListener(new OnItemSelectedListener())
and in onItemSelected(AdapterView adapterview, View view, int position, long id) method body write your logic to start new activity based on the position.
Related
I want to display the list of my array in the spinner when the page first loads and also when the user touches on the spinner.
I am doing this :
String[] simpleArray = {"java","android","Data Structures","HTML","CSS"};
ArrayAdapter<String> usageListAdapter = new ArrayAdapter<String>(this, R.layout.spinner_small_text, simpleArray);
usageListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
btn_list_type.setAdapter(usageListAdapter);
btn_list_type.setBackgroundColor(0x00000000);
btn_list_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) {
listTypePickerSelected(pos);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I am trying to use a spinner, save the position of each selection and depend on this, another spinner with different selections for each position appears..
I made the first spinner with the help of the spinner docs but then I can't do something different for each selection.. Is there anyone who can help me?
Thank you!
You can use a switch statement in the onItemSelected() method:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
switch (pos) {
case 0:
//do action for first selection
break;
case 1:
//do action for second selection
break;
...
}
}
it is too simple to impliment:
try spinner ItemSelectedListener.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(position==0){}//do the function you want to perform
else if(position==1){}//And so on
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I have a listview in which i have 4 item and I have a class which contain some listner implementation what I want when I click item at poition 1 I want to add that listener class instance.how can I do that
code:-
m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position){
case 1:
MonetizationManager.createSession(getActivity(), "91707", this);
MonetizationManager.setRewardListener(getActivity());
}
}
});
here I want to set listener in specific index
case 0: will be first position of list you are you logic at case 1:
m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position){
case 0:
// First item Clicked
MonetizationManager.createSession(getActivity(), "91707", this);
etizationManager.setRewardListener(getActivity());
break;
case 1:
// 2nd item Clicked
MonetizationManager.createSession(getActivity(), "91707", this);
etizationManager.setRewardListener(getActivity());
break;
case 2:
// 3rd item Clicked
MonetizationManager.createSession(getActivity(), "91707", this);
etizationManager.setRewardListener(getActivity());
break;
case 3:
// 4th item Clicked
MonetizationManager.createSession(getActivity(), "91707", this);
etizationManager.setRewardListener(getActivity());
break;
}
}
});
Try this
1 First Way
m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position){
case 1:
// 2nd item Clicked
MonetizationManager.createSession(getActivity(), "91707", this);
etizationManager.setRewardListener(getActivity());
break;
}
}
});
2 Second Way
if(position==1)
{
m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// 2nd item Clicked
MonetizationManager.createSession(getActivity(), "91707", this);
etizationManager.setRewardListener(getActivity());
}
});
}
Please use this example i have perfect works
//first declare interface in adapter class on
customButtonListener customListner;
public interface customButtonListener {
public void onButtonClick(String song_id);
}
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}
//and use any view or button click
button.customListner.onButtonClick(int position)
//Adapter where use
adapter.setCustomButtonListner(this);
//Implement this method in activity
#Override
public void onButtonClick(int position)
{
}
i hope help this
If you want to add listener to the view which is in the position 1, then you may use
m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position){
case 1:
MonetizationManager.createSession(getActivity(), "91707", this);
MonetizationManager.setRewardListener(getActivity());
(here_put_your_view_type)view).setYourListner();
}
}
});
I have 2 arraylists, 1 is use to display the elements to the spinner and another one is use to display on a textview when one of the element from spinner is selected.
Example:
0---a---football
1---b---badminton
2---c---basketball
"a,b,c" are the elements in arraylist1; "football, badminton, basketball" are the elements in arraylist2; "0,1,2" are the index for both arraylists.The index of elements on both of the arraylists has already arranged properly as shown above.
What I want to do now is to let the spinner to display "a,b,c". When I select "b" in the spinner, the textview will show me "badminton".
What should I write in the onItemSelected of the spinner?Any idea for this?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
switch(position){
case 0:
textView1.setText(sportsList.get(0));
break;
case 1:
textView2.setText(sportsList.get(1));
break;
case 2:
textView3.setText(sportsList.get(2));
break;
}
}
});
You can get the selected item in the spinner by this code
String selected = spinner1.getText().toString();
Then check for the condition,
if(selected.equals("a")){
textview1.setText(array2.get(0));
}else if(selected.equals("b")){
......
try this,
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Textview your_tv = (Textview)findviewbyid(R.id.tv);
your_tv.setText(your_array[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
So I have buttons (not the soft key pad) on the screen but I want some to be disabled (can't click) when you pick a certain spinner option. Like I have the buttons 0-9 (for numeric input) and if "Base 2" (spinner selection 0) is picked I want all the buttons except 0 and 1 to be disabled.
You can add a OnItemClickListener and react to the option that is given like for example this way
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if(position == 1)
button.setClickable(false);
}
});
Spinner does not support setOnItemClickListener. If you try and use it, you will get an exception:
java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner
As such, you should use setOnItemSelectedListener:
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
Toast.makeText(MyActivity.this, "position= "+position+" / id= "+id, Toast.LENGTH_LONG).show();
switch(position) {
case 0:
button0.setClickable(true);
button1.setClickable(false);
break;
case 1:
button0.setClickable(false);
button1.setClickable(true);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});