Please, someone, tell me my mistake in the attached code of spinner. It crashes the application
String arr[] = {"Maths", "Biology"};
ArrayAdapter a = new ArrayAdapter(this,android.R.layout.simple_spinner_item, arr);
Spinner spin = (Spinner) findViewById(R.id.spin1);
spin.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
spin.setAdapter(a);
You cannot cast activity for AdapterView.OnItemSelectedListener
spin.setAdapter(a);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Get position and the related item from here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Another interface callback
}
});
Related
The following code is my Spinner init code , But when it's item selected, The callback listener don't call
private void showSelectDialog(final List datas, final AdapterView.OnItemSelectedListener onItemClickListener) {
if (EmptyUtil.isCollectionNotEmpty(datas) || datas.size() > 1) {
final Spinner spinner = new Spinner(this);
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.spinner_item, datas);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setDropDownHorizontalOffset(DeviceUtil.getDeviceWidth(this) / 2);
spinner.setDropDownVerticalOffset(DeviceUtil.getDeviceHeight(this) / 2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
ToastTool.showToast(PlaceanOrderActivity.this,"onItemSelected");
onItemClickListener.onItemSelected(adapterView,view,i,l);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
onItemClickListener.onNothingSelected(adapterView);
}
});
spinner.performClick();
}
}
Who has ideas for this problem, Thanks for first!
My target is to show part of selected spinner item string, as selected item. In Spinner options I want show full string, but when selected only first part of it. Example:
Selected: "Racing Car" > show: "Racing",
Selected: "Fast car" > show: "Fast"
Does that possible? My code:
private void defineRacingCarsArray(){
racingCarsArray = Arrays.asList(
"Racing Car",
"Fast Car"
);
}
private void defineRacingCarsCodeSpinner(){
racingCarsCodeSpinner = (Spinner) view.findViewById(R.id.cars_code_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < racingCarsArray.size(); ++i) {
adapter.add(racingCarsArray.get(i));
}
racingCarsCodeSpinner.setAdapter(adapter);
racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position);
String car = racingCarsArray.get(position);
;
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Try this,
racingCarsCodeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// here you have to update
TextView selectedView = (TextView) view;
selectedView.setText(racingCarsArray.get(position).split(" ",2)[0]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I'm new to Android and I'm trying to get a String from spinner. I've made some research but I couldn't find anything useful. Without trying to get String the code is working properly. This is the code which is working:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I tried to use a getter method as follows where I didn't get any errors:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I'm trying to set this String to a text view using:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivityClassName AnObject = new MainActivityClassName();
text.setText(AnObject.getSpinnerString());
Where I get an error saying "cannot resolve method getSpin()". I do realise I'm not passing any parameters to the method but I don't know how I can do it here. I appreciate any help or any other advice to solve the problem in a different way. Thanks in advance.
This is the full code if that helps:
public class MainActivity extends AppCompatActivity {
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
text = (TextView) findViewById(R.id.textView);
// spinner 1
final Spinner staticSpinner1 = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter1 = ArrayAdapter
.createFromResource(this, R.array.lang,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner1.setAdapter(staticAdapter1);
// spinner 2
final Spinner staticSpinner2 = (Spinner) findViewById(R.id.spinner2);
staticSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String spinnerLanguage = staticSpinner1.getSelectedItem().toString();
switch (spinnerLanguage) {
case "Afrikaans":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterAF = ArrayAdapter
.createFromResource(getBaseContext(), R.array.af,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterAF
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterAF);
break;
case "French":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterFR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.fr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterFR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterFR);
break;
case "English":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterEN = ArrayAdapter
.createFromResource(getBaseContext(), R.array.en,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterEN
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterEN);
break;
case "Turkish":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterTR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.tr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterTR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterTR);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivity AnObject = new MainActivity();
text.setText(AnObject.getSpinnerString());
}
}
You can do this:
String selectedText = null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedText = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Now you can use the selectedText variable to pass on to whatever method you want. But, keep in mind, unless you select something in the spinner, it's value would be null.
If you really have to use it outer, you can do this :
public void setTextView(String text){
text.setText(text);
}
and call this method inside your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextView(staticSpinner2.getSelectedItem().toString());
}
Having read that piece of code, why don't you extract "spinnerLanguage2" to your main class and then access it from there? Assigning the value inside the OnItemSelected method.
EDIT:
My previous answer is wrong. Try to implement an interface in order to communicate with the other class. Thus, calling a method of that interface you can pass the value. I give you a link with this (It explains it for fragments, but yo can communicate your classes too):
http://developer.android.com/intl/es/training/basics/fragments/communicating.html
as demonstrated here by jalopaba, I already created a new class: How do you get the selected value of a Spinner?
public class MyItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
and registering this to the spinner in the original class:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
However, I still can't use that selected string yet to fill in my code in the same class:
textView.setText(selected);
I'm new to this Android anyway, so this question may be too dummy to some of you
Add the setText code in the onItemSelected:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
yourTextView.setText(parent.getSelectedItem().toString);
}
Try this code,I hope It's help you.
final CharSequence[] array_min = {"No
Min","100","200","300","400", "500","600","700","800","900","1000",
"1100","1200","1300","1400","1500","1600","1700","1800","1900","2000","2500","3000","3500" };
Spinner s = (Spinner) layout.findViewById(R.id.viewSpin);
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_min);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3)
{
selItem = parent.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use global variable..make following changes in your code
String selected="";
public class MyItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selected = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
textView.setText(selected);
After register spinner ,you can get selected item from getSelectedItem() method on any action such ocClick()
regType = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.regType, android.R.layout.simple_spinner_dropdown_item);
regType.setAdapter(adapter);
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSave:
intent.putExtra("regtype",regType.getSelectedItem().toString());
startActivity(intent);
break;}}
I have my spinner pulling from an array,(that works)..
What I want to happen is I have several textviews that will map to whatever is selected from the spinner array... e.g British Columbia. then the Texviews will all say british columbia.
I figure i should be using a setText of some kind but have been on able to find an example anyhelp would be appreciated.
Here is my spinner code:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.material_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final TextView tv34=(TextView) findViewById(R.id.tv34);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
tv34.setText(parent.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
});
Try this:
final String[] array=getResources().getStringArray(R.array.material_array);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
mTextView_1.setText(array[position]);
mTextView_2.setText(array[position]);
mTextView_3.setText(array[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
EDIT :
Or you can do it like:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
String selectedItem=spinner.getSelectedItem().toString();
mTextView_1.setText(selectedItem);
mTextView_2.setText(selectedItem);
mTextView_3.setText(selectedItem);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});