Android: two Spinner onItemSelected() - android

I have two spinners (day_spin and time_spin) in one Activity. I want to save the selected day_spin value into a variable. Is it possible to differenciate between the two spinners in the same Listener? Or do I have to write my own Listener class for each spinner?
Also I want to get the number of the selected item. For example I have String Array with 6 elements. I select number 3. How can I get the id?
day_spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter_day = ArrayAdapter
.createFromResource(this, R.array.spinner_day_array,
android.R.layout.simple_spinner_item);
adapter_day
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
day_spin.setAdapter(adapter_day);
day_spin.setOnItemSelectedListener(this);
time_spin = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter_time = ArrayAdapter
.createFromResource(this, R.array.spinner_time_array,
android.R.layout.simple_spinner_item);
adapter_time
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
time_spin.setAdapter(adapter_time);
time_spin.setOnItemSelectedListener(this);
This is my Listener:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
DAY = parent.getItemAtPosition(pos).toString();
TIME = parent.getItemAtPosition(pos).toString();
}
I hope you can help me!

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinner1)
{
//do this
}
else if(parent.getId() == R.id.spinner2)
{
//do this
}
}

public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
//parent.getItemAtPosition(pos);
//Spinner spnir = (Spinner)parent;
parent.getItemAtPosition(pos);
TextView tvReadValue = (TextView) findViewById(R.id.tvReadValue);
switch (parent.getId())
{
case R.id.spinDimension:
tvReadValue.setText("spinDimension" + pos);
break;
case R.id.speedDimension:
tvReadValue.setText("speedDimension" + pos);
break;
}

in onItemSelected method
int i = spinnerFirst.getSelectedItemPosition();
int j = spinnerSecond.getSelectedItemPosition();
String strName1 = yourFirstarray[i];
String strName2 = yourSecondarray[j];

try this...
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinnerone)
{
// operations
}
else if(parent.getId() == R.id.spinnertwo)
{
//operations
}
}

Two Spinner in same class
I try a lot of this but at the last, I got a working Code.
state = findViewById(R.id.spinnerState);
country = findViewById(R.id.spinnerCountry);
List<String> categories = new ArrayList<String>();
categories.add("Select State");
categories.add("Andhra Pradesh");
categories.add("Arunachal Pradesh");
categories.add("Assam");
categories.add("Bihar");
categories.add("Chandigarh");
categories.add("Delhi");
categories.add("Goa");
categories.add("Gujarat");
categories.add("Haryana");
categories.add("Himachal Pradesh");
categories.add("Jammu and Kashmir union territory");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(dataAdapter);
state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// can leave this empty
}
});
List<String> categories2 = new ArrayList<String>();
categories2.add("Select Country");
categories2.add("Afghanistan");
categories2.add("Albania");
categories2.add("Algeria");
categories2.add("India");
categories2.add("Andorra");
categories2.add("Angola");
categories2.add("Antigua and Barbuda");
categories2.add("Argentina");
categories2.add("Armenia");
// Div Spinner implementing onItemSelectedListener
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
country.setAdapter(dataAdapter2);
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// can leave this empty
}
});
}

#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (adapterView.getId() == R.id.number_of_poket_spinner) {
NumberOfPokets item = (NumberOfPokets) adapterView.getSelectedItem();
Toast.makeText(this, item.getPokets(), Toast.LENGTH_LONG).show();
} else if (adapterView.getId() == R.id.select_pleats) {
PleatsModel item2 = (PleatsModel) adapterView.getSelectedItem();
Toast.makeText(this, item2.getPleats(), Toast.LENGTH_LONG).show();
}
}

Related

Linking 3 spinners together

I'm very new to android programming, I'm trying to create 3 spinners, these spinners will be in the form of country (122 units) province (10000) and district (15000) and when the country is selected, the provinces of that country will appear on the spinner and the districts belonging to it will appear on the spinner. or how can I add the district if and else make this situation very complicated. How should I use a structure? Thank you for your help
{
spinnerIller = (Spinner) findViewById(R.id.spinneriller);
spinnerIlceler = (Spinner) findViewById(R.id.spinnerilceler);
dataAdapterForIller = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, iller);
dataAdapterForIlceler = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,ilceler0);
dataAdapterForIller.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapterForIlceler.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerIller.setAdapter(dataAdapterForIller);
spinnerIlceler.setAdapter(dataAdapterForIlceler);
spinnerIller.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
((TextView) view).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gri));
if(parent.getSelectedItem().toString().equals(iller[0])) {
dataAdapterForIlceler = new ArrayAdapter<String>(Register.this, android.R.layout.simple_spinner_item, ilceler0);
}else if(parent.getSelectedItem().toString().equals(iller[1])) {
dataAdapterForIlceler = new ArrayAdapter<String>(Register.this, android.R.layout.simple_spinner_item, ilceler1);
}else if(parent.getSelectedItem().toString().equals(iller[2])) {
dataAdapterForIlceler = new ArrayAdapter<String>(Register.this, android.R.layout.simple_spinner_item, ilceler2);
}
dataAdapterForIlceler.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerIlceler.setAdapter(dataAdapterForIlceler);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerIlceler.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
(TextView) view).setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gri));
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

