6

This is probably a simple straight forward thing to do for some.

I have this code...

SQLiteDatabase db = dbs.getReadableDatabase(); 
String SQL = "SELECT * FROM Table"; 
final Cursor cursor = db.rawQuery(SQL, null);
    array_spinner1[0] = "Select:";
    while (cursor.moveToNext()) {
      array_spinner1[i]= cursor.getString(1) + " - " + cursor.getString(2);
      i ++;
    } 
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                  android.R.layout.simple_spinner_item, array_spinner1);
    s1.setAdapter(adapter);

I populate the array and everything runs fine. However, i want to start the position of the spinner not at the first item(Select). I want it to start where the item in the cursor = 'here' for example. I hope i made sense?

To put it into context in 'Table' column 1 is age range from and column 2 is age range to. so in the spinner i get 0-5, 6-10, 11-20 etc

and what i want to do is start the spinner selected at 11-20 if the user's d.o.b makes him that age....? I know setSelection would select a certain value, but i need to work out the correct one for the users age?

So i basically want to know how to work that out and populate select the spinner correctly, thanks

Beginner
  • 28,539
  • 63
  • 155
  • 235

2 Answers2

8

The Spinner class inherits setSelection(int position, boolean animate) from AbsSpinner. Call that method with the position of your desired default (and false for the animated bit). You only need to iterate through your list of possible values to check for conditions, so maybe I don't understand where your difficulty lies?

Reading through this a second time I get the impression that this is isn't really an Android question. What you're looking for is how to see if a given date of birth places a user in a given age bracket. I've seen age math done in Joda here: How do I calculate someone's age in Java?

From there it's a matter of seeing if the age falls within your given range.

boolean isInRange(Years age, int start, int end) {
  int intAge = age.getYears();
  if (intAge >= start && intAge <= end) {
    return true;
  } else {
    return false;
  }
}

One more addition to my answer here. If you dont' want to deal with running Joda on Android, here's another possibility for calculating age using only the standard Java components. See the third post.

http://www.coderanch.com/t/391834/java/java/there-better-way-calculate-age

Per your request for an example, here is SimpleDateFormat for date strings.

String dateString = "04/02/2004";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date parsedDate = format.parse(dateString, 1);
Community
  • 1
  • 1
Mike Yockey
  • 4,565
  • 22
  • 41
  • hmmm i have the date of birth in a string dd/mm/yyyy, how do i work out the age from this – Beginner Feb 28 '11 at 13:34
  • You can parse date strings into Date() objects using SimpleDateFormat. http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Mike Yockey Feb 28 '11 at 13:41
  • sorry i still dont understand how to just get the age and set the range spinner to it, that parse doesnt return it as mm/dd/yyy either – Beginner Feb 28 '11 at 14:29
  • 1
    No, the parser returns a Date() object which you'll use for the date math demonstrated elsewhere in my answer. – Mike Yockey Feb 28 '11 at 14:30
  • also in android i had to delete the 1 from the .parse function – Beginner Feb 28 '11 at 14:30
  • According to the javadoc for Android, the parse() method requires a position argument. If you're calling parseObject() you don't need a position argument. http://developer.android.com/reference/java/text/SimpleDateFormat.html#parse(java.lang.String, java.text.ParsePosition) – Mike Yockey Feb 28 '11 at 14:37
  • ok i have the age :) now how do i run a function to check what place to set the position of the spinner? – Beginner Feb 28 '11 at 14:50
  • Now that you have the age, modify the method I provided above to work with the age you have. Since you know the position of each range in your spinner list you should be able to correspond a given range to a list position. Give it a try and see what you come up with. You can refactor it after it works if you don't like your implementation. – Mike Yockey Feb 28 '11 at 14:53
0
String DOB = "04/02/2004";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date parsedDate = format.parse(DOB, 0);

Date dateNow = new Date(Date.getYear(),Date.getMonth(),Date.getDay());

Date dateDiff = dateNow - parsedDate;
int Age = dateDiff.getYear();

if(Age>0 && Age<5){
pos = 0;
}//etc....

s1.setSelection(pos, false);

I haven't checked it out but i think its something like that you are after. I might be wrong. Will test it in a min.

AverageMarcus
  • 903
  • 1
  • 9
  • 26