I have dailog with a EditText and a Spinner, i am implementing as below
spinner1 = (Spinner) dialog.findViewById(R.id.quesspinner);
ArrayAdapter<CharSequence> arrayadapter = ArrayAdapter.createFromResource(activity, R.array.fbquestion,R.layout.textview);
spinner1.setAdapter(arrayadapter);
logo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
spinner1.setVisibility(View.GONE);
spinner1.performClick();
}
});
Now i am unable to read the value of Spinner, i want the text in the Spinner to be displayed in my EditText . code seems to be fine for me but itz not working.Any help is appreciated
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
int Text = parent.getSelectedItemPosition();
edittext.setText(Text);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use spinner.getSelectedItem() instead of parent.getSelectedItem()
Change your code to like this
To show content in EditText before changing the spinner then do like this
spinner1.setAdapter(arrayadapter);
edittext.setText(spinner1.getSelectedItem().toString());
.
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String Text = spinner1.getSelectedItem().toString();
edittext.setText(Text);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Related
Im using a spinner and i was wondering how can you get an item from the spinner to appear in an image button the images are using string as value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_up);
spinnerListener();
St = (Spinner) findViewById(R.id.Namess);
imageButton01 = (ImageButton) findViewById(R.id.ImageButton1);
}
private void spinnerListener() {
ArrayList<Name> players = List_content.ENTRY_LIST_PLAYERS;
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(Line_up.this, android.R.layout.simple_spinner_item);
{for (Name p : players){adapter.add(p.f_name);}};
playerss.setAdapter(adapter);
playerss.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> av, View v,
int position, long itemId) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void goalkeeper(View v){
st.performClick();
}
OK. Here's the trick:
When ImageButton is clicked, you just need to call performClick() on the spinner object.
int[] pictureIds = {
R.drawable.picture1,
//add as many picture IDs as you want
R.drawable.picture2
}
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinner.performClick();
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
imageButton.setImageResource(pictureIds[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Don't forget to set spinner adapter just as you did correctly in your question.
I have open a spinner on button click.
Now when I select an item of spinner, my setOnItemSelectedListener is not fired. Only the spinner gets closed on selecting an item.
The code is below:-
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Spinner spinner = new Spinner(activity);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(activity,"Selected",Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
final CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(
activity);
spinner.setAdapter(adapter);
spinner.performClick();
}
});
I think problem in line
final Spinner spinner = new Spinner(activity);
Our spinner have modifer "final", try to declare our spinner as a field.
Example:
holder.button.setOnClickListener(new OnClickListener() {
Spinner spinner;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
spinner = new Spinner(activity);
...
}
});
I have issue in changing Spinner text colour. I am using the following code to change Text Colour. But I don't know why it's throwing a NullPointerException at setTextColor(). I am fetching items of the spinner from a database dynamically.
public class PersonalInformation extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info)
ArrayAdapter<String> adapter8 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item,arrayList);
adapter8.setDropDownViewResource(R.layout.spinner_view);
mySpinner.setAdapter(adapter8);
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
((TextView) arg0.getChildAt(arg2))
.setTextColor(Color.BLACK);
onulocation = arg0.getItemAtPosition(arg2)
.toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
}
this may help you...
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
TextView textView = (TextView) view;
textView.setTextColor(Color.BLACK);
onulocation = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});
}
public void valide_button() {
valideButton = (Button) findViewById(R.id.valide_button);
valideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestProjectActivity.this,
MetroMapActivity.class);
startActivity(intent);
}
});
}
}
The way to get the selection of the spinner is:
spinner1.getSelectedItemPosition();
Documentation reference:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.
So you could change that line to:
TestProjectActivity.this.number = position + 1;
If that does not fix the problem, please post the error message generated when your app crashes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.button);
spinner = findViewById(R.id.sp_item);
setInfo();
spinnerAdapter = new SpinnerAdapter(this, arrayList);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//first, we have to retrieve the item position as a string
// then, we can change string value into integer
String item_position = String.valueOf(position);
int positonInt = Integer.valueOf(item_position);
Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
note: the position of items is counted from 0.
final int[] positions=new int[2];
Spinner sp=findViewByID(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText( arg2....);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if (position ==0) {
if (rYes.isChecked()) {
Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
} else if (rNo.isChecked()) {
Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
}
}
This code is supposed to select both check boxes.
Is there a problem with it?
I wanted to ask how can one use both spinner and button on the same activity. Spinner listens with its onItemListener and then the button would have an onClick listener too. So in my case, it generates an error. My scenario is that I get the selected string from the spinner and then the rest of the values from editTexts and then hit "submit" to send data to the server. But I reckon these two listeners aren't very friendly with each other?
I set up these methods for the spinner right:
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
}
});
Then I need to put the onclick listener for the button:
go.setOnClickListener(new OnClickListener(){
public void onClick(View arg0)
{
}
Where do I put this one? Before the nothingSelected method or after that?
I think they are very friendly with each other :)
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
}
});
go.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// your code here
}
});
public class StackOverflowActivity extends Activity {
private static final String[] SPINNER_DATA = new String[] { "Item 1", "Item 2" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spn = (Spinner) findViewById(R.id.spinner1);
spn.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, SPINNER_DATA));
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Spinner.onItemSelected()", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"Spinner.onNothingSelected()", Toast.LENGTH_LONG)
.show();
}
});
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button.onClick()",
Toast.LENGTH_LONG).show();
}
});
}
}