Adding selected Spinner value in a table on runtime - android

Is it possible to add selected spinner item in a table (which is generating dynamically) in textview on button click.
Generally I use this code:
spinner=new String[5];
spinner[0]="1";
spinner[1]="2";
spinner[2]="3";
Spinner spn = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, spinner);
spn.setAdapter(adapter);
but what to do when to add the selected value in table?

you need to listen to the Spinner's OnItemSelected event. And don't forget to set the layout for the drop-down items! So in order:
Set the Adapter like you did correctly: spn.setAdapter(adapter);
Set the drop-down item view: spn.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Hook on the OnItemSelected event:
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View arg1,
int position, long arg3) {
// gets called when the user selects an item
TextView txt = (TextView)findViewById(R.id.your_textview_id);
txt.setText((String)adapter.getItemAtPosition(position));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// what to do when nothing is selected
}
});
That's it...

After the Spinner is populated get the Selected value in Spinner by using OnItemSelectedListener.
Here is the code to get the Selected value in Spinner :
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
selectedspinnervalue =spinner.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
In which selectedspinnervalue contains the selected value..
Then you add the value in Table..

Related

Android change value of Spinner

I created 2 Spinners and I to fill them with an array of numbers. What I want is that the user chooses the number they want on the first one, and then I save it to a variable that I use to populate the dropdown list of the second one with values that start with that number. How can I do this?
How can I change the text in the second spinner to start with the number chosen.
I have this, I store the item of the Spinner on the variable x, and I want a second Spinner to start with that variable.
mySpinner
.setAdapter(new ArrayAdapter<String>(NewGuiaActivity.this,
android.R.layout.simple_spinner_dropdown_item,
establist));
// Spinner on item click listener
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// Locate the textviews in activity_main.xml
x = estab.get(position).getID();
Toast.makeText(getApplicationContext(), x,
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
If I understand what you want to do correctly, for example if you select the number 4 from the drop down of spinner1, you want spinner2 to be set to the 4th position in it's list:
Try something like this, say you have mySpinner1 and mySpinner2, let your class implement OnItemSelectedListener and give it a class variable private int spinner2Pos = 0;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == case R.id.spinner_1_id){
spinner2Pos =
(spinner.getItemAtPosition(position) < mySpinner2.getAdapter().getCount())?
spinner.getItemAtPosition(position): 0;
mySpinner2.setSelection(spinner2Pos);
}
}
This should set mySpinner2 to the item you select with mySpinner1, which is stored as a class variable spinner2Pos. I haven't had time to check it so let me know if there are errors.

How can I click a button to launch an ItemSelected event of spinner control?

I have a spinner control with setOnItemSelectedListener in my app, when I select an item of the spinner, the event onItemSelected will be launched.
Now I hope to click a button to launch the onItemSelected event, how can I do? Thanks!
spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
//Do Business Logic
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Just call the following from your button listener:
spinnerFolder.getOnItemSelectedListener().onItemSelected(spinnerFolder, spinnerFolder.getSelectedView(), spinnerFolder.getSelectedItemPosition(), spinnderFolder.getSelectedItemId());
That's all :-)
The third parameter (int arg2) of onItemSelected for your spinner is the position, so you can get the current selection by
String selection = (String) mSpinnerAdapter.getItem(position);
or use mSpinner.getItemAtPosition(position) if you don't have a custom adapter.
Store the current selection somewhere and pick it up in the onClickListener for your button.
When you populate the spinner with some data, you will have some sort of list objects (Strings, custom object, whatever) in the SpinnerAdapter, you'll keep a reference to that list, let's call it: private List<Object> dataList = ...
First of all I would create a method that would handle the specific data object:
protected void doBusinessLogic(Object myObj) {
// do the things that make you happy
}
Then call this from listener as:
spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// args2 is the position from backed data list
doBusinessLogic(dataList.get(args2));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Then from button listener you would call above method again, but the object index you would get is from spinnerFolder.getSelectedItemPosition();:
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int dataIndex = spinnerFolder.getSelectedItemPosition();
doBusinessLogic(dataList.get(index));
}
});

how to store the selected value of a spinner in sqlite database?

i want to store the selected value of a spinner in sqlite database
i am giving spinner value through array adapter
following is my code for spinner
ArrayList<String> incorparray = new ArrayList<String>();
incorparray = new ArrayList<String>();
incorparray.add("DOB");
incorparray.add("Incorporation Date");
incorparray.add("Establishment Date");
ArrayAdapter<String> adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, incorparray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
incorporation.setAdapter(adapter);
please help! i need it urgently
i want to store the selected value of a spinner in sqlite database
So first you need to set OnItemSelectedListener() for your Spinner to be able to handle select events. Then simply on onItemSelected() method just perform insertion ino SQLite with proper method.
Example:
yousSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
String selectedItem = parent.getItemAtPosition(pos).toString();
// make insertion into database
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
where parent.getItemAtPosition(pos).toString() returns selected item from Spinner.
Add set on selected listener on spinner and then the selected value is stored in item. Now store the value of item in sqlite using insert query.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
item = (String) parent.getItemAtPosition(arg2);
// ((TextView) parent.getChildAt(0)).setTextColor(0x00000000);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

gridview in android

i have one spinner which display data from database. now i want to display data in grid view which selected in spinner.
so plz give good solution for my this question.
I think you are trying to get the value selected from the spinner and display it in grid
View,if so try this..
Spinner spin_type = (Spinner) findViewById(R.id.Spinner_type);
spin_type.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selected Value=array_type[arg2].toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Where array_type your String array that is having complete "DATA"..
You will get the Value selected in the Spinner in "Selected Value"..

Spinner on value Change

I have two spinner in my system. Now I have to change the selected value of the 2nd spinner depending on the first spinner value. As soon as the user will change the 1st spinner value, the 2nd spinner value will set automatically depending upon the 1st spinner's selected value. How to implement this?
From the Hello Spinner tutorial:
Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored.
Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.
You have to put the condition on onItemSelected of very first spinner. By this example you can get value of 2nd spinner depending on 1st spinner:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(arg0.equals(spin0)){
spin1.setClickable(true);
if(spin0.getSelectedItem().equals("India"))
{
ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s1);
}
else if(spin0.getSelectedItem().equals("Pakistan"))
{
ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s2);
}
else if(spin0.getSelectedItem().equals("China"))
{
ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s3);
}
}
}
You should define the onItemSelected() separately for each spinner. Otherwise the code gets executed if anything is selected from either spinners.
newCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selCat = newCategory.getItemAtPosition(arg2).toString();
if (selCat != "New")
{
loadSpinnerData(topic);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
newTopic.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
loadSpinnerData()
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Categories

Resources