I am designing a medication reminder app and I have chosen to use a spinner so that the user can choose to select "Once A Day", "Twice A Day" or "Three Times A Day".
I would then like to use the answer from the spinner to create 1, 2 or 3 timepickers as necessary.
The only problem is that I am getting really stuck on how to do this.
Can somebody please point me in the right direction??
Many Thanks
Sarah
You can create Array in strings.xml
<string-array name="time_arrays">
<item>Once A Day</item>
<item>Twice A Day</item>
<item>Three Times A Day</item>
</string-array>
Spinner arrayadapter Adapter
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.time_arrays));
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourspinner.setAdapter(dataAdapter);
Initialize
private Spinner spnSemester;
ArrayAdapter<String> timeAdapter;
List<String>courseList= new ArrayList<>();
spnTime = (Spinner) findViewById(R.id.spnTime);
courseList.add("Once A Day");
courseList.add("Twice A Day");
courseList.add("Thrice A Day");
timeAdapter= new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,courseList);
spnTime.setAdapter(timeAdapter);
timeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
use on click Listener to get value
spnTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Store selected value here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Initially design your xml layout declaring Spinner and three TimePicker, more detail about Spinner here
Then hide and show timePicker as required.
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
String[] spinner_values = { "Once A Day", "Twice A Day", "Three Times A Day" };
#Override
protected void onCreate(Bundle savedInstanceState) {
//setup timepickers as required
timepicker1=(TimePicker)findViewById(R.id.timePicker1);
timepicker2=(TimePicker)findViewById(R.id.timePicker2);
timepicker3=(TimePicker)findViewById(R.id.timePicker3);
//initially set visibility to GONE for all three timers
//setup spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, spinner_values);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Switch(position){
case 2 :
//show timePicker3
case 1:
//show timepicker2
case 0:
//show timePicker1
break;
default
//hide all timerPickers
break;
}
}
}
Related
I want to show first item as a default text on spinner but in the drop down list that item is not shown like there is arraylist
final ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("number");
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
The number is always shown as a default text on spinner but after clicking on spinner there is one, two, three is shown in drop down list. when we select any number one, two three the default text number is not changed. How can i do it?
ArrayAdapter<String> adapte = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapte.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapte);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View arg1,int pos, long arg3)
{
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this, it show the default as Number..
May be this help you
String[] priceString = { "Number", "one, "two"};
Spinner priceSpinner = (Spinner) findViewById(R.id.price_spinner);
ArrayAdapter priceAdapter = new ArrayAdapter( this.getActivity(), android.R.layout.simple_spinner_item,priceString); priceSpinner.setAdapter(priceAdapter);
I am developing a page with 2 spinners, where the first spinner has some category populated from DB.
Based On the value selected from spinner1, the selectedCategoryName is passed in the method setReportNamesContent().
I am also getting the values populated in the second spinner, but I am unable to do the selection in the 2nd spinner.
My idea is to pass both these selected values on click of a button, but always the 1st value from the 2nd spinner is getting passed on click of the button.
protected void setReportCategory(String loginName) {
map = DBManager.getInstance().reportCategoryListByLoggedInUser(loginName);
reportCategoryList = new ArrayList<String>(map.keySet());
Collections.sort(reportCategoryList);
reportCategorySpinnner = (Spinner) findViewById(R.id.ReportCategoryList);
reportCategorySpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportCategoryList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportCategorySpinnner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.getId()){
case R.id.ReportCategoryList:
selectedCategoryName = parent.getItemAtPosition(position).toString();
break;
case R.id.ReportNameList:
selectedReportName = parent.getItemAtPosition(position).toString();
break;
}
if(!isEmptyOrNull(selectedCategoryName))
setReportNamesContent(selectedCategoryName);
}
private void setReportNamesContent(String selectedCategoryName) {
reportNameList = map.get(selectedCategoryName);
Collections.sort(reportNameList);
reportNameSpinnner = (Spinner) findViewById(R.id.ReportNameList);
reportNameSpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportNameList);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportNameSpinnner.setAdapter(dataAdapter1);
}
I had already reffered various question in this site and tried all of them and are not working. Please advice for a working answer
I recently posted a question on how to change the values of a spinner during program execution and was told to change the array used to make the adaptor and call notifyDataSetChanged();
I tried that but my spinner is not getting updated even though my array is. I attach the code below
public void onCreate(Bundle savedInstanceState)
{
res=getResources();
Boolean a;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// First spinner whose value determines the value of the second spinner
spinner = (Spinner) findViewById(R.id.spinner1);
// ArrayAdaptor of first 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);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setVisibility(4);// spinner 2 is not visible initially
// ArrayAdaptor of first spinner
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_drop);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
Context context = getApplicationContext();
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos!=0)
{
//copying string array of second spinner from strings.xml to the current string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0] , Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.refreshDrawableState();
spinner2.setVisibility(0);//this command works and the spinner is visible, but it is empty
}
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
Any idea what I am doing wrong any ideas will help.
PS: I have tried removing the entire invisible, visible thing, doesnt help
Thanks in advance
Modify your onItemSelected method as follows, note the new addition I added below.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos!=0) {
//copying string array of second spinner from strings.xml to the current
//string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0],
Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.setAdapter(adapter2); // <--- New Addition
// spinner2.refreshDrawableState();
spinner2.setVisibility(0); //this command works and the spinner is visible,
//but it is empty
}
}
Im new to android. i have tried to built an application which has 2 spinner...
i have few doubts to clear before going deep to my application..
please give brief answer to my question...
is it possible to have 2 spinners in one view?
can i add setOnItemSelectedListener(this) for both?
if yes, how could i know which option is selected on both the spinners? like spinner.getSelectedItemPosition(); on both the spinners?
waiting for ur valuable replies. reference materials are also appreciable
Yes, you can have multiple spinner's that can be managed by single listener or you can have seperate Listener for each.
For Single Listener you can do something like this,
Set the Listener
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
Then you can use onItemSelected to get perform the action related to the spinner's.
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long arg3)
{
int id = parent.getId();
switch (id)
{
case R.id.spinner1:
// your stuff here
break;
case R.id.spinner2:
// your stuff here
break;
}
Check this:
Spinner spinner0, spinner1, spinner2;
TextView textView0, textView1, textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView0 = (TextView) findViewById(R.id.text0);
spinner0 = (Spinner) findViewById(R.id.spinner0);
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text0);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(adapter0);
spinner0.setOnItemSelectedListener(onItemSelectedListener0);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setSelection(0,false); //make sure only one listener is
//active in the onCreate method by setting the other listener as false
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
textView2 = (TextView) findViewById(R.id.text2);
spinner2 = (Spinner) findViewById(R.id.spinner2);
MySpinnerAdapter adapter2 = new MySpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_item, obj2);
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(0,false);
spinner2.setOnItemSelectedListener(onItemSelectedListener2);
}
OnItemSelectedListener onItemSelectedListener0 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner0
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener1 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner1
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener2 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner2
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
If the reference of two spinners are different(which will be , if you are using 2 spinners) , give different IDs for them and you can uniquely identify them, Yes you can set click listener to both of them individually.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
yes
yes
spinner1.getSelectedItemPosition(), spinner2.getSelectedItemPosition()
i had a EditText , a button and a spinner . When click the button , the spinner will add a new item with name you entered in the EditText. But here is the question, my adapter.add() method seems doesn't work...here is my code:
public class Spr extends Activity {
Button bt1;
EditText et;
ArrayAdapter<CharSequence> adapter;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button)this.findViewById(R.id.bt1);
et = (EditText)this.findViewById(R.id.et);
spinner = (Spinner)this.findViewById(R.id.spr);
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);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String temp = et.getText().toString();
adapter.add(temp);
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);
}
});
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
#Override
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();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
}
}
thanks! ...still waitting
When you have created your ArrayAdapter you haven't assigned a resizeable List to it, so when you do add() it cannot increment the size of it and throws a UnsupportedOperationException.
Try something like this:
List<CharSequence> planets = new ArrayList<CharSequence>();
adapter = new ArrayAdapter<CharSequence>(context,
R.array.planets_array, planets);
//now you can call adapter.add()
You should use a List. With an Array such as CharSequence[] you would get the same UnsupportedOperationException exception.
Javi is right except don't reference an array for the second parameter.
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item,
someList);
I believe this is working as designed, but not as expected. ArrayAdapter used to only take an array, but the list constructor was added later. I'm guessing its just doing a toArray() on your list. This is why you have to either call add on the adapter, or create a new adapter when your List changes.
you can create an arraylist and copy all recourse to this object then create arrayadaptor and send this arraylist and in onclicklistener of button, add edittext content to arraylist object then call notifydatasetchanged of adator