2013-10-24 60 views
0

我是全新的android开发和stackoverflow,所以请在我身上轻松一点。 ;)当使用TextView预期字符串时返回的Int int

我已经创建了一个包含多个TextView的ViewGroup。其中一个特别有一个switch case,其中case常量是在我的java对象中定义的整数。然后,我将case语句设置为我认为是String的值。最后,我使用setText()方法将TextView对象设置为此String。我遇到的问题是,实际返回到视图的是与案例相关的整数,而不是文本值。这使我相信,我传递给TextView的是一个整数,但我不知道如何或在哪里可以纠正这一点。

任何帮助将不胜感激。

的ViewGroup中的代码是在这里:

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; 
    } 

常量在我的Java对象,这里定义的情况下:

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; 

,并设置类型的行动时,被选中的复选框中主要活动在这里:

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); 

部分Strings.xml在这里:

<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> 

感谢您的帮助!

+1

我会检查你的资源文件,看看是否例如R.string.action_label确实是一个字符串,而不是一个数字。 – Marcelo

+1

您可以粘贴上面放置的R.string的strings.xml吗? (R.string.action_label等) –

+0

Marcelo,R.string.action_label其实是一个int(public static final int action_label = 0x ####),但在我的Strings.xml文件中,我有动作。由于R是自动生成的并且action_label已经在我的xml中定义为一个字符串,所以我如何让R识别它呢? – user2916836

回答

0

我没有看到您的代码的任何问题。这里有几件事情要尝试(也许你只需要重新编译和重新运行呢?):

尝试调用的setText与资源ID而不是字符串(即R.string.unknown_label等):https://developer.android.com/reference/android/widget/TextView.html#setText%28int%29

尝试登录genreName的价值,你打电话的setText权利之前:

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

+0

谢谢D.Moore。我现在无法测试这些东西,但会让你知道这些结果如何。再次感谢! – user2916836

+0

我尝试了3个建议,但仍然将流派显示为int而不是String。相反,我将movieInfo对象中的流派大小写常量更改为“字符串”,并修改了将这些常量称为整数的其余代码。然后,我使用if-else if语句而不是switch case,因为我只有7个case。使用if-else if语句而不是开关情况是不好的编码习惯? – user2916836

+0

编码不良?呃..不是真的。使用枚举的效率更高,因为您要比较字节值而不是逐个字符,但这两种方法的性能差别不会太大。我仍然对你的问题感到困惑,现在你的代码不同了,我真的不知道。如果它工作,太棒了!否则,发布您的更新代码,我们可以从那里去。 –

相关问题