How to show Spinner selected Item first in Android - android

In my application i have one spinner with data Cart,Trolley,Lorry,Truck etc..
in a button click i am saving spinner selected item and other items in the data base.
Now in another button click i want to display all saved data,so in that i want to display previously saved spinner item first instead of default one.
How can i achieve this,please anyone suggest me
Ex:in spinner 1,2,3,4 displayed now if i select 3 and saved in data base now this time i want to show spinner data as 3,4,1,2.

It's much simple by getting index of spinner from DB and set the currently selected index on spinner item,for example if the spinner position stored in DB
then set it as
spinner.setSelection(2);// Note : Position starts from 0,1,2,3 on array

You can change spinner content as below
String[] items = new String[] {"3","4", "1","2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spinner2.setAdapter(adapter);

So I understand you correctly, you want to reorder the spinner items based on the user's previous selection? You just have to update the Adapter that you assigned to the spinner in that case.
I guess you wired up a simple ArrayAdapter in this case, so a basic solution would be to modify the order of the strings contained in that adapter, after selection.

ArrayList listArray = new ArrayList();
listArray.add("one");
listArray.add("two");
listArray.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listArray.toArray(strArray););
spinner2.setAdapter(adapter);
here took items as ArrayList and when user click on any item , break that arraylist in two part start to that point and point to last. then take one temp arraylist and add second part then first part so in that one .
and again call
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listArray.toArray(strArray););
spinner2.setAdapter(adapter);

Related

How to fill Spinner with int value

When normally populating a Spinner as I have done in the past I normally use a SpinnerAdapter then normally have items in resources to populate it.
I have currently though a different query, I have in my code a user input for an int and I want my spinner to populate with numbers up to the user selected number. So if the user enters the number '5' it is saved to an int variable. I then want the Spinner to show 1,2,3,4,5 as choices.
I am really not sure how I would approach this.
Thanks, Oli
Edited
Below is a basic example of how you would add Integers to your spinner :
mspin=(Spinner) findViewById(R.id.spinner1);
Integer[] items = new Integer[]{1,2,3,4};
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
mspin.setAdapter(adapter);
You can refer to this and make changes in your project as per your logic. Also in your case you should use an ArrayList of integers since the number of choice of the user seems to be dynamic. you can create an arraylist and replace in for the Integer array in the above code.
Hope this helps!!

Create empty Spinner

How can I create an empty spinner with no elements in it and add them later?
ArrayAdapter<String> aSpin = ArrayAdapter.createFromResource(this, null , null);
Doesnt work.
I'm curious, in which scenario would you want an empty spinner?
anyway, a spinner with a null object will give you an annoying exception. If you really want a spinner "empty", i would recommend you to create a normal spinner with a fake object. That object would be a string like " ". So, it won't be empty but it will look like if it is.
-- Edit --
In order to use a spinner with objects, you will need an adapter. So, first, if you want an empty spinner, just find its view from your layout and that's all. You will have an empty spinner:
mSpinner = (Spinner)findViewById(R.mLayout.mSpinnerID);
Then, when you need to add your items to the spinner, create an array with those items, then add them to the adapter, and add the adapter to your spinner. For example, let's say that you will have strings:
ArrayList<String> mList = new ArrayList<String>();
mList.add("item 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mList);
mList.setAdapter(adapter);
An Adapter is something different than a Spinner. If you want to create an empty Spinner, just define one in your layout file or create one programmatically. It will not contain any items if you don't set an Adapter on the Spinner (or specify static entries in the xml declaration).
You might want to take a look at the Spinner tutorial.

How to change the entry of a spinner in code (Android)

Hi I have a spinner for which I would like to change its entry. I have created an array in the values folder. I know that I can edit the entry of the spinner by right clicking on it. But I want to know, how can I change the entry of the spinner using code. I was hoping there would be something like spinner5.editEntries
Can someone help please?
Spinner Spinnermiles = (Spinner) findViewById(R.id.Spinnermiles);
String [] arrmile ={"5","10","20","30","40","50","70","80","90","100"};
adapter = new ArrayAdapter<String>(Searching.this,android.R.layout.simple_spinner_item,arrmile);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinnermiles.setAdapter(adapter);
If you want to change item in spinner at position 3 (which is "30" in example),
Set value at that position e.g.
arrmile[3] = "enter new value you want";
and after that call
adapter.notifyDataSetChanged();
then value at that position will be get updated.

creating autocomplete in android

I want to create a AutoCompleteTextView in android. The problem is that I want to show whole list of data when user selects AutoCompleteTextView and start filtering data as user types the letters.Please help me in doing this.
Well here is an way how you can do that,
declare an String array -
String[] array = new String[]{"first","second","third","fourth"};
Now, initialize the Adapter with the source.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,array);
Finally fetch the AutoCompleteTextView id from your xml and set the adapter.
AutoCompleteTextView mView = (AutoCompleteTextView)
findViewById(R.id.myAutoTextView);
mView.setAdapter(adapter);

how to display a list in android

I have a String countries[].
now when i click a button then on the onClick event this abc array is filled.suppose it is filled with the name of 10 countries.These 10 countries should be visible as a list so that i can choose any 1 country among the list. but i am not able to show them as a list.
My programme crashes if i use the following code :
ListView list = (ListView)findViewById(R.id.ListView);
list.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, countries));
As countries is filled later on the click of a button.So initially the countries array is empty and as the onCreate() is executed first it crashes.
I found the examples on net where there is pre-defined array.so it works.My array is filled in a onClick event.how can i display List at that moment???
Make list your member variable.
Move the setAdapter call inside your onClick()....That'll do.

Categories

Resources