Setting spinner position dynamically in android? - android

I am developing an app where i need to set spinner values dynamically based on previous screen values. My code...
Main.java.
String[] values = {"All","Only Walk-in","Only Phone","Only Web","Walkin-phone","Walkin-web","phone-web"};
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,apttypes);
spinner.setAdapter(adapter);*/
But here what i want is from previous screen i am getting some value (like spinner postion). based on that i need to set spinner value to display...
Means from previous screen if i got values =0 means,
i need to set spinner value to display "All" from values array at top.
If i got value= 5 means,
i want to set spinner value to display as "Walkin-web"
How can i do that. can anyone help me with this...

Pass the value in from the previous Activity using extras in the Intent you use to launch it. Then when you've read the value call
int position = getIntent().getIntExtra( "name", defaultValue );
spinner.setSelection( position );
Which will move the spinner to the index you selected.

Use following array code and create new array adapter each time
String[] str=new String[maxsize-4];

you can implement onItemClick event on Spinner like this and setSelection(position)
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
payfeeTabStudentNameSpinner.setSelection(position);
spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Inside your First Activity A.java
public static String Position = YOUR_SPINNER1.getSelectedItem().toString();
Inside your Second Activity B.java
if(A.Position.equals("0")){
//Set your adapter accordingly
}else if(A.Position.equals("1")){
//Set your adapter accordingly
}

You can assign the Spinner's Position using the following code..
Spinner s1;
-----------
-----------
int position=valueFromPrevious;
s1.setSelection(position);
-----------
-----------

pass the values from previous activity using Extras and then when u want use it in your current activity follow this:
If u get String value then typecast it into interger by parseInt method...
String strtext4 = getIntent().getStringExtra("qualification");
then
int position = Integer.parseInt(strtext4);
after this just set it to your spinner
qualificationspinner.setSelection(position);

Related

Spinner first item default set empty value?

