Android change value of Spinner - android

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.

Related

Listview Search issue

I have a listview that working perfet.Today I added a search function to the listview using this code
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
The problem: I have 114 items on my listview and the selected item depends on the position, when I do search and press on the item that I found it will return the position 1, I want to return the position of that item before the search.. not the new position, is there anyway to solve it?!
Nope, it will return the position of the item based on the current lenght of your list view/adapter.
Why would it matter?
If you want to start a different activity based on the item clicked, there are many ways to do it. Using the position in the list to choose the activity works ONLY when the positions are fixed, which is not your case, since the user can filter the items.
Instead, start the activity based on the content of the item clicked.
Say for instance, your listview is populated using the following items:
public static final String [] PLANETS = {
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
};
Using the following ArrayAdapter:
mAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, R.id.text1, PLANETS);
Then you can edit your OnItemClickListener to start the activity based on the actual planet selected, not the position selected.
private AdapterView.OnItemClickListener mOnPlanetOnItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int iistPosition, long l) {
//Notice I don't get the value from the String array
//Rather, I tell the adapter to give me the text from the selected item.
String planet = mAdapter.getItem(iistPosition);
Intent newActivity = new Intent(Example.this, NextActivity.class);
newActivity.putExtra("planet", planet);
}
};
If you are using a custom adapter it's even easier.
EDIT It is unfortunate that because of the way your mp3 files are named you must depend on the list position to get appropiate file.
In that case you could try a few workarounds, for instance:
If your list view is populated with a set ArrayList or just a List, you could first get the text of the item selected, and then get the actual array position based on the text of the item selected.
Like this:
private AdapterView.OnItemClickListener mOnPlanetOnItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int iistPosition, long l) {
//get the text of the current item
String planet = mAdapter.getItem(iistPosition);
//get the position of this planet in the original unfiltered array
int actualListPosition = PLANETS_ARRAY.indexOf(planet);
//handle the click based on the actualListPosition
}
};
I hope that helps

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
}
});

How to add a title bar in Android while sorting?

Here is my code:
public class MainActivity extends Activity {
Spinner spin;
TextView tex;
String[] country = {"A", "Afghanistan", "Albania", "Etc"};// A to Z country names
String[] code = {"+93", "+91", "Etc"}; // A to Z country Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner)findViewById(R.id.spinner1);
tex = (TextView)findViewById(R.id.tex);
ArrayAdapter aa1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, country);
aa1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setPrompt("Select the Country");
spin.setAdapter(aa1);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tex.setText(code[arg2]);
// tex.setText(country[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
I want to display the country's list in alphabetic order on spinner. And before that it should display the A, B, C up to Z. But This A to Z must be unselectable mode in spinner list. How can I achieve that?
You'll have to create your custom adapter that extends the ArrayAdapter.
it will probably be very easy, something like:
// the get view on your adapter
getView(LayoutInflater, etc, etc){
convertView = super.getView(inflater, etc, etc);
if(getItem(position).equals("A") || getItem(position).equals("B") || // etc, or create some clever way to go through a ArrayList with just the letters ){
convertView. // set not clickable stuff
}
}
but I reckon the Spinner still will close the list whenever the user clicks. Maybe you must override the spinner OnItemClick to get a coherent behaviour.

Adding selected Spinner value in a table on runtime

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..

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