if I have two spinner and add to first spinner as:
int pos=0;
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
pos=position;
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
And now I want to fill the second spinner Based on the position of first spinner How do I do that?
e.g.
if pos->0 second spinner Contains {A,B,C}
if pos->1 second spinner Contains {D,E,F}
... etc.
thanks in advance...
In onItemSelected(...), change the adapter of the second spinner based on the position of the first. You should be able to reuse some of your code to accomplish this.
Related
I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I have an issue of loading view dynamically. I have three dynamic spinner view when i select first item from the spinner it should create a text view above second spinner but its creating a view below third spinner. Can any one guide me how to achieve it.I have shared my code below.
/add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
rl.setOrientation(LinearLayout.VERTICAL);
// add spinner
String[] spinnerArray = {"Android","ios","other"};
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
String[] spinnerArray1 = {"flowers","vegetables","other"};
Spinner spinner1 = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray1); //selected item will look like a spinner set from XML
spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(spinnerArrayAdapter1);
String[] spinnerArray2 = {"cat","dog","other"};
Spinner spinner2 = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray2); //selected item will look like a spinner set from XML
spinnerArrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(spinnerArrayAdapter2);
rl.addView(spinner);
rl.addView(spinner1);
rl.addView(spinner2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(position==1)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
rl.addView(tv);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Do you know how LinearLayout works?
http://developer.android.com/reference/android/widget/LinearLayout.html
You should call
addView(tv, 1);
Specifying position on where to insert view.
I have used android custom spinner.
My custom spinner look like bellow
My List data is like
List<String> list = new ArrayList<String>();
list.add("001-00434016457-Md. Mokhhlesur Rahman");
list.add("001-00434016724-Mohammad Nazim Ullah");
list.add("001-00434016725-Mohsin Kabir");
list.add("001-00434016881-Maruf Pervaz Khan");
list.add("001-00434017171-Md.Nazmul Hasan");
My Spinner Adapter and setting data in spinner is like bellow
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
My problem is spinner's item text is large like "001-00434016881-Maruf Pervaz"
I want to select only id like 001-00434016881.
I don't want to get full text of the spinner item selection. How can I remove name part like -Maruf Pervaz Khan from 001-00434016881-Maruf Pervaz on item selection from spinner.
Please Help me
Try this. If the row item maintains same pattern as 001-00434016881-Md.Some Name
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItem(position).substring(0,adapter.getItem(position).lastIndexOf("-")), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
I have activity with 3 spinners and selection of 1st spinner is related to 2nd and 2nd selection is related to the 3rd, so I have the spinner code one inside one.
CrustSp, SizeSp and extraDescriptionOneSP are the 3 spinners I have. I have loaded the items to the Spinners and I can select a value as well. But my problem is whenever i made a selection in CrustSp and SizeSp doesn't show in MyOnItemSelectedListener(). But if I made a selection in extraDescriptionOneSP it shows inside MyOnItemSelectedListener().
I can't figure where I have gone wrong, anyone can point out where I have gone wrong it will be really helpful.
This the code
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultSizes = getFilteredSizes(crustSelectedItem);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultSizes);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String sizeSelectedItem = sizeSP
.getSelectedItem().toString();
List<String> resultTopping = getFilteredToppings(sizeSelectedItem);
extraDescriptionOneSP
.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this));
ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultTopping);
dataAdapterExtraDesOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
extraDescriptionOneSP
.setAdapter(dataAdapterExtraDesOne);
extraDescriptionOneSP
.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterExtraDesOne,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
}
});
MyOnItemSelectedListener method,
public class MyOnItemSelectedListener implements OnItemSelectedListener {
Activity activity;
String StrOneSPPosition;
public MyOnItemSelectedListener(Activity p_activity) {
activity = p_activity;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// check which spinner triggered the listener
switch (parent.getId()) {
case R.id.sp_crust: {
String StrCrustSPPosition = String.valueOf(crustSP
.getSelectedItemPosition());
System.out.println("crust = " + StrCrustSPPosition);
System.out.println("crust = " + position);
CrustSPPosition = Integer.parseInt(StrCrustSPPosition);
}
I don't see you using the selected position to determine the data for next spinner. BTW you should not make new adapter for each selection, instead reuse the once made adapter.
I want to show first item as a default text on spinner but in the drop down list that item is not shown like there is arraylist
final ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("number");
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
The number is always shown as a default text on spinner but after clicking on spinner there is one, two, three is shown in drop down list. when we select any number one, two three the default text number is not changed. How can i do it?
ArrayAdapter<String> adapte = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapte.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapte);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View arg1,int pos, long arg3)
{
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this, it show the default as Number..
May be this help you
String[] priceString = { "Number", "one, "two"};
Spinner priceSpinner = (Spinner) findViewById(R.id.price_spinner);
ArrayAdapter priceAdapter = new ArrayAdapter( this.getActivity(), android.R.layout.simple_spinner_item,priceString); priceSpinner.setAdapter(priceAdapter);