I have to populate states depending on the selected country. I also have to capture the items selected in other spinners. But the listener methods are not getting called.
countriesSpinner.setOnItemSelectedListener(this);
statesSpinner.setOnItemSelectedListener(this);
yearSpinner.setOnItemSelectedListener(this);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
switch(view.getId()){
case R.id.countriesSpinner:
{
Log.i("selected country",selectedCountry);
selectedCountry = parent.getItemAtPosition(position).toString();
populateStates();
ArrayAdapter stateAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, states);
statesSpinner.setAdapter(stateAdapter);
}
break;
case R.id.statesSpinner:
{
selectedState=parent.getItemAtPosition(position).toString();
}
break;
case R.id.yearSpinner:
{
selectedYear = parent.getItemAtPosition(position).toString();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
you got the Switch Id wrong...your need to use parent.getId()
try this:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
switch (parent.getId())
{
case R.id.countriesSpinner:
Log.i("selected country",selectedCountry);
selectedCountry = parent.getItemAtPosition(position).toString();
populateStates();
ArrayAdapter stateAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, states);
statesSpinner.setAdapter(stateAdapter);
break;
case R.id.statesSpinner:
selectedState=parent.getItemAtPosition(position).toString();
break;
case R.id.yearSpinner:
selectedYear=parent.getItemAtPosition(position).toString();
break;
}
}
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) {
}
});
Why is this code not working? is there any problem with switch case?
I have 2 spinners (of branch and semester). When a branch is selected it must show a Toast of selected branch and same with the semester.
public class MainActivity extends Activity implements OnItemSelectedListener, OnClickListener{
Spinner branch, semester;
Button go;
TextView branchText, semText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
branch = (Spinner) findViewById(R.id.spinner1);
semester = (Spinner) findViewById(R.id.spinner2);
go = (Button) findViewById(R.id.button1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.Branch, android.R.layout.simple_spinner_item);
branch.setAdapter(adapter);
branch.setOnItemSelectedListener(this);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.Semester, android.R.layout.simple_spinner_item);
semester.setAdapter(adapter1);
semester.setOnItemSelectedListener(this);
go.setOnClickListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch (view.getId())
{
case R.id.spinner1:
branchText = (TextView) view;
Toast.makeText(this, branchText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner2:
semText = (TextView) view;
Toast.makeText(this, semText.getText(), Toast.LENGTH_SHORT).show();
break;
}
}
You should get the id of selected item by "AdapterView parent" not "View view".
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch (parent.getId()){
case R.id.spinner1:
branchText = (TextView) view;
Toast.makeText(this, branchText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner2:
semText = (TextView) view;
Toast.makeText(this, semText.getText(), Toast.LENGTH_SHORT).show();
break;
}
}
Change Toast.makeText(this, branchText.getText(), Toast.LENGTH_SHORT).show(); to Toast.makeText(this, branchText.getText().tostring(), Toast.LENGTH_SHORT).show();
change all branchText.getText() to branchText.getText().tostring()
Reference branch array and use position to retrieve branch text:
String[] branchArr = getResources().getStringArray(R.array.Branch);
case R.id.spinner1:
Toast.makeText(this, branchArr[position], Toast.LENGTH_SHORT).show();
break;
I am making an android application that uses a listview. I want to get the index number of an item once an item has been pressed(single-click). I've gone through several tutorials, but none seemed to help. How can i get the index number and pass it to a String. I do then want to delete it, but i'll manage that part my self. I only need the index number and pass it to a string. The code where i'll get the index number is straigt after the onCreate method. Please help and thanks in advance! This is the code that i am using:
public class NotesActivity extends ListActivity implements OnClickListener {
/** Called when the activity is first created. */
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
Button AddItemToListView, AddItemToListViewButton, CancelButton, DeleteButton;
LinearLayout AddItemToListViewLinearLayout, DeleteItemFromListViewLinearLayout;
public String DeleteIndexNumber;
static final String[] COUNTRIES = new String[] {
"Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes);
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
//This is where i need the index number to be passed to the string "DeleteIndexNumber"
DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
DeleteItemFromListViewLinearLayout.setVisibility(View.VISIBLE);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu meny) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listviewmenubuttons, meny);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.AddItemToListView:
AddItemToListViewButton = (Button)findViewById(R.id.AddItemToListViewButton);
CancelButton = (Button)findViewById(R.id.CancelButton);
DeleteButton = (Button)findViewById(R.id.DeleteButton);
CancelButton.setOnClickListener(this);
DeleteButton.setOnClickListener(this);
AddItemToListViewLinearLayout = (LinearLayout)findViewById(R.id.AddItemToListViewLinearLayout);
AddItemToListViewButton.setOnClickListener(this);
AddItemToListViewLinearLayout.setVisibility(View.VISIBLE);
break;
}
return true;
}
public void onClick(View src) {
switch(src.getId()) {
case R.id.AddItemToListViewButton:
AddItemToListViewEditText = (EditText)findViewById(R.id.AddItemToListViewEditText);
myList.add(AddItemToListViewEditText.getText().toString());
((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged();
AddItemToListViewEditText.setText("");
AddItemToListViewEditText.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
AddItemToListViewLinearLayout.setVisibility(View.GONE);
break;
case R.id.CancelButton:
DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
break;
case R.id.DeleteButton:
break;
}
}
}
notice the parameters for the onItemClick method
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
position will give you the index you desire..
Isn't it position parameter in onItemClick ?
The position parameter in onItemClick will give you the index of the clicked Item in the list.
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.
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.