having a little problem getting the information from my spinner, this is how i have it set up in my activity
color = (Spinner)findViewById(R.id.ledColor);
vibrate = (Spinner)findViewById(R.id.vibPattern);
populateSpinners();
color.setOnItemSelectedListener(new colorSelected());
vibrate.setOnItemSelectedListener(new vibrateSelected());
public void populateSpinners(){
ArrayAdapter<CharSequence> cAdapter;
cAdapter = ArrayAdapter.createFromResource(this, R.array.colors,android.R.layout.simple_spinner_item);
int cSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
cAdapter.setDropDownViewResource(cSpinnerDD);
color.setAdapter(cAdapter);
ArrayAdapter<CharSequence> vAdapter;
vAdapter = ArrayAdapter.createFromResource(this, R.array.vibrate, android.R.layout.simple_spinner_item);
int vSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
vAdapter.setDropDownViewResource(vSpinnerDD);
vibrate.setAdapter(vAdapter);
}
public class colorSelected implements OnItemSelectedListener{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
arg0.getItemAtPosition(arg2);
nColor = arg0.toString();
Log.v("EditContact",nColor);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
/********************************************************************************************/
public class vibrateSelected implements OnItemSelectedListener{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
arg0.getItemAtPosition(arg2);
nVibrate = arg0.toString();
Log.v("EditContact",nVibrate);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
but it does not get the selection from the spinner what am i doing wrong?
create a public method like this :
public String returnString(AdapterView<?> parent,int position,long ID){
return parent.getSelectedItem().toString();
}
and put it in your onItemSelected method :
String yourtext = returnString(arg0, arg2, arg3);
Get spinner selected value by using below code..
String value=spinner.getSelectedItem().toString();
Related
I have a spinner in my code and I want to get position of element in the array when selected. Here is my code which is running perfectly. In selection I am storing the string value of element but I also want the position count of element
public class MainActivity extends Activity implements OnItemSelectedListener {
final Context context = this;
private Button button;
private String selection;
private String[] states = new String[]{
"Gujrat","Jammu and Kashmir","Kerala","Karnataka","Lakshadweep","Maharashtra","Manipur","Mizoram",
"Nagaland","New Delhi","Rajasthan","Tami Nadu","West Bengal"
};
ArrayList<String> categoryList = new ArrayList<String>();
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
//make it fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//fix portrait orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.menu);
Button dialogButton = (Button) dialog.findViewById(R.id.btncross);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, states);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_state);
spin.setOnItemSelectedListener(MainActivity.this);
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
In the code:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
arg2 is the position of spinner
int arg2 of `onItemSelected()` is the position of selected items.
So you can use it like
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selection = states[arg2];
Log.v(TAG, "index=" + arg2);
Log.v(TAG, "selction=" + selection);
}
spin.getSelectedItemPosition();
Here in your setOnItemSelectedListener(new OnItemSelectedListener()
public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id)
Parameters
parent The AdapterView where the selection happened
view The view within the AdapterView that was clicked
position The position of the view in the adapter
id The row id of the item that is selected
int arg2 to get Selected Item Id
Update only this method,
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.v(TAG, "index=" + spin.getItemIdAtPosition(int2));
}
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.
}
});
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
}
});