EDIT: i have added in all of my code (excluding package and imports.....) and if i try to run it it crashes...... any ideas why?
public class BaseConverter extends Activity {
/** Called when the activity is first created. */
int inputBase;
int outputBase;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner input_spinner = (Spinner) findViewById(R.id.InputSpinner);
Spinner output_spinner = (Spinner) findViewById(R.id.OutputSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.base_numbers_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
input_spinner.setAdapter(adapter);
output_spinner.setAdapter(adapter);
input_spinner.setOnItemSelectedListener(new InputItemSelectedListener());
output_spinner.setOnItemSelectedListener(new OutputItemSelectedListener());
}
public class InputItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id)
{
/* switch (Integer.parseInt(parent.getItemAtPosition(pos).toString())
case ((Integer)parent.getItemAtPosition(pos)).intValue();
inputBase = 2;
break;
case 8:
inputBase = 8;
break;
case 10;
inputBase = 10;
break;
case 16;
inputBase = 16;
break;
*/
Toast.makeText(parent.getContext(), "You selected input base " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
public class OutputItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "You selected output base " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
and now need to have a switch - case scenario that all revolves around what the value they selected it. they are all numbers (the choices) and are stored in an Integer array. How do i set up that switch-case correctly? i tried doing a simple thing like
case ((Integer.parseInt(parent.getItemAtPosition(pos).toString())
So i figured it out. you need to make the array a STRING array and the use:
ArrayAdapter adapter = new ArrayAdapter
etc.....
then use
Integer.parseInt(parent.getItemAtPosition(position).toString());
to find the numerical value of whatever you selected. NOTE: it must be all numerical or it will give you an error.
Related
I want the spinner to open up by itself when I run the activity, so I'm using the performClick() method but it's showing this error:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
What should I do?
Here is my code
public class FacilityComplaint extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner1;
private static final String[] suggestions = {"Select from suggestions", "Switch not working",
"Switch faulty", "Switch light not working", "Switch handle faulty"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facility_complaint);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(FacilityComplaint.this,
android.R.layout.simple_spinner_item, suggestions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
spinner1.performClick();
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
//do something
break;
case 1:
//do something
break;
case 2:
//do something
break;
case 3:
//do something
break;
case 4:
//do something
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
For spinner you have don't need to implement "AdapterView.OnItemSelectedListener" Create like this .
spinner1 = (Spinner) findViewById(R.id. spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
suggestions, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.performClick();
// Get selecte index or Suggestions
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String selectedSugg = suggestions[pos];
Log.d("Suggestions ", selectedSugg);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Can someone please help me. I have just started Android development and I want to create an app which has a drop down menu. There seems to be two errors in my code which I am unable to resole. The errors are at implements OnItemSelectedListner and at spinner.setOnItemSelectedListener(this);
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{
int Cups = 1;
int Price = 1;
int Sum = 0;
private Spinner spinner;
private static final String[]paths = {"item 1", "item 2", "item 3", "item 4", "item 5"};
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position)
{
case 0:
Price=0.5;
break;
case 1:
Price=1;
break;
case 2:
Price=2;
break;
case 3:
Price=3;
break;
case 4:
Price=4;
break;
}
}
The first Error is to implement the correct OnItemClickListener
implements AdapterView.OnItemClickListener
The second is to override the correct method from the listener which is
#Override
onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
The items of the first one are defined in xml (<string-array>) but the second one should present different items arrays of strings according what is selected on the first...
The possible array of strings for the seconds are fetch from a web service using an AsyncTask (This part is working). In my onPostExecute(Void result) I have this:
private class GetInfoTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(StateTabActivity.this);
//...
#Override
protected void onPostExecute(Void result) {
Log.d("StateTabActivity","onPostExecute");
sectorsArray = getSectorsName(); // sectorsArray is an array of strings
roomsArray = getRoomsName(); // roomsArray is an array of strings
subcategorySpinnerAdapter = new ArrayAdapter<String>(StateTabActivity.this, R.layout.my_spinner,sectorsArray);
subcategorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subCategorySpinner.setAdapter(subcategorySpinnerAdapter);
dialog.dismiss();
}
}
On the onCreate() of my activity I have:
Spinner categorySpinner = (Spinner) findViewById(R.id.statetab_category_spinner);
ArrayAdapter<String> categorySpinnerAdapter = new ArrayAdapter<String>(this, R.layout.my_spinner,getResources().getStringArray(R.array.array_category));
categorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setAdapter(categorySpinnerAdapter);
subCategorySpinner = (Spinner) findViewById(R.id.statetab_subcategory_spinner);
categorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.d("StateTabActivity","in onitemselected");
switch (arg2) {
case 0:
//I want to set here the items of sectorsArray to be displayed on the second spinner (subCategorySpinner)
break;
case 1:
//I want to set here the items of roomsArray to be displayed on the second spinner (subCategorySpinner)
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
So my question is: What should I do to bind the right array to the second spinner, according what is selected on the first one?
This is my code to get district list as per selected state.
final Spinner state = (Spinner)_activity.findViewById(R.id.state);
final Spinner district= (Spinner) _activity.findViewById(R.id.district);
_activity.findViewById(R.id.name_of_city);
state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {
if(i == 0){
districtAdapter =new ArrayAdapter<CharSequence>( _activity , android.R.layout.simple_spinner_item, **DistrictList**.AndraPradesh);
//DistricList is another class.its code given below
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
district.setAdapter(districtAdapter);
if(_viewState == SAVED_VIEW){
district.setSelection(getArraySpinner(DistrictList.AndraPradesh,_initialValues.getAsString("District")),true);
}
}
In DistricList class,
public class DistrictList {
public static final String[] AndraPradesh = new String[] {"Adilabad",
"Anantapur",
"Chittoor",
"East Godavari",
"Guntur",
"Hyderabad",
"Karimnagar",
"Khammam"};
}
}
i want a spinner when select an item from the spinner its should load the corresponding java page that has been set.can we load a java page on selecting an item from spinner in android if so how can we implement this can any one provide some sample code
You can
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
switch(position) {
//Use cases to set Intents
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do Nothing
}
});
String[] items ={“One”,“Two”,“Three”,“Four”,“Five”};
Spinner sp = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item,items);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
//call first class
break;
case 1:
//call second class
break;
case 2:
//call third class
break;
case 3:
//call fourth class
break;
default:
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//nothing selected from spinner
}
});
just use spinner.onItemSelectedListener(new OnItemSelectedListener())
and in onItemSelected(AdapterView adapterview, View view, int position, long id) method body write your logic to start new activity based on the position.
So i'm starting a application and the first thing to do is make a description of a city when the city is selected. I can show the description but it make the description of all the cities on the same time and it don't come out when i select another city : it add more and more .
this is my code :
public class Main extends Activity implements OnItemClickListener, OnItemSelectedListener {
TextView description;
Spinner spin;
ArrayAdapter adapter_city;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
description = (TextView)findViewById(R.id.description);
spin = (Spinner)findViewById(R.id.spin);
adapter_city = ArrayAdapter.createFromResource(this, R.array.Cities, android.R.layout.simple_spinner_item);
adapter_city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_city);
spin.setOnItemSelectedListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
switch(position){
case 0 : description.append(getString(R.string.Paris));
case 1 : description.append(getString(R.string.Chicago));
case 2 : description.append(getString(R.string.NewYork));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
thank you .
Instead of:
description.append(getString(R.string.Paris));
Why don't you just use:
description.setText(R.string.Paris);
Here is what it should look like:
case 0 : description.setText(R.string.Paris);
break;
case 1 : description.setText(R.string.Chicago);
break;
case 2 : description.setText(R.string.NewYork);
break;
You need to add breaks after every case or execution will continue to all of them when matched:
switch(position){
case 0 : description.setText(R.string.Paris); break;
case 1 : description.setText(R.string.Chicago); break;
case 2 : description.setText(R.string.NewYork); break;
}
Try using description.setText(CharSequence text) instead of append. Append is for appending text.