I create a Spinner depending on some rules of my app. So, its created dynamically. How can I retrieve which item was selected?
Spinner mySpinner = (Spinner)linearMoreInfo.getChildAt(1);
Using this code to retrieve the created dynamically spinner. But got no idea of how I can handle its click/select item.
Any helps?
I hope it works for you:
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Object obj = parent.getItemAtPosition(pos);
...
}
public void onNothingSelected(AdapterView<?> parent) { }
});
Try this Code...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
selected_item_position = pos;
selected_item = spinner.getSelectedItem().toString();
// You can insert your code...
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Related
I have 8 spinners:
pl1sp = (Spinner) offersdialoglayout.findViewById(R.id.pl1offer);
pl2sp = (Spinner) offersdialoglayout.findViewById(R.id.pl2offer);
pl3sp = (Spinner) offersdialoglayout.findViewById(R.id.pl3offer);
pl4sp = (Spinner) offersdialoglayout.findViewById(R.id.pl4offer);
and
pl1scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl1scores);
pl2scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl2scores);
pl3scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl3scores);
pl4scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl4scores);
I need to set them 2 listeners. One for first 4 spinner and one for last 4. I don't want to repeat code like this:
pl1sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum(); // same method for first 4 spinners. for last 4 spinners is checkScoresSum()
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
pl2sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
and so on..
So I need to write listener only twice, first for first 4 spinners and second for last 4 spinners. Thanks
define one listener for first 4 spinner
Spinner[] offerspinners = {R.id.pl1offer, R.id.pl2offer, R.id.pl3offer, R.id.pl4offer};
for(Spinner offer : offerspinners){
offersdialoglayout.findViewById(offer).
setOnItemSelectedListener(offerListener);
}
AdapterView.OnItemSelectedListener offerListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1offer:
// offer 1 related code
break;
case R.id.pl2offer:
// offer 2 related code
break;
}
// common code
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
than define 2nd listener with same code here
Spinner[] scorespinners = {R.id.pl1scores, R.id.pl2scores, R.id.pl3scores, R.id.pl4scores};
for(Spinner score : scorespinners){
scoresdialoglayout.findViewById(score).
setOnItemSelectedListener(scoreListener);
}
AdapterView.OnItemSelectedListener scoreListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1scores:
// score 1 related code
break;
case R.id.pl2scores:
// score 2 related code
break;
}
// common code
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
You can manage multiple spinners using a single listener. Please check this answer. It deals with the same issue. Hope it helps.
Follow these steps:
Implement OnItemSelectedListener in your class and override the unimplemented method onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3).
use pl1sp.setOnItemSelectedListener(this) and do this for all spinners.
Use the overridenmethod onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) like this:
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
int id = view.getId();
if(id==R.id.pl1offer || id==R.id.pl2offer || id==R.id.pl3offer ||id==R.id.pl4offer){
// do this
}
else{
//do this
}
}
What you can do is create an array containing all your spinners then use a for loop to attach each individual spinner to setItemSelected() method ie
Spinner[] spinner = {pl1sp, pl2sp, pl3sp, pl4sp};
for(Spinner individualSpinner : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Custom method here
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
Spinner[] spinners = {pl1scores, pl2scores, pl3scores, pl4scores};
for(Spinner individualSpinner2 : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Custom method here
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
I have 3 spinners in my xml..
1. one is Product Name
2. one is Product Quantity.
3. and 3rd one is Discount
Now, i calling separate Listener for each one.. the problem is i want to use item quantity to calculate final price and i called spinner in all in same sequence which is i wrote Name,Quantity and Discount..
The listener of Quantity is called at last even though i wrote above Discount Spinner..i don't know what's happening..can anybody ever face this problem...Please help me..
This is my Spinner Listener:-
spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
strItem=spnSelectItem.getItemAtPosition(arg2).toString(); }
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
///This method called last..
spnQty.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
strQty=spnQty.getItemAtPosition(arg2).toString();
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
///This method called before spnQty ..
spnItemDiscount.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
disc=round(Double.parseDouble(spnItemDiscount.getItemAtPosition(arg2).toString().replace("%", "")));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Why not create a separate method to perform all calculation like this:
private void calculatePrice() {
String strItem = spnSelectItem.getSelectedItem().toString();
String qty = spnQty.getSelectedItem().toString();
String discount = spnItemDiscount.getSelectedItem().toString().replace("%", "");
//perform calculate and update UI as you like
}
and then either make a single listener for all of three spinners or call them in all of them, e.g.:
spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//do some work, update UI or reset qty and discount spinner or whatever
strItem = spnSelectItem.getItemAtPosition(arg2).toString();
//call calulcate method
calculatePrice();
}
});
Hy,i need to have an onclick on a spinner.
This is my spinner:
Spinner spinner = (Spinner) findViewById(R.id.spnTarghe);
final String targhe[]=risposta.split("/");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
targhe
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
How can I do for use an onclicklistener that do this simple istruction:
sTarga=targhe[position];
By using onitemselected listener you can select item in spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String mselection=spinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "selected "+ mselection, 30).show();
/**** do your code*****/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//
}
});
use onItemSelected
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
sTarga=targhe[position];
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/
but I can't seem to add an onclicklistener
I just want to be able to do an action when I click on the row, with the data that the row contains of course
any ideas?
thanks
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Setup the list view
final ListView prestListView = (ListView) findViewById(R.id.list);
final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);
prestListView.setAdapter(prestationAdapterEco);
// Populate the list, through the adapter
for(final prestationEco entry : getPrestations()) {
prestationAdapterEco.add(entry);
}
prestListView.setClickable(true);
prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});
If your Activity extends ListActivity, you can simply override the OnListItemClick() method like so:
/** {#inheritDoc} */
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
// TODO : Logic
}
The prestListView.getItemAtPosition(position); returns the UI widget: Text, Icon, ...
Try this instead:
Object o = prestationAdapterEco.getItemAtPosition(position);
or
Object o = arg0.getItemAtPosition(position);
Get the object from the adapter. Not from the list-view.
2.
Object o is a prestationEco object. Not a String.
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You are doing
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
The o that you get back is not a String, but a prestationEco so you get a CCE when doing the (String)o
Try this:
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
How to get a selected item from a spinner in android?
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
}
});
Note:
The documentation for the Spinner widget clearly states:
A spinner does not support item click events. Calling this
method will raise an exception.
Better use OnItemSelectedListener() instead, as above.
In your Activity:
mSpinner = (Spinner) findViewById(R.id.my_spinner);
// Define your adapter, in my case I have a class (SpinnerData) so I get an ID instead of the spinner's text
ArrayAdapter<SpinnerData> myadapter = new ArrayAdapter<SpinnerData>(
this, android.R.layout.simple_spinner_item, new SpinnerData [] {
new SpinnerData("1","Item 1"),
new SpinnerData("2","Item 2")
});
myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(myadapter);
mSpinner.setOnItemSelectedListener(this);
This is useful if you have other elements in the form that trigger the onItemSelected event:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id{
SpinnerData sd;
switch (parent.getId()) {
case R.id.my_spinner:
sd = (SpinnerData)mSpinner.getSelectedItem();
// do something with sd.id
break;
...
}
As reference, the SpinnerData class:
public class SpinnerData {
public String id;
public String name;
public SpinnerData(String _id, String _name) {
this.id = _id;
this.name = _name;
}
public String toString() {
return this.name;
}
}
Hope it helps.
Try this Code..
sp2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long id) {
// TODO Auto-generated method stub
int selected_item_position = arg2;
String selected_item=sp2.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});