Question

I am brand new to android development and stackoverflow so please go easy on me. ;)

I have created a ViewGroup with a number of TextViews. One in particular has a switch case where the case constants are integers defined in my java object. I then set the case statements to what I believe to be a String value. Finally I use the setText() method to set the TextView object to this String. The issue I am having is that what is actually being returned to the view is the integer associated with the case and not the text value. This leads me to believe that what I am passing to the TextView is an int but I do not see how or where I can correct this.

Any help would be greatly appreciated.

The ViewGroup code is here:

public View getView (int position, View convertView, ViewGroup parent)
    {
        ViewGroup listItem;
        if (convertView == null) {
            listItem = (ViewGroup) getLayoutInflator().inflate(R.layout.list_item, null);
        }
        else {
            listItem = (ViewGroup) convertView;
        }
        MovieInfo movieInfo = list.get(position);
        TextView movietitleText = (TextView) listItem.findViewById(R.id.movietitle_text);
        TextView directorText = (TextView) listItem.findViewById(R.id.director_text);
        TextView producerText = (TextView) listItem.findViewById(R.id.producer_text);
        TextView releaseyearText = (TextView) listItem.findViewById(R.id.releaseyear_text);
        TextView genreText = (TextView) listItem.findViewById(R.id.genre_text);

        movietitleText.setText(movieInfo.getMovietitle());
        producerText.setText(movieInfo.getProducer());
        directorText.setText(movieInfo.getDirector());
        releaseyearText.setText(movieInfo.getReleaseyear());

        String genreName;
        Resources resources = context.getResources();
        switch (movieInfo.getGenre()) {
        case MovieInfo.GENRE_ACTION:
            genreName = resources.getString(R.string.action_label);
            break;
        case MovieInfo.GENRE_COMEDY:
            genreName = resources.getString(R.string.comedy_label);
            break;
        case MovieInfo.GENRE_DRAMA:
            genreName = resources.getString(R.string.drama_label);
            break;
        case MovieInfo.GENRE_FAMILY:
            genreName = resources.getString(R.string.family_label);
            break;
        case MovieInfo.GENRE_HORROR:
            genreName = resources.getString(R.string.horror_label);
            break;
        case MovieInfo.GENRE_ROMANCE:
            genreName = resources.getString(R.string.romance_label);
            break;
        default:
        case MovieInfo.GENRE_UNKNOWN:
            genreName = resources.getString(R.string.unknown_label);
            break;
        }
        genreText.setText(genreName);
        return listItem;
    }

The case constants are defined in my java object here:

public static final int GENRE_UNKNOWN  = 0;
public static final int GENRE_ACTION   = 1;
public static final int GENRE_COMEDY   = 2;
public static final int GENRE_DRAMA    = 3;
public static final int GENRE_FAMILY   = 4;
public static final int GENRE_HORROR   = 5;
public static final int GENRE_ROMANCE  = 6;

private String movietitle;
private String director;
private String producer;
private String releaseyear;
private int genre;

And the action to set the Genre when the check box is checked is in the main activity here:

if (actionCheckBox.isChecked()) movieInfo.setGenre(MovieInfo.GENRE_ACTION);
if (comedyCheckBox.isChecked()) movieInfo.setGenre(MovieInfo.GENRE_COMEDY);
if (dramaCheckBox.isChecked()) movieInfo.setGenre(MovieInfo.GENRE_DRAMA);
if (familyCheckBox.isChecked()) movieInfo.setGenre(MovieInfo.GENRE_FAMILY);
if (horrorCheckBox.isChecked()) movieInfo.setGenre(MovieInfo.GENRE_HORROR);
if (romanceCheckBox.isChecked()) movieInfo.setGenre(MovieInfo.GENRE_ROMANCE);

Portion of Strings.xml here:

<string name="movietitle_label">Movie Title</string> 
<string name="director_label">Director</string> 
<string name="producer_label">Producer</string> 
<string name="releaseyear_label">Release Year</string> 
<string name="genre_label">Genre</string> 
<string name="action_label">Action</string> 
<string name="comedy_label">Comedy</string> 
<string name="drama_label">Drama</string> 
<string name="family_label">Family</string> 
<string name="horror_label">Horror</string> 
<string name="romance_label">Romance</string> 
<string name="unknown_label">Unknown</string> 
<string name="ok_label">OK</string> 
<string name="clear_label">Clear</string> 
<string name="showdatabase_label">Show Database</string> 
<string name="id_label">ID</string> 
<string name="select">Select Year</string> 

Thanks for your help!

Was it helpful?

Solution

I don't see any issues with your code. Here's a couple things to try (maybe you just need to recompile and rerun it?):

Try calling setText with the resource id instead of a string (i.e. R.string.unknown_label, etc): https://developer.android.com/reference/android/widget/TextView.html#setText%28int%29

Try logging the value of genreName right before you call setText:

Log.d("MovieGenre", "genreName=" + genreName);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top