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.
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) {
}
});
}
I build a spinner in which i put element through array list and .i store the value of get selcted item from spinner in a variable,in my spinner it having 15 element,now i want to try when a specific item is selected from spinner then a dialog box is open with edit text and button then user can edit and save it in spinner.how can i do this.
my code for spinner is
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faultid);
addItemsOnSpinner2();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection()
{
Spinner faultSpinner = (Spinner) findViewById(R.id.spinner1);
mspinner.setOnItemSelectedListener(new
CustomOnItemSelectedListener());
}
public void addItemsOnSpinner2() {
ArrayList<String> faulttypespinner = new ArrayList<String>();
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("BD-Ballast Deficiency");
faulttypespinner.add("SE-Super elevation on curve");
faulttypespinner.add("LP-Loose Packing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("BA-Bridge");
faulttypespinner.add("LC-Level Crossing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("P and C-Point n Xing");
faulttypespinner.add("OTH-Other Defect");
faulttypespinner.add("SEJ-SEJ");
faulttypespinner.add("WEED-Weed on Cess");
ArrayAdapter<String> faultadapter = new ArrayAdapter<String>
(mConetxt,android.R.layout.simple_spinner_item, faulttypespinner);
faultadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mspinner.setAdapter(faultadapter);
}
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " +
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
You'll need to implement Custom Adapter, where you'll have a method something like :
public void changeItemValue(int position, String newValue){
Item itm = getItem(position);
itm.setValue(newValue);
notifyDataSetChanged();
}
When you open the dialogBox onSelectedItem, you'll have to pass an instance of your spinner adapter to a dialog and then to the listener of a dialog button, where you'll call changeItemValue method on button click.
Why u r using custom adapter,when click on spinner get the position of clicked item and show the pop up and on button click set the value to the faulttypespinner arraylist using index stored previously. then add notifydatasetchanged for adapter to update the spinner thats all.
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.
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.
I am trying to connect 2 spinners together. Meaning, the items inside 2nd spinner will depend on whatever item is chosen for the 1st spinner.
This is the code inside the main java file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
This is the code inside MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
So the onItemSelected function will print out the item that was chosen in 1st spinner on the screen. However, I can't figure out how to create the 2nd spinner fully based on the value inside 1st spinner.
I know that there should be something needed to be done inside onItemSelected, but I just can't figure it out since I am a newbie in Java Android.
Can you guys assist me on this?
Thank you.
You should just do something very similar to what you do in onCreate, only with the other spinner. For example:
List<String> values = findValuesBySelection(parent.getItemAtPosition(pos).toString());
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, values, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);