2013-10-24 59 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

編碼不良?呃..不是真的。使用枚舉的效率更高,因爲您要比較字節值而不是逐個字符,但這兩種方法的性能差別不會太大。我仍然對你的問題感到困惑,現在你的代碼不同了,我真的不知道。如果它工作,太棒了!否則,發佈您的更新代碼,我們可以從那裏去。 –

相關問題