Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我在将类型化数组中的可绘制资源ID添加到自定义ArrayAdapter中时遇到问题。它们都以“的形式”出现;0“;_Java_Android_Butterknife - Fatal编程技术网

Java I';我在将类型化数组中的可绘制资源ID添加到自定义ArrayAdapter中时遇到问题。它们都以“的形式”出现;0“;

Java I';我在将类型化数组中的可绘制资源ID添加到自定义ArrayAdapter中时遇到问题。它们都以“的形式”出现;0“;,java,android,butterknife,Java,Android,Butterknife,我对Java编程比较陌生,我正在从事Android基础纳米学位项目(Udacity)的一项作业。我正在构建一个自定义类(Song)并使用ArrayAdapter和RecyclerView填充布局。我已经导入了ButterKnife来绑定数据 数组的数据已硬编码到strings.xml中的类型化数组资源中。歌曲的每个属性都有一个类型化数组-歌曲标题、专辑标题、艺术家姓名、歌曲长度和封面艺术。除了封面艺术是int(可绘制资源ID)之外,所有的都是字符串。当我运行应用程序时,所有数据点都会填充到布局中

我对Java编程比较陌生,我正在从事Android基础纳米学位项目(Udacity)的一项作业。我正在构建一个自定义类(Song)并使用
ArrayAdapter
RecyclerView
填充布局。我已经导入了
ButterKnife
来绑定数据

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

我尝试过多次定义类型化数组,但每次运行应用程序时,数组中的值对于每个封面艺术都是“0”。我已尝试将键入的数组编码为

以下是应用程序运行时的日志输出。日志中没有错误。 请注意调试输出末尾的“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回购协议是


非常感谢您的帮助

我会发表评论,但我只是一个新手,很抱歉提前给出了完整的答案:)。
解释如何从xml数组中获取可绘制ID。

如果代码的答案对您没有帮助,那么还有另一篇关于类似问题的文章(尽管很旧):


还注意到,这两个链接都有
,而不仅仅是

您可以使用
类型Darray
访问
字符串.xml
中的
整数数组中存储的资源值。我已经克隆了您的存储库,并进行了以下调整以解决您的问题。我也做了一个你可以合并它。这是我的修改

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>

我希望有帮助。如果您面临进一步的困难,请告诉我。

很好。它工作得很好。非常感谢你!!我的荣幸。快乐编码!:)谢谢你试着回答我的问题。您链接的资源确实与为我的问题提供的解决方案有相似之处。谢谢,谢谢你试着回答我的问题。您链接的资源确实与为我的问题提供的解决方案有相似之处,我在“谷歌”搜索解决方案时看到了这一点,但它有一些额外的内容,实际上并不适用。我完全忽略了Darray的部分。由于我对Java缺乏经验,我没有注意到眼前的答案。:-)
<array name="songsCoverArt">
   <item>R.drawable.coverart_indigo_big</item>
   ...
</array>
@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());
     }
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
<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>
...

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

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

    ...

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

    ...
}