How to add/remove items in spinner adapter - android

In my application I have two spinner which one is using same adapter.
Spinner mSpinner1 = findSpinnerView(R.id.spinner1);
Spinner mSpinner2 = findSpinnerView(R.id.spinner2);
SpinnerCustomAdapter mAdapter = new SpinnerCustomAdapter(this,List<Food> foodList);
mSpinner1.setAdapter(mAdapter);
mSpinner2.setAdapter(mAdapter);
How could I remove or add items in adapter? More specifically when I select one of item that selected item should be remove when selecting another that items should be removed but the previous should be appear again.

I recommend that you do the deletion of spinner items this way:
foodList.remove(foodList.get(itempostoremove));
SpinnerCustomAdapter mAdapter = new SpinnerCustomAdapter(this, foodList);
mSpinner1.setAdapter(mAdapter);

you can change the foodList,add or delete
and use mAdapter.notifyDataSetChanged() to refresh view

OnItemSelected will be fired by this:
this.getAdapter().remove(currentWagon);
this.getAdapter().notifyDataSetChanged();
this.setAdapter(this.getAdapter());

Related

Open a row in listview automatically (android project)

For example; I have long listview and it has 30 lines(items). I want to show this listview but after open the screen, it will show 15. lines. That is, the middle of the listview will be shown automatically. Is it possible? (NOTE: I don't want to show 15. line as first row.)
UPDATE:
i don't want to delete rows. I have listview and it works well. I want to show the middle of list. Scrool will flow until 15. row and i will see all of them but it will show 12. or 15. row when listview opens.
Bind your ListView to ArrayAdapter
final ListView lv = (ListView) findViewById(R.id.lv);
String[] fruits = new String[] {
"Cereus peruvianus",
"Bacupari",
"Beach Plum",
"Black raspberry"};
// Create a List from String Array elements
final List fruits_list = new ArrayList(Arrays.asList(fruits));
// Create an ArrayAdapter from List
final ArrayAdapter arrayAdapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, fruits_list);
// DataBind ListView with items from ArrayAdapter
lv.setAdapter(arrayAdapter);
Remove / Delete first item from List. You may remove multiple in a loop.
fruits_list.remove(0);
// Notify adapter
arrayAdapter.notifyDataSetChanged();
Edit:
// For scrolling to specific item in your list
lv.smoothScrollToPosition(15); // Here 15 is the position of the item
Answer is lv.setSelection(15);

To set the positions of items in spinner

I am populating my spinner from database. I have a collection of mobile brands. I have added "Add a new brand" also... But when I am setting the spinner items from DB, it comes in somewhere middle.. I want it to go at the end.. Can i do it? if yes, how? please help, thanks in advance.
Spinner brand;
brand=(Spinner)findViewById(R.id.spinner_brand);
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> Brand = db.getBrands();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Brand);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
brand.setAdapter(dataAdapter);
}
Add time column in your brand table and on querying sort by time. So, you will get last added item at last in spinner.
Create an ArrayList and add all your database items to it. and then add your "Choose a brand" string to it. and then pass the ArrayList to Spinner.
Although I should warn you, you are "probably" doing it wrong. If you add "Choose a brand" item to spinner, it will also be selectable, which you might not want :)

How to ArrayAdapter mixed add item?

I have a ArrayAdapter. I want to item with mixed. For example;
ArrayAdapter<String> Adapter = new ArrayAdapter<String>();
Adapter.add("Table");
Adapter.add("Desk");
Adapter.add("Pen");
Adapter.add("Computer");
Adapter.add("Mouse");
Adapter.add("Book");
I am adding items my ArrayAdapter. But I want to after adding, I add other item on under "Pen" item. For example;
Adapter.add("New Item");
But this item is last. I want to this item is adding under "Pen" item. How can I do ?
Use insert(T object, int index) instead. Refer http://developer.android.com/reference/android/widget/ArrayAdapter.html

spinner with no select option

I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.

How to remove a particular Item from a spinner

In my application i have spinners,if there are 10 items in a spinner how can i delete some specific item (like 3rd or 4th) from that spinner i used below code but not succeeded.
for(int i = 0;i<3;i++) {
Object t= cropT.getItem(i);
((ArrayAdapter<String>) cropT).remove((String) t);
spinnerCropType.setAdapter(cropT);
}
You dont need to set adapter everytime your delete items from it. In fact, after removing items from your spinner, you need to call notifyDataSetChanged() method on your adapter to refresh the spinner
//for example
adapterSpinner.notifyDataSetChanged();

Categories

Resources