I'm trying to connect actions in Spinner with AutoCompleteTextView. My goal is when user selects some item in spinner, AutoCompleteTextView suggestion list needs to be changed.
Example:
Spinner items: Cats, Dogs, Horses
String1: cat1, cat2, cat3, ...
String2: dog1, dog2, dog3, ...
String3: horse1, horse2, horse3 ...
So when user chooses 'Dogs' in spinner and after that clicks on AutoCompleteTextView, he will get following suggestions after he begins to write: dog1, dog2, dog3. Similar for Cats and Horses.
I can't find a solution for this problem.
I tried to put onClickListener to AutoCompleteTextView but it seems that doesn't work.
autoCompleteTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some actions
});
And there should be some better solution because after the screen opens that AutoCompleteTextView is already selected.
Check out the API for AutoCompleteTextView - http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
The suggestions are populated from a data adapter, so you could modify the data adapter for your autoCOmpleteTextView after the user makes their spinner selection:
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String[] choices;
// set the adapter for the autoCOmpleteTextView here based on what was selected
if(DOGS) {
choices = new String[] {"dog1", "dog2", "dog3"};
} else if (CATS) {
choices = new String[] {"cat1", "cat2", "cat3"};
}
// etc...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, choices);
myTextView.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
// nothing
}
});
Related
I have two spinners in which the first spinner uses autocomplete textview , when first spinner item is selected in normal way without using autocomplete text view the second spinner loads the corresponding value according to the first spinners selection. My issue is that when autocomplete textview is used for selecting first spinner value the second spinner is not loaded with the values according to first spinners selection. can any one please tell me a solution
public void vesselspinnerHandler(){
//Setting Class to Spinner
final ArrayAdapter<String> vesselspinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, vessellist);
vesselspinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vesselspinner.setAdapter(vesselspinnerAdapter);
vesselspinnerAdapter.notifyDataSetChanged();
final AutoCompleteTextView searchvessel=findViewById(R.id.searchvessel);
final AutoCompleteTextView selves = findViewById(R.id.selectedvessel);
ArrayAdapter adapter1 = new ArrayAdapter(selection.this,android.R.layout.simple_spinner_dropdown_item,vessellist);
selves.setAdapter(adapter1);
selves.setDropDownVerticalOffset(50);
vesselspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selves.setText(vesselspinner.getSelectedItem().toString());
searchvessel.setText(selves.getText().toString());
getdate();//second spinner call
return;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Here is my code for my spinner.. i want it to save to my database by using setText but theres no option for setting it into setText.. this is how i want it to save on my database spinpstat.setText(student.status);
this is how i want it to declare in my onClick(View view)
student.status = spinpstat.getText.toString(); but it gives me following error
spinpstat = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapterstat = ArrayAdapter.createFromResource(this, R.array.statuslist, android.R.layout.simple_spinner_item);
adapterstat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinpstat.setAdapter(adapterstat);
spinpstat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
projstat = spinpstat.getSelectedItem().toString();
spinpstat.setSelection(position);
parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
this is the data i saved .. i set the spinner status in old student
but this how the data result in review of the data i saved.. my spinner instead of status is old student it set as new student which is the first value in my spinner list
any help will do.. thanks in advance..
Use Spinner.setSelection to set selected item from Spinner like:
spinpstat.setSelection(adapterstat.getPosition(student.status));
And get selected item from Spinner on onClick as:
student.status = spinpstat.getSelectedItem().toString();
In my application there are lot's of views(spinner, buttons, editText, textView, ImageButton etc).
After selecting one value in the spinner it not reflect the value which I selected. The old value is till there on the spinner. But when I tap on other views like editTest then spinner value automatically updated.
I think no need to mention the code because normal spinner is there which populating data from ArrayAdapter and I am doing small task after selecting value.
CODE:-
spinnerWard = (Spinner) findViewById(R.id.spinnerWard);
aAdapterWard = new ArrayAdapter<WardList>(this,
android.R.layout.simple_spinner_item, listWard);
aAdapterWard.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerWard.setAdapter(aAdapterWard);
spinnerWard.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
intWardPosition = pos;
intFinalWardId = listWard.get(pos).getId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
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 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