Here in this code i have added pincode numbers from 600000 to 600113 in the spinner but i want the first position to be just empty. When the user clicks only then the item should be shown. Please see my code -
final String[] myarray=new String[114];
for(int i=0;i<114;i++)
{
myarray[i]=String.valueOf(a);
a++;
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,myarray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
addnum_spinner.setAdapter(adapter);
lv.setOnItemClickListener(itemClickedListener);
//Spinner click
addnum_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
String value = myarray[position];
//
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
add this property to your spinner in xml and try if that works
android:prompt=""
I don't know if this is the right way but what about just adding the empty string before the for loop as the first item and then you just have to handle the user clicking on the empty item in your Listener. Something like
final String[] myarray=new String[115];
myarray[0] = ""
for(int i=1;i<115;i++)
{
myarray[i]=String.valueOf(a);
a++;
}
I know this question is old, but I'm adding a solution. I found this out on accident, as I DIDN'T want a blank default value, but there was one.
If using an ArrayAdapter, I found that attaching the adapter to an empty ArrayList, and THEN filling it left a blank initial value.
Filling the ArrayList first, then attaching the ArrayAdapter removed the blank default value.
Obviously, if not using an ArrayAdapter, using the prompt, or setting the first Spinner item as <item></item> with nothing in between in the XML should work.

How to set selected Items from a spinner as a default value in Android?

This is my code. If I click "14 " then in a spinner should be set as a default value as 14, and if I changed "20" or some values like "16" it should set as a default value in a Spinner.
So selected value must be displayed in a Spinner.
Spinner font=new Spinner(con);
option= new String[] {"Select","14","16","18","20"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(con,android.R.layout.simple_spinner_dropdown_item,option);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
font.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id) {
if(!(arg0).getSelectedItem().toString().trim().equalsIgnoreCase("Select"))
size=Float.parseFloat((option[(arg0).getSelectedItemPosition()]).trim());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
font.setAdapter(adapter);
int i;
for( i=0;i<option.length;i++){
if(option[i].toString().equalsIgnoreCase("14")){
}
else {
font.setSelection(i);
}
}
When you goto next screen and comeback then it automatically displays the last selected value by default (if your for loop code not exists here). if you goto previous screen from the spinner screen and come back then spinner will be created again and adapter is set again. so value selected last cannot be set. If still that is your requirement then save the value to shared preferences when selected. then after setting adapter, you get the value if already exists. In your code instead of checking equal condition with "14" you check it with shared preference saved value and call setSelection(i) in if loop

Android How to retrieve value of Dynamically added Spinner

I am dynamically adding the Spinners in my application by parsing the XML file.
I have done using the below code
List<Spinner> allspin = new ArrayList<Spinner>();
Spinner spin = new Spinner(getParent());
allspin.add(spin);
spin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(),
android.R.layout.simple_spinner_item, selectval);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
Spinners are displayed correctly but i don't know how to retrieve the value of selected spinner. If there is one spinner i can retieve, but there are multiple how should i do?
You can get the reference of the spinner from your arraylist like this :
Spinner spn = allspin.get(index);
After that you can get the selected item by simply calling:
spn.getSelectedItemPosition();
You can set id of spinner dynamically.
For that,you can use
spin.setId(i); //if you use i of for loop for creating multipal spin at a tym or you can use a global variable i,incremented by one each time you create a spinner
and further,you can use those ids to get values from particular spinner.
Example:
for(int i=1;i<4;i++)
{
Spinner spin=new Spinner(getApplicationCotext());
spin.setId(i);
...
//other code
...
mLayout.add(spin);//add this spinner to your layout(mLayout is object of your layout in xml)
}
Now,
for(int i=1;i<4;i++)
{
Spinner sp=(Spinner)findViewById(i);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
System.out.println(sp.getText().toString());//prints values of a pinner when it is changed/selected
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
If you need multiple spinners (I'm not sure I understand your use case), then, as you're creating each of them, you need to also create the appropriate listeners. You shouldn't have to retrieve values of spinners yourself; handling these selection events should be done in your listeners. Perhaps if you explained what you're attempting to do a bit better it would be easier to help...

Get the values from one layout to next layout?

I've listed some items in list that extends Activity and the list are listed using Custom Adapter. My question is, this is my xml File I've added some items to this spinner. How can i get the spinner values to next layout. Anyone knows then please tell me? Advance thanks.
I'm not clear on what you're actually asking here, but as a guess there are two possible things you're asking
How to get the currently selected value out of the Spinner
How to set the same value to a spinner in the next layout
1.
Is simple enough
((Spinner)findViewById(R.id.spinner1)).getSelectedItem()
Will return the object selected by your spinner.
Is slightly more complex, you'll need to determine what index in the data supplied corresponds to the result you get from getSelectedItem(), for example if you had an array of Strings then you could search through until you found the index and then set it on the new spinner.
For example:
String[] options = new String[] {"One","Two","Three","Four"};
String val = (String)((Spinner)findViewById(R.id.spinner1)).getSelectedItem();
//.......pass this to a layout/activity etc.........
for (int i=0; i<options.length; i++)
{
if (options[i].equals(test))
{
((Spinner)findViewById(R.id.spinner2).setSelection(i);
break;
}
}
But your best bet would be to try and explain more clearly what you're asking.
first you have to select data from spinner using
spinnerobject.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Spinner spinnerobject = (Spinner) findViewById(R.id.Spinner02);
string value = (String)spinnerobj.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
then u hava to use intent for sending it to next activity..
Use
Intent.putExtra(..):
intent.putExtra("keyName", "somevalue");
This method is overloaded and takes various types as second argument: int, byte, String, various arrays..
To get the data out use appropriate getXYZExtra(). For String this is:
getStringExtra(String keyName)
First thing :: you can pass value from one activity to second activity not one layout to second
second :: if you need to pass value from one activity to second use
first activity::
activity.putExtra("lastpage", lastscore5);
*here lastpage is key which unique for aplication
second activity::
Intent i1 = getIntent();
var = i1.getIntExtra("lastpage", 1);

How to change data on one spinner by onclick on another spinner in android?

all i have two spinners spinner1 & spinner2. in one spinner i am showing names from database.(Ex. onkar) now if i select onkar name on spinner1 then all names must be occurred on spinner2 except name onkar.
i want to do like this.but i am not getting how to achieve it?
give me some sample or give idea for the same.
or can i change data of spinner dynamically????
Thanks in Advance--
consider that u have used two adapters to set data to spinner when u select first spinner just remove that value from second adapter and then call method notifyDataSetChanged() it will change second spinner data.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
super.onItemSelected(parent, view, position, id);
// Step 1: Remove Data from ArrayList 1
// Step 2: Adpater for first spinner .notifiyDataSetChanged()
}

Categories

Resources