how to invisible recyclerview item getting value from spinner

I want when user click on "قرآن" my program invisible else things mean tarjuma and tafsir,
same like when he clicks on "مع ترجمہ" my program visible Quran and tarjuma and hide the tafsir.
I am using spinner it works fine but after refresh my recyclerview but I want without refreshing the recyclerview, mean is that when the user selects any Spinner value recyclerview change the view.
refresh mean going to another activity.
anybody who help me!!
here is my RecyclerView Adapter
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final Quran quran = quranic.get(position);
if (Quran.spinner.equals("قرآن")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.reference.setText(quran.getReferences());
holder.tarjuma.setVisibility(View.INVISIBLE);
holder.tafsir.setVisibility(View.INVISIBLE);
} else if (Quran.spinner.equals("مع ترجمہ")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.reference.setText(quran.getReferences());
holder.tafsir.setVisibility(View.INVISIBLE);
} else {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.tafsir.setText(Html.fromHtml(quran.getTafsir()));
holder.reference.setText(quran.getReferences());
}
}
my MainActivity
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Quran.java
public static String spinner = "";
You just need to call notifyDataSetChanged(); on your recycler view adapter each time you select an item from your spinner.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Even better if you keep a reference to your recycler view adapter, then replace:
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
by:
adapter.notifyDataSetChanged();
}
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
//add this line
yourAdapterObject.notifyDataSetChanged();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

Update Spinner from another spinner dynamically

I have a fragment that creates controls at runtime, first create a spinner and then the second, the detail is in the second spinner depends on having the first selection.
As I can update the data of the second spinner depending on the selection of the first?
final Spinner Combo2 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
final Spinner Combo1 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list = new ArrayList<String>();
list.add("TERRESTRE");
list.add("FLUVIAL");
list.add("AEREO");
final ArrayAdapter<String> adapterCombo = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item,list);
List<String> list2 = new ArrayList<String>();
list2.add("OP1");
list2.add("OP2");
final ArrayAdapter<String> adapterCombo2 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list2);
if(listFormato.get(posicion).cSeccion.trim().equals("M"))
{
adapterCombo.setDropDownViewResource(R.layout.spinner);
Combo1.setAdapter(adapterCombo);
Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo1);
Combo1.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
}
else if(listFormato.get(posicion).cSeccion.trim().equals("U")){
final Spinner Combo0 = new Spinner(FichaRutasVerif2SeccionSlideFragment.this.getActivity());
List<String> list3 =new ArrayList<String>();
list3.add("SI");
list3.add("NO");
ArrayAdapter<String> adapterCombo3 = new ArrayAdapter<String>(FichaRutasVerif2SeccionSlideFragment.this.getActivity()
,android.R.layout.simple_spinner_item, list3);
adapterCombo3.setDropDownViewResource(R.layout.spinner);
Combo0.setAdapter(adapterCombo3);
llTempZZ.addView(Combo0);
llTempXX.addView(llTempZZ);
//llTempXX.addView(llTempAA);
llTEMPADRE.addView(llTempXX);
pllh.addView(llTEMPADRE);
Combo0.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if (pos == 0) {
pll2.setVisibility(View.VISIBLE);
}
else if(pos==1) {
pll2.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo0.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
else if(listFormato.get(posicion).cSeccion.trim().equals("T"))
{
adapterCombo2.setDropDownViewResource(R.layout.spinner);
Combo2.setAdapter(adapterCombo2);
//Combo1.setSelection(listFormato.get(posicion).iIndexCombo);
llTempZZ.addView(Combo2);
Combo2.setGravity(Gravity.CENTER);
llTempXX.addView(llTempZZ);
llTEMPADRE.addView(llTempXX);
//////TEXTOS
Combo2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView,
View view, int pos, long id) {
if(Combo2.getSelectedItemPosition()==0)
{
}
if (pos ==12) {
llTempAA.setVisibility(View.VISIBLE);
} else {
llTempAA.setVisibility(View.GONE);
}
listFormato.get(posicion).vResultado = Combo2.getSelectedItem().toString();
RECORDCARDITEM_DAO.Actualizar(FichaRutasVerif2SeccionSlideFragment.this.getActivity(), listFormato.get(posicion));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
You can do something like this
Spinner firstSpinner = (Spinner) findViewById(R.id.first_spinner);
// You can set your first spinner values here if not set already, and then
firstSpinner.setOnItemSelectedListener(onFirstSpinnerChange(firstSpinner));
And the further add this
AdapterView.OnItemSelectedListener onFirstSpinnerChange(Spinner mySpinner) {
return new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// You can add this method to set the values of the second spinner in your activity
// You can use the position, or alternatively selectedItemView for this purpose to identify the selected item
setSecondSpinnerBasedOnFirstOnePositionSelected();
// Or you can just set your spinner in this block
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
};
}

How can I pick only once a value from spinner in android

I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable

Spinner return null value

when i am selected spinner value then some time it return a true value and some time it return null value
My spinner code is
list.add("Please Select Approver Name");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
sp1.setSelection(0);
Item Selected Listener
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
I am working on it past two day
Please Help Me how i can fix this problem
Use the Toast inside the onITemSelected method as below.
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

Categories

Resources