I am building an application in which i added a spinner where the user is supposed to select the month and with respect to change in month the edittext field also changes. I made a code by my side but it is not working also not showing any error.
My code is :
public class NewMemberRegister extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner4;
EditText e1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_member_register);
spinner4=(Spinner)findViewById(R.id.spinner4);
ArrayAdapter adapter3
=ArrayAdapter.createFromResource(this,R.array.period,android.R.layout.simple_spinner_item);
spinner4.setAdapter(adapter3);
spinner4.setOnItemSelectedListener(this);
e1=(EditText)findViewById(R.id.amount);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String str= parent.getItemAtPosition(position).toString();
e1=(EditText)findViewById(R.id.amount);
if (spinner4.getItemAtPosition(position)==0){
e1.setText(" ");
}else if(spinner4.getItemAtPosition(position)==1){
e1.setText("100");
}else if(spinner4.getItemAtPosition(position)==2){
e1.setText("1500");
}else if(spinner4.getItemAtPosition(position)==3){
e1.setText("3500");
}else if(spinner4.getItemAtPosition(position)==4){
e1.setText("6000");
}else if(spinner4.getItemAtPosition(position)==5){
e1.setText("10000");
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
Any Ideas for this?...Comments are Welcome !
Here:
if (spinner4.getItemAtPosition(position)==0){
}else
..
Causing issue. because comparing non Integer type to int value.
As see here:
AdapterView.getItemAtPosition(int) : method takes index of item as int but return Object type value instead of int.
So use Integer.parseInt to get int representing of Object which is returned from getItemAtPosition before comparing it:
if (Integer.parseInt(spinner4.getItemAtPosition(position).toString())==0){
e1.setText(" ");
}else
.....
Do same for other conditions.
Related
It's Will Be Good For Your Help,,
i have 2 Spinner + Mediam Text + Button
First Spinner "hint" [where i'm Now]
(Los angeles, California, London)
Second Spinner "hint" [i Want To Go]
(Florida, Origin, London)
When i Click On Button, Will Show Result in Text Box ..
Example:
When I choose I'm in "Los angeles" And Want To Go "London" Then Result Will Show in Text Box, that Says "If you Want Go To London You Have To Travel with Airplane".
Please explain because I'm Lv1 in Android Studio.
Try this code..!
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
Spinner temp1,temp2;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
t1=(TextView)findViewById(R.id.t1);
temp1=(Spinner)findViewById(R.id.temp);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp1.setAdapter(adapter);
temp2=(Spinner)findViewById(R.id.temp2);
ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp2.setAdapter(adapter2);
temp1.setOnItemSelectedListener(this);
temp2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner temp1=(Spinner)parent;
Spinner temp2=(Spinner)parent;
if (temp1.getId()==R.id.temp) {
String item = parent.getItemAtPosition(position).toString();
t1.setText(item);
}
if (temp2.getId()==R.id.temp2) {
String item = parent.getItemAtPosition(position).toString();
t1.setText(item);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I tryed to create two dependent spinners. I know there is more than enough of this type of question, but nothing fixed my problem. It seems onItemSelected does not fire at all. I want the second (townships_spinner) change when first spinner (divisions_spinner) is selected or changed. Let's say I have states and cities, when I select the state a want to display only cities from given state. Spinners are created dynamicaly.
That's my code:
public class RegistrationActivity extends Activity implements AdapterView.OnItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
createLayout();
}
public void createLayout(){
division_spinner = new Spinner(this);
ArrayAdapter division_adapter = new ArrayAdapter(this,R.layout.spinner,divisions.getList());
divisions_id = viewer.getValueByKey(viewer_form.getViewers_form_viewers_inputname());
division_spinner.setAdapter(division_adapter);
division_spinner.setSelection(divisions.getIndex(divisions_id));
division_spinner.setOnItemSelectedListener(this);
township_spinner = new Spinner(this);
ArrayAdapter township_adapter = new ArrayAdapter(this,R.layout.spinner,townships_map.get(divisions_id));
if (divisions_id == null) {
township_spinner.setEnabled(false);
}
String value = viewer.getValueByKey(viewer_form.getViewers_form_viewers_inputname());
township_spinner.setSelection(townships.getIndex(value, divisions_id));
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(RegistrationActivity.this, "Yes",Toast.LENGTH_LONG);
if(arg0.equals(division_spinner)) {
Toast.makeText(RegistrationActivity.this, "Yes - You got it!",Toast.LENGTH_LONG);
township_spinner.setEnabled(true);
ArrayAdapter township_adapter = new ArrayAdapter(this, R.layout.spinner, townships_map.get(((Division)division_spinner.getSelectedItem()).getDivisions_id().toString()));
township_spinner.setAdapter(township_adapter);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
But "Yes" even "Yes - you got it!" toast does not appear.
I guess I miss something stupid, but I cant find it.
you're implementing AdapterView.onItemSelected.
...Just try this instead
Spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
Toast.makeText(topThis, "selected", Toast.LENGTH_LONG).show();
//Call other spinner here to update.
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "nothing selected", Toast.LENGTH_LONG).show();
}
});
Edit: as Prasad says. If you don't have setContentView(View); your activity won't have an xml to inflate, so your views will never get created into anything.
I made a series of mistakes that led to the fact that it did not work as I expected.
First: I forgot to call **show()** on Toast.makeText()
Second: I make a huge mistake when creating ArrayList. I filled ArrayList with data, then I put the list to HashMap and after that I called ArrayList.**clear()**. Which led to override the HashMap values, and that was why it seemed, the spinner was not changing at all...
Well... I feel quite silly ...
I have problem with spinner control. I am trying to set spinner items dynamically. Initially I have one item in spinner.
When I try to register the spinner.setOnItemSelect Listener, it immediately call onItemSelected method of it. However I don't want to call this method as soon as my activity get started.
So for this I put a following condition.
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
Spinner spinner;
String[] str_arr = {"aaaaaaaa"};
private int mSpinnerCount=0;
private int mSpinnerInitializedCount=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str_arr));
spinner.setOnItemSelectedListener(this);
mSpinnerCount=1;
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (mSpinnerInitializedCount < mSpinnerCount) {
mSpinnerInitializedCount++;
}
else {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
But when I try to select an item on spinner it gives following warning in logcat,
09-03 13:02:02.528: W/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#450fafb8
I get the idea that until and unless Item of spinner won't change this method won't be called.
But I have one value in spinner, so how to get the focus, any idea?
Try this to what i said in comment...
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position > 0) {
//Your actions
}
else {
// Nothing or can show a toast to say user to select a value...
}
}
Try like this
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position != 0) {
//put your actions here
}
else {
// nothing here or toast
}
}
I think the below code are not right because you implements OnItemSelectedListener
spinner.setOnItemSelectedListener(this);
You get this warning when you try to open already opened window, or try to do something like onFocus on already focused view.
Here you already have the item selected in the Spinner
I want to store the dropdown value of the spinner to the database. I am able to get dopdown as per the tutorial in the android developer site but i am not able to store that dropdown value to the database when user click on save button.I don't know it is possible or not if yes please tell me how to do that with some example.
Thanks in advance
This is my code
public class Akshay extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Spinner For Selecting Room
Spinner spinner_room = (Spinner) findViewById(R.id.spinner_for_Room_type_screen_2);
ArrayAdapter adapter_room = ArrayAdapter.createFromResource(this,
R.array.room_array, android.R.layout.simple_spinner_item);
adapter_room.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_room.setAdapter(adapter_room);
spinner_room.setOnItemSelectedListener(new MyOnItemSelectedListener_room());
}
}
// Listener Implementation of Spinner For Selecting Room
public class MyOnItemSelectedListener_room implements OnItemSelectedListener
{
public void onItemSelected(AdapterView parent, View view, int pos, long id)
{
}
public void onNothingSelected(AdapterView parent)
{ // Do nothing.}
};
}
public class Akshay extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
// Spinner For Selecting Room
Spinner spinner_room = (Spinner) findViewById(R.id.spinner_for_Room_type_screen_2);
ArrayAdapter<CharSequence> adapter_room = ArrayAdapter.createFromResource(this,
R.array.room_array,android.R.layout.simple_spinner_item);
adapter_room.setDropDownViewResourc(android.R.layout.simple_spinner_dropdown_item);
spinner_room.setAdapter(adapter_room);
spinner_room.setOnItemSelectedListener(new Listener_Of_Selecting_Room_Spinner());
}
// Listener Implementation of Spinner For Selecting Room
public static class Listener_Of_Selecting_Room_Spinner implements OnItemSelectedListener
{
static String RoomType;
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id)
{
// By using this you can get the position of item which you
// have selected from the dropdown
RoomType = (parent.getItemAtPosition(pos)).toString();
}
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing.
}
};
// Listener Implementation For Saving Number Of Board
private OnClickListener btnListener_Btn_Save_Room_Board = new OnClickListener()
{
public void onClick(View view)
{
DBAdapter dbAdapter1 = new DBAdapter(view.getContext());
String room;
try {
dbAdapter1.createDataBase();
dbAdapter1.openDataBase();
// Here i am using the object RoomType which i have got from
// the Listener of spinner
room = Listener_Of_Selecting_Room_Spinner.RoomType;
ContentValues initialValues1 = new ContentValues();
initialValues1.put("RoomType", room);
//Here i am storing it(RoomType) to the database
dbAdapter1.InsertNumberOfBoardInDB("Configuration", null,initialValues1);
}
catch (Exception e) {
}
finally {
dbAdapter1.close();
}
}
};
}
If you're looking for information on how to use SQLite databases in an Android app, I highly recommend going through the Notepad tutorial.
Or are you stuck at handling the button click?
This doesn't directly answer the question, but design-wise and for simplicity's sake it may make some sense to store UI values as preference.
For example:
public static final String PREFS_NAME = "MyPrefsFile";
public static final String SPINNER_VALUE = "SPINNER VALUE";
SharedPreferences settings = getSharedPreferences(PREFS_NAME , 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(SPINNER_VALUE, <the spinner value>);
editor.commit();
Of course this depends on your requirement, ymmv.
I'm trying to pass a value from an android spinner selection into a url. All of my other vars are passing and the toast on the spinner in displaying the correct spinner choice when selected. (for purposes here, the code doesn't show all vars.)
My log shows the country VAR as NULL. Need to get the "country0" value to pass like the others. How can I make this happen?
thanks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(DEBUG_TAG, "onCreate");
mContext = this; //TODO legacy may not be needed
SERVER = this.getString(R.string.mygallerist_server_base);
settings = getSharedPreferences(PREFS_NAME, 0);
uid = settings.getString("uid", NOT_SET);
//SPINNER
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mOver35CheckBox = (CheckBox) findViewById(R.id.Over35);
class MyOnItemSelectedListener implements OnItemSelectedListener {
//SPINNER PARSING
#Override
public void onItemSelected(AdapterView<?> country0,
View view, int pos, long id) {
Toast.makeText(country0.getContext(), "The country is " +
country0.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView country0 ) {
// Do nothing.
}
}
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
final String country = country0;///MAYBE THIS IS WHAT IS WRONG
setPassword.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// If all fields filled in then go to server
String country1 = country; /// OR MAYBE THIS IS WHAT IS WRONG
String userName = mUserNameEditText.getText().toString();
Perhaps I'm missing something in your question, but when you select the value for the Toast, you could just set the value of a private member, then read this value in your onClick, (being as you know you're getting the correct value in the onItemSelected)
private String countrySelection;
public void onItemSelected(AdapterView<?> country0, View view, int pos, long id) {
countrySelection = country0.getItemAtPosition(pos).toString();
Toast.makeText(country0.getContext(), "The country is " + countrySelection, Toast.LENGTH_LONG).show();
}
...
final String country = countrySelection;
setPassword.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
sendDataToServer(country, ...);
Does this solve your issue?
Edit...
What type is country0 in this context? (I don't have comment privileges yet)
final String country = country0;///MAYBE THIS IS WHAT IS WRONG