String Food;
String[] FoodArray;
Spinner spinFood;
spinFood= (Spinner) findViewById(R.id.spin_food);
FoodArray=getResources().getStringArray(R.array.carray);
spinFood.setAdapter(new
CustomSpinnerAdapter(ChildActivity.this,R.layout.activity_child,FoodArray));
spinFood.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
if(position==1){
Food=getString(R.string.textYes);
}
else if (position==2){
Food=getString(R.string.textNo);
}
else {
Food="0";
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
i tried a lot but didn't get the answer,
please help me.
the spinner should be set to a last recorded value which comes from the database, by default.
String compareValue;
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinFood.setAdapter(new CustomSpinnerAdapter(
getApplicationContext().this, R.layout.yourlayout,
compareValue));
if (!compareValue.equals(null)) {
int spinnerPosition = adapter.getPosition(compareValue);
spinFood.setSelection(spinnerPosition);
}
take your last recorded value i.e compareValue from sqlite database and set that value to ArrayAdapter and check for the null condition.
You can use spinner.setSelection() method to record your last transaction
Related
i create in a two way data binding spinner in Android, and added value into with a result of a REST GET.
I need now to add the first value of the spinner as an empty string that when it will be selected from the spinner, the value will be null.
In the fragment class i wrote this code:
//spinner
appCompatSpinner = binding.spinner;
spinnerAdapter = new SpinnerAdapter(getActivity(), viewModel.usersDetailsList );
appCompatSpinner.setAdapter(spinnerActorAdapter);
appCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
viewModel.onSelected(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mAdapter.notifyDataSetChanged();
Can someone please help me to achieve that?
I am trying to retrieve the spinner's selected position. The values inside the spinner are of type String and I am using setOnItemSelectedListener to change a class String variable to the one that has been selected and use it later on:
spinner = (MaterialBetterSpinner) myToolbar.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,socialMediaOptions);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
private void sendPost(){
Log.v(TAG, socialMedia);
}
The problem is that the variable does not change when an item is selected, it seems like onItemSelected is not being called when there is a change in the spinner selection
See this thread:
https://github.com/Lesilva/BetterSpinner/issues/42
There said that BetterSpinner extends EditText, that's why onItemSelectedListener is not being called. You should use TextChangedListener instead.
Hope your socialMediaOptions are string array like this
socialMediaOptions={"FB","TWIT","IN"}
then
// Specify the layout to use when the list of choices appears
add this line
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = socialMediaOptions[position];
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I have 2 spinners in an activity.
Based on the selection of one item in spinner1, relevant data should be loaded in spinner2. Consider spinner1 has data related to country and spinner2 has data related to state.
I should be able to get this done once the activity is created and if the user changes the selection in spinner1.
But I am stuck with populating the spinner2 data based on the saved value of spinner1.
I am calling spinner1.setSelection(indexSaved) but since I am only loading spinner2 in the onItemSelectedOf of spinner1, setSelection of spinner1 is not firing the onItemSelected.
Please let me know how I can achieve this.
I have done same thing in my project without any problem:
Below is code may be it help you:
On Create Method filled state list:
//initialize spinners
spinnerStateList = (Spinner) findViewById(R.id.spinnerStateList);
spinnerDistrictsList = (Spinner) findViewById(R.id.spinnerDistrictsList);
fillStateList();
public void fillStateList()
{
states = manage_states.fetchAllStates(getApplicationContext());
//add new element for select name
states.add(0,new RowItem("-1", "Select State"));
String[] spinnerArray = new String[states.size()];
for(int i= 0;i<states.size();i++)
{
spinnerArray[i] = states.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStateList.setAdapter(dataAdapter);
spinnerStateList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedStateID = states.get(position).getId();
fillDistrictsSelectionChangeState(selectedStateID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void fillDistrictsSelectionChangeState(String selectedStateID)
{
if(selectedStateID.equalsIgnoreCase("-1"))
{
linearLayoutDistrictsList.setVisibility(LinearLayout.GONE);
linearLayoutBlocksList.setVisibility(LinearLayout.GONE);
linearLayoutPHCList.setVisibility(LinearLayout.GONE);
linearLayoutSHCList.setVisibility(LinearLayout.GONE);
}
else
{
linearLayoutDistrictsList.setVisibility(LinearLayout.VISIBLE);
districts = manage_districts.fetchAllDistrictsByStateId(getApplicationContext(), selectedStateID);
//add new element for select name
districts.add(0,new RowItem("-1", "Select District"));
String[] spinnerArray = new String[districts.size()];
for(int i= 0;i<districts.size();i++)
{
spinnerArray[i] = districts.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDistrictsList.setAdapter(dataAdapter);
spinnerDistrictsList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedDistrictID = districts.get(position).getId();
fillBlocksSelectionChangeDistrict(selectedDistrictID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
Im new to android. i have tried to built an application which has 2 spinner...
i have few doubts to clear before going deep to my application..
please give brief answer to my question...
is it possible to have 2 spinners in one view?
can i add setOnItemSelectedListener(this) for both?
if yes, how could i know which option is selected on both the spinners? like spinner.getSelectedItemPosition(); on both the spinners?
waiting for ur valuable replies. reference materials are also appreciable
Yes, you can have multiple spinner's that can be managed by single listener or you can have seperate Listener for each.
For Single Listener you can do something like this,
Set the Listener
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
Then you can use onItemSelected to get perform the action related to the spinner's.
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long arg3)
{
int id = parent.getId();
switch (id)
{
case R.id.spinner1:
// your stuff here
break;
case R.id.spinner2:
// your stuff here
break;
}
Check this:
Spinner spinner0, spinner1, spinner2;
TextView textView0, textView1, textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView0 = (TextView) findViewById(R.id.text0);
spinner0 = (Spinner) findViewById(R.id.spinner0);
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text0);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(adapter0);
spinner0.setOnItemSelectedListener(onItemSelectedListener0);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setSelection(0,false); //make sure only one listener is
//active in the onCreate method by setting the other listener as false
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
textView2 = (TextView) findViewById(R.id.text2);
spinner2 = (Spinner) findViewById(R.id.spinner2);
MySpinnerAdapter adapter2 = new MySpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_item, obj2);
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(0,false);
spinner2.setOnItemSelectedListener(onItemSelectedListener2);
}
OnItemSelectedListener onItemSelectedListener0 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner0
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener1 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner1
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener2 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner2
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
If the reference of two spinners are different(which will be , if you are using 2 spinners) , give different IDs for them and you can uniquely identify them, Yes you can set click listener to both of them individually.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
yes
yes
spinner1.getSelectedItemPosition(), spinner2.getSelectedItemPosition()
I want to have dynamic spinner in android e.g one spinner for country and depending upon the value selected in Country, i want to get another spinner for states.
I believe you can solve this with the OnItemSelectedListener.
public void onCreate() {
....
Country[] mCountries = ... ;
final Spinner spinner1 = ...;
final Spinner spinner2 = ...;
spinner1.setAdapter(new ArrayAdapter(mCountries);
spinner1.setOnItemSelectedListener( new OnItemSelectedListener() {
void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Country country = (Country) parent.getAdapter().getItem(position);
spinner2.setAdapter(new ArrayAdapter(country.getStates());
}
void onNothingSelected(AdapterView<?> parent) {
spinner2.setAdapter(null);
}
});
....
}