select item in getView method adapter spinner - android

i have spinner that i create with my custom adapter and i want to select specify item in load spinner and do not use setSelection method for select specify item because i use AsyncTask for load data to spinner.
write this code in adapter :
spinner.setSelection(myposition);
this method work but when click on spinner and chose an item, dropDown not hidden.
my question is how can i select item when i creating the items spinner.
i means how can i select item in the getView method in adapter?
please help me.

whe loading data in asyncTask (e.g for loop to fill arrayList) you can note the position you need to select and after adding all the data to adapter call the select method on spinner within same ayncTask (postExecute).
And why don't your drop down hiding are you overriding the click function of spinner ?

Related

Android spinner drop down with non selected items only

How to show spinner drop down with only the items which is not currently selected like below images
Drop down list when spinner with FOLLOWING as current item is clicked
Drop down list when spinner with NEW as current item is clicked
There is no default setup for that usecase. You need the change the Data-Set of the SpinnerAdapter (Model) or change the Adapter used by your Spinner in the onItemSelected method and save the selected Item in a sparate variable. A Spinner always displays alle Items in from the Spinner Adapter. There is also not the option to add an emty cell.

Overriding Android ListView onItemClick causing item to be selected (checked)

I am trying to better understand the internal functioning of ListView as it pertains to selecting one or multiple items- it's actually amazing how difficult it was for me to even get this far in my understanding.
By default a regular click on a ListView item is setting the 'checked' state for that item to true. How do I override this behavior so this selection does not happen?
And more fundamentally, what are the underlying ListView mechanics here? Is the row view's default onClick then calling the ListView's onItemClick/LongClick handlers, or how does this click handling get sequenced?
I do want to allow a choiceMode of multipleChoice, but I only want to select it onItemLongClick. Overriding onItemClick does not change this behavior, and overriding the row view's onClick handler in the adapter getView() function seems to prevent the ListView onItemClick and onItemLongClick from ever happening.
Below is more detailed context on my application
My goal is to have my ListActivity display a ListView, which functions as follows:
Clicking an item performs a non-selecting action (expands the row to show more info)
Long clicking an item selects it. Selecting an item is indicated by highlighting the background of the row (as in the Gmail app)
You can select multiple items
My application structure is:
Activity is an extension of ListActivity
Adapter is an extension of ArrayAdapter<>
ListView row layouts are completely custom layouts (not any sort of built-in ListView row layout)
My understanding of the built-in functionality for ListView has me to the point where I am
setting choiceMode to multipleChoice
using the ListView 'checked' functionality for making and tracking the selections
using a custom selector as an 'activatedBackgroundIndicator' to show the highlighting (example here)
Keep an ArrayList to maintain ListView items selected position. When a ListView item is selected check in that ArrayList whether item position is in ArrayList or not. If item is not in ArrayList change state of Item to checked else change state to unchecked and remove the position object from ArrayList. This worked for me.

spinner selected item is highlighted while in drop down mode android

I have a spinner populated with custom view by extending BaseAdapter. Items are populated by overriding the getView() method in BaseAdapter
I need to find a way to distinguish the selected item in the drop down list only. I mean when the user clicks on the spinner the item previously selected will show in different color/ background etc.
I don't think there is a need to see the code because it's similar to many on the site.
Couldn't you just use setOnItemSelectedListener? It would record the position of the spinner, and you could find if the position change.
http://developer.android.com/reference/android/widget/AdapterView.html

Listview - list items updated with fetched data in background

Currently i am having listview with multiple items (First time, i fetch data with the use of AsyncTask), now i want to implement the below functionalities:
I am showing Option Dialog(With options Edit, Delete, Read) whenever user do long-click on any item, now if user select "Delete" option, at that time i want to remove item from the listview, listview should be displayed without that item meanwhile the data should also be fetched(in background) from the web.
Anybody knows, how do i implement such?
on the action of delete, delete the data from the source array, and call notifyDataSetChanged() on your adapter. and from the place where you call notifyDataSetChanged, you can do all the web stuff.
Use notifyDataSetChanged() over your Adapter
For display a dialog box when log press of listview item use onContextMenu() method in that onContextmenu u use the listview object and in that contaxt menu u write the code for display a dialog box with 3 options and next one remove item from listview when u click on the delete button in that dialog box is in delete button click listener remove the item from the listview and again set that remaining elements to that listview.

how to delete the particular selected item from the list of items in spinner

I followed http://www.designerandroid.com/?p=8 this one to add the values in the spinner.In it while we select the "clear spinner items" the whole events are will be delete. But i need to the selected particular item only want to delete.Any one can help me.. The sample code will help to me lot.
If you want to remove the selected item in the spinner:
adapter.remove((String)spinner.getSelectedItem());
adapter.notifyDataSetChanged();
where "adapter" is the adapter set to the spinner, it's as simple as that. :)
if adapter is out of scope you can get the adapter from the spinner, cast properly and remove the item:
((List<String>) spinner.getAdapter()).remove((String)spinner.getSelectedItem());
((List<String>) spinner.getAdapter()).notifyDataSetChanged();
To delete particular item from spinner you have to remove it from arrayadapter which you are using for filling it.
So first get the position of item you are wanting to delete.
Then get the object from arrayadapter from its position by method.
int pos = 0;
object t= m_adapterForSpinner.getitem(pos); // where m_arrayadapter is array adapter using for filling spinner
And then remove it from spinner by using following code:
m_adapterForSpinner.remove((CharSequence) t);
Then fill your spinner again with arrayadapter.

Categories

Resources