0

I'm creating a content provider for an Android app in Java and have a String array of constants and separate names for the index of each string in this array, as follows:

private static final String[] FILM_PROJECTION = {
    FilmContract.FilmEntry.COLUMN_FILM_ID,
    FilmContract.FilmEntry.COLUMN_TITLE,
    FilmContract.FilmEntry.COLUMN_POSTER_URL,
    FilmContract.FilmEntry.COLUMN_OVERVIEW,
    FilmContract.FilmEntry.COLUMN_RATING,
    FilmContract.FilmEntry.COLUMN_DATE,
    FilmContract.FilmEntry.COLUMN_POPULARITY,
    FilmContract.FilmEntry.COLUMN_VIDEOS,
    FilmContract.FilmEntry.COLUMN_REVIEWS
};

private static final int COLUMN_FILM_ID_INDEX    = 0;
private static final int COLUMN_TITLE_INDEX      = 1;
private static final int COLUMN_POSTER_URL_INDEX = 2;
private static final int COLUMN_OVERVIEW_INDEX   = 3;
private static final int COLUMN_RATING_INDEX     = 4;
private static final int COLUMN_DATE_INDEX       = 5;
private static final int COLUMN_POPULARITY_INDEX = 6;
private static final int COLUMN_VIDEOS_INDEX     = 7;
private static final int COLUMN_REVIEWS_INDEX    = 8;

I'm thinking there's surely a better way to do this, and I tried it with enum e.g:

private enum FILM_PROJECTION_INDICES {
    FILM_PROJECTION
}

But Java doesn't unpack the String array so that doesn't work (despite all the values being constant). What I'm asking for is something like this:

index = FILM_PROJECTION.getIndexOf(FilmContract.FilmEntry.COLUMN_TITLE);

Is there any way to do this?

Can_of_awe
  • 1,431
  • 1
  • 11
  • 17
  • Enums work like plain classes, you can add methods and private fields to them. Store the index inside the enum and provide a getter to access it. Or use a Map. – BackSlash Mar 31 '17 at 09:49
  • wouldn't a Map fit your needs? – jhamon Mar 31 '17 at 09:49
  • 1
    Possible duplicate of [Where is Java's Array indexOf?](http://stackoverflow.com/questions/4962361/where-is-javas-array-indexof) – Horia Coman Mar 31 '17 at 09:49

2 Answers2

4

FilmEntry itself should already be an Enum (see tutorial) with a custom toString() method:

enum FilmEntry {
    COLUMN_FILM_ID("Film Id"),
    COLUMN_TITLE("Title"),
    COLUMN_POSTER_URL("Poster URL"),
    COLUMN_OVERVIEW("Overview"),
    COLUMN_RATING("Rating"),
    COLUMN_DATE("Date"),
    COLUMN_POPULARITY("Popularity"),
    COLUMN_VIDEOS("Videos"),
    COLUMN_REVIEWS("Reviews");

    private String title;

    FilmEntry(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return title;
    }
}

System.out.println FilmEntry.COLUMN_FILM_ID.toString(); // returns "Film Id"
System.out.println FilmEntry.COLUMN_FILM_ID.name(); // returns "COLUMN_FILM_ID"
System.out.println FilmEntry.COLUMN_FILM_ID.ordinal(); // returns 0
sschuberth
  • 28,386
  • 6
  • 101
  • 146
0
public enum FilmType {

    FilmContract.FilmEntry.COLUMN_FILM_ID(0),
    ...
   ;

    private final int code;

    FilmType(int code) {
        this.index = index;
    }

    public Integer getIndex() {
        return index;
    }
}

index = FilmType.FilmContract.FilmEntry.COLUMN_FILM_ID.getIndex()