I am working on an app, which has a spinner in two activities. In the first activity the user can edit the spinner item and save the value. I used an ArrayAdapter and a onItemSelectedListener to store the value in the database. This works fine.
In the second activity the spinner with the changes of the user should be shown. Can anybody tell me how to solve this problem? If you need some more information, please ask, because I am a newbie on android.
This is the code for the first activity:
#Override
protected void onCreate(final Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_spinner1);
final Spinner spinner1 = (Spinner)findViewById(R.id.sp_countries);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries,
R.layout.spinner1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id)
{
mSave.countries = spinner1.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
Thanks for your answers in advance.
if you get the value from the updated database and set the whole values through setAdapter()
method in the spinner or notify the spinner to change something in database.
Related
firstly I'm a newbie to using this program so any help is appreciated. I need spinner 1 to show 7 cities the user can choose from and spinner 2 to show the same 7 cities that the user can select.
I have some code below but my tutor says it's wrong and it won't work for the second spinners I tried to find a way but can seem to do so
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Cities, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
}
spinner 1 is meant to be the start destination and spinner 2 is meant to be the destination they are going to. once the user selects the options it tells them the distance from spinner 1 to spinner 2.( the distances are already given to us in a table)
Your code contains only one spinner.
Spinner spinner = findViewById(R.id.spinner1);
Define one more spinner in XML and assign the same adapter to new spinner and set a separate OnItemSelectedListener each spinner.
Get the values(city names) from each spinner and calculate the distance.
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();
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
}
});
I am trying to save spinner selected value, but i am getting like below i shown when i retrieve the details. Anybody know what is the problem.
Spinner:android.widget.Spinner#43e807a0
Have you used the getSelectedItem() inside setOnSelectedListner? If not, do as shown below:
mPres_doctor.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
String pres_doctor = mPres_doctor.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
The result is displaying as an object value, usually I follow the below method to get the spinner values:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner, android.R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
strings.xml
<string-array name="spinner">
<item>Dev</item>
<item>Stieve</item>
<item>John</item>
<item>Britto</item>
</string-array>
I'd like to show the text containing result of addition of two values: one from onespinner selected item, and another from twospinner selected item. But eclipse shows an error in line
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
What's the matter? Full code goes below.
public class photographer extends Activity implements OnItemSelectedListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner onespinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> unitadapter = ArrayAdapter.createFromResource(
this, R.array.onespinner, android.R.layout.simple_spinner_item);
unitadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
onespinner.setAdapter(unitadapter);
onespinner.setOnItemSelectedListener(this);
Spinner twospinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> courseadapter = ArrayAdapter.createFromResource(
this, R.array.twospinner, android.R.layout.simple_spinner_item);
courseadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
twospinner.setAdapter(courseadapter);
twospinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
TextView text = (TextView)findViewById(R.id.result);
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
getSelectedItem is a method, but you are referencing it like an instance variable. You need to change your code to:
text.setText(onespinner.getSelectedItem() + twospinner.getSelectedItem());
You haven't posted the exact error, but I'm guessing that your problem is that you are trying to make a reference to onespinner and twospinner in onItemSelected and those two objects are not in the scope of that function; they are declared in onCreate.
Now, the View view argument of onItemSelected is the spinner that was clicked, but you need a reference to both spinners (not just the one that was selected). Easiest way to do this is to globally declare oneSpinner and twoSpinner and that should solve your problem.
EDIT: Also what Ted Hopp said.