我在将可绘制资源 ID 从类型化数组添加到自定义 ArrayAdapter 时遇到问题。他们都是“0”

I'm having trouble adding drawable resource ids from a typed array into a custom ArrayAdapter. They all come in as "0"

我是 Java 编程的新手,我正在处理 Android 基础纳米学位课程 (Udacity) 的作业。我正在构建自定义 class(歌曲)并使用 ArrayAdapterRecyclerView 填充布局。我导入了 ButterKnife 用于绑定数据。

数组的数据已被硬编码到 strings.xml 中的类型化数组资源中。歌曲的每个属性都有一个类型化数组——歌曲名称、专辑名称、艺术家姓名、歌曲长度和封面艺术。所有都是字符串,除了封面是一个 int(可绘制资源 ID)。当我 运行 应用程序时,除封面艺术外,所有数据点都填充到布局 (RecyclerView) 中。

我已经尝试了几次定义类型化数组的迭代,但是每次当我 运行 应用程序为每个封面艺术时带入数组的值都是“0”。我试过将类型化数组编码为 <array name="songsCoverArt"> ...</array>,也尝试过 <integer-array name="songsCoverArt">...</integer-array>。每次都得到完全相同的结果。

数组填充在 MainActivity.java 中。

ArrayList<Song> songs = new ArrayList<>();
    String[] songTitle = getResources().getStringArray(R.array.songsTitles);
    String[] albumTitle = getResources().getStringArray(R.array.albumTitle);
    String[] songLength = getResources().getStringArray(R.array.songsLength);
    String[] artistName = getResources().getStringArray(R.array.artistName);
    int[] songCoverArt = getResources().getIntArray(R.array.songsCoverArt);

    for (int i = 0; i < songTitle.length; i++) {
        // debug logging
        Log.d(TAG, "Adding = " + songTitle[i] + " " + albumTitle[i] + " " + songLength[i] + " " + artistName[i] + " " + songCoverArt[i]);
        songs.add(new Song(songTitle[i], albumTitle[i], songLength[i], artistName[i], songCoverArt[i]));
     }
     songAdapter.addItems(songs);
     recyclerView.setAdapter(songAdapter);

strings.xml 中封面艺术的类型化数组,如下所示:

<array name="songsCoverArt">
   <item>@drawable/coverart_indigo_big</item>
   ...
</array>

我也试过像这样定义 Drawable,这给了我完全相同的结果:

<array name="songsCoverArt">
   <item>R.drawable.coverart_indigo_big</item>
   ...
</array>

ImageView 填充在 SongAdapter.java:

@BindView(R.id.songTitle) TextView songTitle;
@BindView(R.id.songLength) TextView songLength;
@BindView(R.id.artistName) TextView artistName;
@BindView(R.id.albumTitle) TextView albumTitle;
@BindView(R.id.coverArt) ImageView coverArt;

public ViewHolder(View itemView) {
   super(itemView);
   ButterKnife.bind(this, itemView);
}

protected void clear() {
   songTitle.setText("");
   albumTitle.setText("");
   songLength.setText("");
   artistName.setText("");
   coverArt.setImageDrawable(null);
}

public void onBind(int position) {
    super.onBind(position);

    final Song song = SongList.get(position);

    if (song.getSongTitle() != null) {
         songTitle.setText(song.getSongTitle());
    }

    if (song.getAlbumTitle() != null) {
         albumTitle.setText(song.getAlbumTitle());
    }
    if (song.getSongLength() != null) {
         songLength.setText(song.getSongLength());
    }

    if (song.getArtistName() != null) {
         artistName.setText(song.getArtistName());
    }

     if (song.getCoverArt() != 0) {
         coverArt.setImageResource(song.getCoverArt());
     }

这是应用程序 运行 时的日志输出。日志中没有错误。 请注意调试输出末尾的“0”。那应该是封面图片的id。

D/SongAdapter: Adding = Be There Jumper 3.4 Halton 0
D/SongAdapter: Adding = Indigo Jumper 4.40 Halton 0
D/SongAdapter: Adding = Kiss Me The Course 4.10 Radson 0
D/SongAdapter: Adding = Halfs Operatic 4.10 Jonesie 0
D/SongAdapter: Adding = Create Poisonous 3.90 Two People 0
D/SongAdapter: Adding = Bigger Proper Attire 4.12 Rotten 0
D/SongAdapter: Adding = Rave On Harlem 3.85 Trapper 0
D/SongAdapter: Adding = Yank Harley 3.90 Korix 0
D/SongAdapter: Adding = Line Up Harlem 3.95 Pepper Sky 0
D/SongAdapter: Adding = Please Cracked 4.05 Yellow 0
D/SongAdapter: Adding = Hurry Cracked 4.20 Yellow 0
D/SongAdapter: Adding = Proud Farsity 4.25 Cannon 0

完整的 github 回购是 here

感谢任何帮助

我本来想发表评论的,但我是新手,提前抱歉为此提供了完整的答案:)。 Storing R.drawable IDs in XML array 解释了如何从 xml 数组中获取可绘制对象 ID。

如果 ProfessionalCode 的回答没有帮助,那么关于类似问题的 SO 上还有另一个(虽然很旧)post:Loading Integer Array from xml

还注意到两个链接都有 <integer-array>,而不仅仅是 <array>

您可以使用 TypedArray 访问存储在 strings.xml 中的 integer-array 中的资源值。我已经克隆了您的存储库并进行了以下调整以解决您的问题。我也做了一个 pull request 你可以合并它。这是我的修改,

strings.xml

<resources>

...

<integer-array name="songsCoverArt">
    <item>@drawable/coverart_indigo_big</item>
    <item>@drawable/coverart_indigo_big</item>
    <item>@drawable/coverart_kissme_big</item>
    <item>@drawable/coverart_halfs_big</item>
    <item>@drawable/coverart_create_big</item>
    <item>@drawable/coverart_bigger_big</item>
    <item>@drawable/coverart_raveon_big</item>
    <item>@drawable/coverart_yank_big</item>
    <item>@drawable/coverart_lineup_big</item>
    <item>@drawable/coverart_hurry_big</item>
    <item>@drawable/coverart_hurry_big</item>
    <item>@drawable/coverart_proud_big</item>
</integer-array>

...

</resources>

MainActivity.java

...

private void prepareDefaultContent() {
        ArrayList<Song> songs = new ArrayList<>();
        //Resources r = recyclerView.getContext().getResources();

        String[] songTitle = getResources().getStringArray(R.array.songsTitles);
        String[] albumTitle = getResources().getStringArray(R.array.albumTitle);
        String[] songLength = getResources().getStringArray(R.array.songsLength);
        String[] artistName = getResources().getStringArray(R.array.artistName);

        TypedArray typedArray = getResources().obtainTypedArray(R.array.songsCoverArt);

        for (int i = 0; i < songTitle.length; i++) {

            int coverArtResourceId = typedArray.getResourceId(i, 0);

            Log.d(TAG, "Adding = " + songTitle[i] + " " + albumTitle[i] + " " + songLength[i] + " " + artistName[i] + " " + coverArtResourceId);

            songs.add(new Song(songTitle[i], albumTitle[i], songLength[i], artistName[i], coverArtResourceId));

        }

        typedArray.recycle(); // Important

        songAdapter.addItems(songs);
        recyclerView.setAdapter(songAdapter);

    }
...

SongDetail.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.song_detail);

    ...

    int BigCoverArt = intent.getIntExtra("coverArt", 0);

    ...
}

希望对您有所帮助。如果您遇到更多困难,请告诉我?