I want to implement one code for three spinner where the user will fill there date of birth at register time when he/she will login and want to update there DOB, the DOB will pass on the spinner I have done it with day and month but there is one problem in year i.e adapter. adapter takes the (thisYear-18) year and print it but I want spinYear will take that value which I stored at registered time.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_details_fragment);
Spinner spinDay = (Spinner) findViewById(R.id.spinnerDay);
Spinner spinMonth = (Spinner) findViewById(R.id.spinnerMonth);
Spinner spinYear = (Spinner) findViewById(R.id.spinnerYear);
Button update = (Button)findViewById(R.id.update);
Intent intent =getIntent();
String str_DOB = intent.getStringExtra("dateofbirth");
Log.e("DOB************",str_DOB);
String[] d = str_DOB.split("-");
Log.e("d[0]-------",d[0].toString());
spinYear.setSelection (Integer.parseInt(d[0]));
spinMonth.setSelection (Integer.parseInt(d[1]));
spinDay.setSelection (Integer.parseInt(d[2]));
code for Year Adapter
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
thisYear=thisYear-18;
for (int i = thisYear; i >= 1967; i--) {
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, years);
spinYear.setAdapter(adapter);
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
final String yearName = adapterView.getItemAtPosition(pos).toString().trim();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
If I understand you correctly you want to set the selection of the year spinner based on your passed in Intent.
setSelection(int position) Takes the index of the item in the spinner you want to select.
After you call setAdapter do as so:
spinYear.setAdapter(adapter);
int index = years.indexOf(d[0]);
if (index != -1) {
spinYear.setSelection(index);
}
you should check the year in onItemSelected() method because this method get the position 0 and set it first.try to use below code:-
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
if((d[2]).equals(adapterView.getItemAtPosition(pos).toString()))
{
final String yearName = adapterView.getItemAtPosition(pos).toString().trim();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Related
Basically i have a method createList() which creates a Spinner and then i add those spinners to an arrayList myList. After adding them i use setOnItemSelectedListener on each Spinner in the array List to get the position but no matter what i select in a spinner i get the position 0. Interestingly, if i don't add spinners to ArrayList and use only one spinner i get the position easily. The problem arises when i put spinner objects in an arrayList.
Here is the code:
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
Spinner spinner1;
int pos;
ArrayList<Spinner> myList = new ArrayList<>();
int length = 0;
int[] numbers;
int show;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.root);
}
public void showPosition(View view) {
numbers = new int[length];
for (int i = 0; i < length; i++) {
myList.get(i).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
numbers[i] = adapterView.getSelectedItemPosition();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
public void createList(View view) {
length++;
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.city, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1 = new Spinner(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(500, 50);
spinner1.setLayoutParams(params);
spinner1.setAdapter(arrayAdapter);
myList.add(spinner1);
linearLayout.addView(spinner1);
}
}
When i print number in the log all the elements of this array are zero no matter what item i have selected in the spinner.
Please help stuck on it for few days :(
Found the answer:
Directly get the position by mySpinner.get(i).getSelectedItemPosition() and use that position with a switch statement to make it workable.
I have 2 spinners in an activity.
Based on the selection of one item in spinner1, relevant data should be loaded in spinner2. Consider spinner1 has data related to country and spinner2 has data related to state.
I should be able to get this done once the activity is created and if the user changes the selection in spinner1.
But I am stuck with populating the spinner2 data based on the saved value of spinner1.
I am calling spinner1.setSelection(indexSaved) but since I am only loading spinner2 in the onItemSelectedOf of spinner1, setSelection of spinner1 is not firing the onItemSelected.
Please let me know how I can achieve this.
I have done same thing in my project without any problem:
Below is code may be it help you:
On Create Method filled state list:
//initialize spinners
spinnerStateList = (Spinner) findViewById(R.id.spinnerStateList);
spinnerDistrictsList = (Spinner) findViewById(R.id.spinnerDistrictsList);
fillStateList();
public void fillStateList()
{
states = manage_states.fetchAllStates(getApplicationContext());
//add new element for select name
states.add(0,new RowItem("-1", "Select State"));
String[] spinnerArray = new String[states.size()];
for(int i= 0;i<states.size();i++)
{
spinnerArray[i] = states.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStateList.setAdapter(dataAdapter);
spinnerStateList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedStateID = states.get(position).getId();
fillDistrictsSelectionChangeState(selectedStateID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void fillDistrictsSelectionChangeState(String selectedStateID)
{
if(selectedStateID.equalsIgnoreCase("-1"))
{
linearLayoutDistrictsList.setVisibility(LinearLayout.GONE);
linearLayoutBlocksList.setVisibility(LinearLayout.GONE);
linearLayoutPHCList.setVisibility(LinearLayout.GONE);
linearLayoutSHCList.setVisibility(LinearLayout.GONE);
}
else
{
linearLayoutDistrictsList.setVisibility(LinearLayout.VISIBLE);
districts = manage_districts.fetchAllDistrictsByStateId(getApplicationContext(), selectedStateID);
//add new element for select name
districts.add(0,new RowItem("-1", "Select District"));
String[] spinnerArray = new String[districts.size()];
for(int i= 0;i<districts.size();i++)
{
spinnerArray[i] = districts.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDistrictsList.setAdapter(dataAdapter);
spinnerDistrictsList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedDistrictID = districts.get(position).getId();
fillBlocksSelectionChangeDistrict(selectedDistrictID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
Hi I am creating a list of spinners dynamically based on a user choice. Here I am also implementing OnItemSelectedListener for each of the spinners, since there are multiple spinners I want to know which spinner's method is currently being accessed. Here's the code,
for (int i = 0; i < count; i++) {
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object item = parent.getItemAtPosition(position);
Log.d("vij-debug", "selector1 no is " + item);
Log.d("vij-debug", "selector1 id is"+ view.getId());
//medicineArray1[i][1]=(String)item;
// here I want to access the iteration value i
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item,
spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
Can anyone suggest a suitable solution?
Set the Spinner's tag to i upon creation, then retrieve it from the AdapterView<?> parent parameter in onItemSelected().
Spinner spinner = new Spinner(this);
spinner.setTag(i);
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
int spinnerNumber = parent.getTag();
}
...
}
);
you could use a List and store your spinner there. in your for loop
//inside for loop
List<Spinner> spinnerList=new ArrayList<Spinner>();
Spinner spinner = new Spinner(this);
spinnerlist.add(spinner);
//end
//outside for loop
for(int i=0;i<spinnerList.size();i++){
setListenerToSpinner(spinnerList.get(i));
}
//end
//function
public void setListenerToSpinner(Spinner spinner){
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object item = parent.getItemAtPosition(position);
Log.d("vij-debug", "selector1 no is " + item);
Log.d("vij-debug", "selector1 id is"+ view.getId());
//medicineArray1[i][1]=(String)item;
// here I want to access the iteration value i
}
}
I have such a method for items adding:
timezoneEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
timezonSpin = (Spinner) findViewById(R.id.timezoneSpin);//timezoneCall();
final ArrayList <String> timezonelist = new ArrayList<String>();
timezonelist.add("Gvatemala");
timezonelist.add("Gonduras");
timezonelist.add("Batumi");
timezonelist.add("Boston");
timezonelist.add("Zhytomir");
timezonelist.add("Zmerinka");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(NewOrderActivity.this,
android.R.layout.simple_spinner_item, timezonelist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timezonSpin.setAdapter(dataAdapter);timezonSpin.performClick();
timezonSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Log.i("sd","Ya tut");
Log.i("choosed",timezonelist.get(position).toString());
timezoneEdit.setText(timezonelist.get(position));
}
public void onNothingSelected(AdapterView<?> arg0) {
TimeZone timezone = TimeZone.getDefault();
String TimeZoneName = timezone.getDisplayName();
int TimeZoneOffset = timezone.getRawOffset()/(60 * 60 * 1000);
Log.i("timezone",Integer.toString(TimeZoneOffset));
timezoneEdit.setText("UTC"+Integer.toString(TimeZoneOffset), TextView.BufferType.EDITABLE);
}
});
When user clicks on button, spinner is showing and user can choose one of the items. But weird thing is that onItemSelectedListener is not calling. I use such shema for my other regular spinners(without button click using) and everything works fine there...What's the missing?
timezoneEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Spinner timezonSpin = (Spinner) findViewById(R.id.timezoneSpin);//timezoneCall();
final ArrayList <String> timezonelist = new ArrayList<String>();
timezonelist.add("Gvatemala");
timezonelist.add("Gonduras");
timezonelist.add("Batumi");
timezonelist.add("Boston");
timezonelist.add("Zhytomir");
timezonelist.add("Zmerinka");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(NewOrderActivity.this,
android.R.layout.simple_spinner_item, timezonelist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timezonSpin.setAdapter(dataAdapter);timezonSpin.performClick();
timezonSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Log.i("sd","Ya tut");
Log.i("choosed",timezonelist.get(position).toString());
timezoneEdit.setText(timezonelist.get(position));
}
public void onNothingSelected(AdapterView<?> arg0) {
TimeZone timezone = TimeZone.getDefault();
String TimeZoneName = timezone.getDisplayName();
int TimeZoneOffset = timezone.getRawOffset()/(60 * 60 * 1000);
Log.i("timezone",Integer.toString(TimeZoneOffset));
timezoneEdit.setText("UTC"+Integer.toString(TimeZoneOffset), TextView.BufferType.EDITABLE);
}
});
try the above code. i think you have to make spinner object inside setOnClickListener event.
Take the timezonSpinner.setOnItemSelectedListener out the block timezoneEdit.setOnClickListener(new OnClickListener() { }
int numberofSpinner = TransportResult.Transfers.size();
Spinner spin=null;
for(int i=0;i<numberofSpinner;i++)
{
spin = new Spinner(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
spinLayout.addView(spin,p);
spin.setId(i);
Transfer transfer = TransportResult.Transfers.get(i);
ArrayList<CharSequence> s = new ArrayList<CharSequence>();
for( Line l : transfer.TransferLine)
{
s.add(l.ShortName+" - "+Helper.FindTransportTypeText(l.LineType));
}
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,s);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==0){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==1){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
If the number of spinner is more than 1, only the last spinner is triggered. For example; i have 3 spinner on screen, when i select the item of first or second spinner, the listener is never triggered. Only the third spinner triggers the listener. How can i solve that?
Thank you
UPDATE
when you use more than one spinner then setid for each spinner spin.setId(int) .and you can check id in OnItemSelected method. Mind that when you set OnitemSelected first time onItemSelected is called.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==1){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==2){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});