【问题标题】:Int returned when String expected using TextView使用 TextView 预期 String 时返回的 Int
【发布时间】:2013-10-24 17:29:50
【问题描述】:

我是 android 开发和 stackoverflow 的新手,所以请放轻松。 ;)

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

任何帮助将不胜感激。

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

case 常量在我的 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> 

感谢您的帮助!

【问题讨论】:

  • 我会检查您的资源文件,例如 R.string.action_label 是否真的是字符串而不是数字。
  • 你能粘贴你放在上面的 R.string 的 strings.xml 吗? (R.string.action_label等)
  • Marcelo, R.string.action_label 实际上是一个 int (public static final int action_label=0x####) 但在我的 Strings.xml 文件中我有 动作 。由于 R 是自动生成的,并且 action_label 已在我的 xml 中定义为字符串,我如何让 R 将其识别为这样?
  • D.Moore,刚刚添加。谢谢!
  • 我认为您会感到困惑,因为您正在 R.java 文件中查找action_label 的定义。 R.java 文件由系统生成,您不必担心。实际上,它只是将您的action_lebel 的引用地址存储为 int。 R.string.action_label 实际上是字符串而不是 int。

标签: java android textview listitem viewgroup


【解决方案1】:

我没有发现您的代码有任何问题。这里有几件事可以尝试(也许您只需要重新编译并重新运行它?):

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

在调用 setText 之前尝试记录genreName 的值:

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

【讨论】:

  • 感谢 D.Moore。我现在无法测试这些东西,但会让你知道结果如何。再次感谢!
  • 我尝试了 3 条建议,但仍然将流派显示为 int 而不是 String。相反,我将我的 movieInfo 对象中的流派大小写常量更改为字符串,并修改了将这些称为整数的其余代码。然后我使用 if-else if 语句而不是 switch case,因为我只有 7 个 case。使用 if-else if 语句而不是 switch case 是不好的编码习惯吗?
  • 编码错误?呃。。不是。使用枚举更有效,因为您正在比较字节值而不是逐个字符,但无论哪种方式都不会有太大的性能差异。我仍然对您的问题感到困惑,现在您的代码不同了,我真的不知道。如果它有效,那就太好了!否则,请发布您更新的代码,我们可以从那里开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2011-12-03
  • 2019-10-12
  • 2016-11-27
  • 1970-01-01
  • 2013-10-16
  • 2020-07-08
相关资源
最近更新 更多