I have a profile activity where user select there country for the first time ,after that when user wish to change the country he will go to profile activity there he should see the previous selected country in the spinner and then he must be able to select another country from the spinner.
I am trying to update my profile activity ,I have a spinner named country in it..I am using set selected for getting the previous selected value .i am getting the value but when i am trying to change that value its not happening .
The Country_value is string
Below is the code for spinner :
country_adapter_list = new ArrayAdapter(MyProfile.this, R.layout.support_simple_spinner_dropdown_item,Country_listItems);
country = (Spinner) findViewById(R.id.sp_country);
country.setAdapter(country_adapter_list);
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
country.setSelection(Country_listItems.indexOf(country_value));
country_data = Country_listItems.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
OnItemSelected() method will select the item present in list at position.
The setSelection() will internally call the onItemSelected() to set the selected item in the spinner. So calling setSelection() inside onItemselected() will not help you.
Try below code.
#Override
public void onItemSelected(AdapterView<?> **adapterView**, View view,int position, long l) {
**adapterView**.setSelection(Country_listItems.indexOf(country_value));
country_data = Country_listItems.get(position);
}
Your code is working fine, but it will not set selection on Spinner if "country_value" is not found in arraylist that you set.
Video Link
Related
I try to explain by showing as many examples as possible, I chose a word from the spinner, for example it is "cat", after I select it in the spinner i.e. after I click it should also appear in the textview.
Spinner click "cat"
TextView change "cat"
I again click Spinner "dog"
TextView change "dog"
now my not full code:
```Spinner sababspinner = findViewById(R.id.Sababspinner);
sababspinner.setOnItemClickListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}```
all spinner texts in string.xml
Make an ArrayList with your spinner dataset, then in the onItemSelected method write something like textView.setText(dataset.get(position)).
I found error in findViewById in static function.
my function is--
public static void onYearSelect() {
Spinner yearSelector;
String yearName;
yearSelector = (Spinner) findViewById(R.id.year_submit);
yearName = yearSelector.getSelectedItem().toString();
}
is there any way to solve this error!
can i store value of Spinner in yearName as String
You need to add view inside function as parameter.
public static void onYearSelect(View view) {
Spinner yearSelector;
yearSelector = (Spinner) view.findViewById(R.id.year_submit);
yearName = yearSelector.getSelectedItem().toString();
}
The view which contains your layout data.
When you call that function add onYearSelect(rootView) here rootView have the layout view.
EDIT 1:
What is View here ?
A View in Android is a widget that displays something. Buttons, listviews, imageviews, etc. are all subclasses of View. When you say "change view" I assume you mean change the layout by using setContentView(). This usually only needs to be done once per activity. An Activity is basically what you are referring to as a screen.
You can read more here.
Set setOnItemSelectedListener on spinner object and inside onItemSelected() you can set value of yearName. Like below,
yearSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String yearName = parentView.getItemAtPosition(position).toString()
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}});
I followed steps in how to change value of textview according to selected item and here is a piece of the code
public void onCreate(Bundle savedInstanceState){.......
final TextView privacyTextView = (TextView) findViewById(R.id.eventPrivacy);
privacySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id3) {
final String selectedItem = parent.getItemAtPosition(position).toString();
privacyTextView.setText(selectedItem);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
list is :
<string-array name="privacy_levels">
<item>Everyone</item>
<item>Friends of Friends</item>
<item>Friends Only</item>
<item>Customize</item>
</string-array>
I have text view with value : privacy when running app it's automatically changed to Everyone -frist one on the list- so what is going wrong ?!!!
Add android:prompt in Spinner xml and set a name like
android:prompt="Privacy";
So first time it will show privacy then user can select from the drop down list.
Try this...!
final String selectedItem = parent.getSelectedItem().toString().trim();
privacyTextView.setText(selectedItem);
If you want to get first item as privacy then you need to add another item in your array as Privacy.
There is a attribute called android:spinnerMode this is used to get the spinner in two modes as dialog or dropdown.
where as the attribute android:prompt is to get the spinner Title or Heading after opening it(in Dialog mode).
Just try to this so you will get selected string:
String name = spinner.getSelectedItem().toString();
I have an AutoCompleteTextView. I set its adapter by extending a CursorAdapter.
After selecting an option from the dropdown, I like to know the position of the item, or the item, that was selected. At least, I want an id for fetching more data in Sqlite.
How can I do that?
Simply override the AutoCompleteTextView's OnItemClickedListener to find the position of the item in the dropdown list or the SQLite row id:
autoCompleteTextView.setOnItemClickedListener(new OnItemSelectedListener() {
#Override
public void onItemClicked(AdapterView<?> parent, View view, int position, long id) {
// Do whatever you want with the position or id
}
});
I am developing an android which has 4 spinners(A,B,C,D).
What I want to achieve is
If Spinner A is set to a value I want the remaining spinners to automatically change their value depending on the value of A
Like if I set Movie(in A)--->Ticket(in B)---->Place(in C)---->Time(D) to automatically fill in.
Thanking you
ChinniKrishna Kothapalli
I think you can set an OnItemSelectedListener to your Movie Spinner. The Spinner tutorial is an adequate example. Your OnItemSelectedListener may be like this:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if (parent.getItemAtPosition(pos).toString().equals("firstMovieName")) {
// set spinner B/C/D with the corresponding information of first movie
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
The onItemSelected method above may be too simple for your need. You need figure it out how to link the four spinners yourself, but May it helps :)