How to update an Spinner dynamically correctly? - android

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()

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.

How to perform a task on selecting spinner item

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}

List.Remove always removes last item for ListView

When i try to remove a specific item from a list View:
buyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tempToken -= selPerk.cost;
plrPerks.add(selPerk);
String tokStr = String.valueOf(tempToken);
tkn.setText(tokStr);
shopItems.remove(selPerk);
selPerk = new Perk();
perkDialog.dismiss();
}
});
It always seems to remove the last item. This is where i open the dialog:
perks.setClickable(true);
perks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Perk perk = (Perk) perks.getItemAtPosition(position);
showItem(perk);
}
});
}
This is the show Item function:
public void showItem(Perk perk) {
if (tempToken >= perk.cost) {
selPerk = perk;
How do i remove a specific item from a list and list view respectively?
Thanks for your time :)
In "setOnItemClickListener" listener, you are getting the perk object. So you can remove that object from your list like this-
shopItems.remove(perk);
and then you can call-
your_adapter.notifyDataSetChanged();
to refresh your listview.
To remove a specific item from a list view, your can call removeView(View toBeRemoved) if you have a reference to the view you wish to remove. If you have the index, you can call removeView(int index).
http://developer.android.com/reference/android/widget/ListView.html
You can remove a specific item from a list in the same way, using remove(Object item) or remove(int index).
http://docs.oracle.com/javase/7/docs/api/java/util/List.html
Hope this helps!
I fixed it. Whenever i removed an item i had to do it like so:
shopItems.remove(selPerk);
perk_adapter.notifyDataSetChanged();
So i had to notify my listeview adapter that i removed an item.

Spinner does not get selected after setting adapter

I have two spinner in dialog. Second one is dependent on first one. I want to bind it and then select it in edit mode. All works fine but second spinner does not get selected. However it get selected when I open my dialog next time.
Here is a part of my code.
ArrayAdapter<String> myAdap1 = (ArrayAdapter<String>) spnForeignKeyTable
.getAdapter();
int spinnerPosition1 = myAdap1.getPosition(objcolumn_schema
.getForeignKeyTable());
spnForeignKeyTable.setSelection(spinnerPosition1);
// Bind Column Spinner.Second spinner
dblist = DBAdapter.getColumns(pf.getString("dbid", ""),String.valueOf(objcolumn_schema.getForeignKeyTableID()));
ArrayAdapter<String> adpf = new ArrayAdapter<String>(
column.this, android.R.layout.simple_spinner_item,
dblist);
adpf.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnForeignKeyColumn.setAdapter(adpf);
int spinnerPosition2 = adpf.getPosition(objcolumn_schema.getForeignKey());
spnForeignKeyColumn.setSelection(spinnerPosition2);
for changing selected item in second spinner when first spinner selection change you will need to set setOnItemSelectedListener for first spinner as:
spinnerPosition1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v,
int position, long id)
{
// change second Spinner selection here
}
public void onNothingSelected(AdapterView<?> arg0)
{
//
}
});

Spinner is activated at the start of my activity

I'm trying to use the spinner object in my app, but I'm having trouble, in fact, it is activated at the start of my activity. While I wish it was enabled when was clicked and selected the desired item.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.ElementaryOperations, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
index1 = s1.getSelectedItemPosition()+1;
if (index1 == 3){
finish();
startActivity(prod);//
}
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
spinner listener OnItemSelectedListener is called when you start activity due to get default value.i mean first element and zeroth index. so you can check if position is zero then dont anything.but mind that you cant achieve default selected value.if you put condition for zero.
if cant work proper than see this question

Categories

Resources