How to perform a task on selecting spinner item - android

I want to exectute Async task on selecting a spinner item and add some data to another spinner. It works well if i select a item. But it automatically executes AsyncTask even when not selecting. This is what i have tried
district
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
ssservice));
// Spinner on item click listener
district
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// ed_spinner = district.getSelectedItem().toString();
new NetCheck2().execute();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
How to execute Async Task only after I click on spinner item. Please help me.

The spinner is badly designed. Unfortunately when you call spinner.setOnItemSelectedListener it automatically calls onItemSelected. This is not a problem with your code, the spinner is designed that way. I know its bad, but this is how it works.
Workaround
If suppose your spinner has 4 entries, add one more entry at top i.e. 0th position and now when spinner is created onItemClicked is called and it will have position 0. So just put a if condition that
if (!(position == 0)){//do your stuff}

Related

How to disable and enable Spinner items on user click

I am developing an android app, which asks security questions after Sign Up for forgot password purpose, which has 10 questions in total. User can select any 3.
I have 3 Spinner for 3 question. Once user select the question from first spinner, second and third spinner should not have them in their list. Please help me to disable or remove that from the list.
screen shot of the activity
First, set a boolean check if it is the first time a spinner is selected. Store the selected item, so that you can add them on question change later.
Boolean ifFirstCheck = true;
String storeItem = "";
Then, Use below code:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(!ifFirstCheck){
listSp2.add(storeItem);
sp2adapter.notifyDataSetChanged();
listSp3.add(storeItem);
sp3adapter.notifyDataSetChanged();
}
String selectedItem = spinner1.getSelectedItem().toString();
listSp2.remove(selectedItem) // Get selected value from spinner1 and remove thar item from spinner2
sp2adapter.notifyDataSetChanged(); // Notify adapter of spinner2 to that dataset has been changed
listSp3.remove(selectedItem)
sp3adapter.notifyDataSetChanged();
storeItem = selectedItem;
ifFirstCheck = false;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Do as above for rest of the spinners.Hope this helps.
I assume you are using an Adapter for the Spinners. If you use an ArrayAdapter and each adapter holds the same list of items, you can just remove the selected items from the list and notify the adapters to update.

android listactivity onCheckedChangeListener

I'm developing an app and I have one ListActivity, which has choice mode set to choice_mode_multiple. now i want to override method, which is called when one item is checked/unchecked, and I've found out that onCheckChanged() method is implemented only for RadioGroup and compund Button. how can I override something like this in ListActivity? or do I have to implement my own Adapter? thanks
Set onlistitemclick listener to listview , then in itemclick method you can get mylistView.getCheckedItemCount() , write the code of the operation to be performed.
If this is not clear let me know what you want to implement when item is checked.
mylistView = (ListView)customView.findViewById(R.id.mylist);
mylistView.setAdapter(mFolderAdapter);
mylistView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(mylistView.isItemChecked(arg2)){
//Dooperation
}
// or
if(mylistView.getCheckedItemCount()>1){
//Dooperation
}
}
});
Use isItemChecked (int position) or setItemChecked(int position, boolean value) if you using standart array adapter and SINGLE or MULTIPLE choise mode. http://developer.android.com/reference/android/widget/AbsListView.html#isItemChecked(int)
Also you can create your custom adapter and inflate row layout with checkbox that can hold onClickListener and CheckedChangedListener.
Use search to find example.

How to update an Spinner dynamically correctly?

I have a spinner with a custom adapter displaying objects from a database.
When the object list changed I create a new adapter with the List and apply it on the spinner. Afterwards the first item is selected, so I tried this:
// 5th item selected
int pos = spinner.getSelectedItemPosition();
spinner.setAdapter(newAdapter);
// 0th item selected
spinner.setSelectedItem(pos);
// 5th item is selected
But the GUI does still show the first item?
spinner.invalidate() did not help.
Is this the correct way to achieve what I want? I really could not find any information on this behavior.
Solved: I guess the main problem was the custom spinner adapter. This works fine now
if (spinner.getCount() > 0) {
pos = spinner.getSelectedItemPosition();
}
MySpinnerAdapter adapter = new MySpinnerAdapter(context, myNewObjects);
spinner.setAdapter(adapter);
spinner.setSelection(pos); // needed
adapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
spinner.setSelectedItem(5);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Did u tried notifydatasetChanged() or notifydatasetInvalidate() method of adapter.
newAdapter.notifydatasetChanged()

Spinner OnclickListener event executes twice, how to handle both events

Spinner OnclickListener event executes twice -
Spinner initialization
User selected manually
where as implementation of listener is as :
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Problem definition
I want to save user selected text into data storage, when user choose any item from spinner, and I am able to do this. But my another task is that to show previously selected item (access from data storage) as selected item in spinner, but each time when I call spinner's activity, spinner shows first item as default selected item, and also in data storage it make change previous item to default.
How can I make difference between 'Spinner initialization' and 'User selected manually' events?
You have to handle both events logically. As these references (Android Spinner selection, problem on spinner) says that you have to use flag variable to handle this, I am putting a code sample.
Hope this will help you to clear your logic.
public class TestActivity extends Activity {
//Checks report spinner selection is default or user selected item
private boolean isDefaultSelection;
//Spinner setup
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// Set true at onCreate
isDefaultSelection = true;
spinner = (Spinner) findViewById(R.id.id_of_spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String> (this, R.layout.drop_down_custom_row, data);
//Implement custom view for drop down of spinner
//spinnerAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(isDefaultSelection) { //If spinner initializes
spinner.setSelection("Set_here_id_of_data_item_from_storage_which_was_previously_stored");
isDefaultSelection = false;
} else { //If user manually select item
int itemPosition = spinner.getSelectedItemPosition();
//Write here code to store selection (itemPosition) of user into data storage
}
}
public void onNothingSelected(AdapterView<?> parent) {
//User selected same item. Nothing to do.
}
});
}
}
Hope it will clear your doubt.
You can call the setSelection at the same time that the items are added to the adapter, see this example: How to avoid onItemSelected to be called twice in Spinners

Need help to delete selected item from listview

I want to delete a selected item from the list view;
actually I want to perform this operation from the context menu. Everything is going fine but I'm not able to delete that item.
Please give me some suggestions or examples to remove item from the listview
I have used like this in my code, it can delete multiple items from the list
ListView lv_ArchivePartylist;
ArrayList<Parties> select_archived_party;
lv_ArchivePartylist = (ListView)findViewById(R.id.archive_ListView01);
lv_ArchivePartylist.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
if(view.findViewById(R.id.img_chkbox_archive).getVisibility()==TextView.GONE)
{
view.findViewById(R.id.img_chkbox_archive).setVisibility(TextView.VISIBLE);
Toast.makeText(ctx_archive, "Name="+archived_parties.get(position).getPartyTitle(), Toast.LENGTH_SHORT).show();
select_archived_party.add(archived_parties.get(position));
}
}
});
Then I've declared one button of "Delete" and on it's On ClickListener method, it calls the code from the database(In your case it may be Arraylist or array) to delete the items selected in Arraylist "select_archived_party". Hope it helps :-)

Categories

Resources