Tag: array

  • Tips discovered while using Android spinners : setting selected item and finding objects in arrays in Java

    To set a selected item programmatically in Android Spinner, you should use setSelection method.

    To get an index of an item to select from an array of strings using IndexOf, you should first use java.util.Arrays.asList method.

    Complete code:

    int genderIndex = java.util.Arrays.asList(R.array.genders).indexOf(form.gender);
    genderSpinner.setSelection(genderIndex);
    
  • Sorting objects in Objective-C in Alphabet Order by Name

    It is a very easy but a common task. There is no need to create any custom NSSortDescriptors. Let’s say, we have an unsorted array of RRRegion objects, that have a name property. This is how we can sort this array:

      NSArray *regionArray = [[RRSession sharedSession] regions];
      // Sorting in alphabet order by name
      NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
      regionArray = [regionArray sortedArrayUsingDescriptors:@[sortDescriptor]];