Android LayoutInflater不在单独的类中工作,不会发生错误

问题描述 投票:0回答:2

我有一个单独的onClickListener类,我试图通过单击此处的活动代码来扩展视图以更改文本

    playNext.setOnClickListener(new ExternalOnClickListener(this));
    playPrevious.setOnClickListener(new ExternalOnClickListener(this));

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NowPlaying"
android:background="#000">

<ImageView
    android:id="@+id/album_cover"
    style="@style/nowPlaying_background"/>

<include layout="@layout/top_buttons"/>

<TextView
    android:id="@+id/album"
    style="@style/nowPlaying_albumName"
    android:text="album name"/>

<TextView
    android:id="@+id/song"
    android:text="song name"
    style="@style/nowPlaying_songName"/>

<LinearLayout
    android:id="@+id/controllers"
    style="@style/controllers_linearLayout">

    <View
        style="@style/nowPlaying_controllers_view_divider"/>

    <Button
        android:id="@+id/skip_previous"
        style="@style/previous_song_btn"
        android:text="" />

    <Button
        android:id="@+id/play_pause"
        style="@style/play_pause"/>


    <Button
        android:id="@+id/skip_next"
        style="@style/previous_song_btn.next_song_btn"
        android:text=""/>

  </LinearLayout>

</RelativeLayout>

public class ExternalOnClickListener implements View.OnClickListener {

private Context context;
private SharedPreferenceConfig preferenceConfig;
private int songIndex = 0;
private int nextSongIndex = 0;
private int previousSongIndex = 0;
private String songName, curSong;

public ExternalOnClickListener(Context context) {
    this.context=context;
    preferenceConfig = new SharedPreferenceConfig(context);

}

@Override public void onClick(View v) {
    switch (v.getId()){

        case R.id.skip_next:
            int songIndex = 0;
            int nextSongIndex = 0;
            String songName = preferenceConfig.readSongName();
            for(Songs s : MainActivity.songs) {
                boolean resultOfComparison= songName.equals(s.getSong());
                if(resultOfComparison){
                    int indexPlusOne = songIndex+1;
                    if((MainActivity.songs.size()) == indexPlusOne){
                        songIndex = 0;
                        nextSongIndex =0;
                    }
                    else{
                        nextSongIndex = songIndex + 1;
                    }

                    String nextSongName = MainActivity.songs.get(nextSongIndex).getSong();
                    String nextAlbumName = MainActivity.songs.get(nextSongIndex).getAlbum();
                    int nextAlbumCover = MainActivity.songs.get(nextSongIndex).getAlbumCover();

                    LayoutInflater inflater = LayoutInflater.from(context);
                    View vi = inflater.inflate( R.layout.activity_now_playing, null );

                    TextView songTextView = (TextView) vi.findViewById(R.id.song);
                    TextView albumTextView = (TextView) vi.findViewById(R.id.album);
                    ImageView albumCover = (ImageView) vi.findViewById(R.id.album_cover);

                    songTextView.setText(nextSongName);
                    albumTextView.setText(nextAlbumName);
                    albumCover.setImageResource(nextAlbumCover);

                    preferenceConfig.writeSongName(nextSongName);
                    Toast.makeText(context, nextSongName, Toast.LENGTH_SHORT).show();
                    break;
                }
                songIndex++;
            }
            break;
    }
  }
}

吐司得到正确显示,我得到正确的歌曲名称,但文本视图和图像没有变化,我没有得到任何错误。我试图用这一行替换inflater代码

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

但结果仍然相同。如何从这样的类中正确地扩充布局?

android onclicklistener layout-inflater
2个回答
1
投票

我认为你的方式是错的,你不需要infalter,可以定义你的图像和文字在哪里

playNext.setOnClickListener(new ExternalOnClickListener(this));在你的自定义类中调用然后将你的元素传递给构造函数,现在你在自定义类中得到了你的视图而不需要在单击中定义主题只是做你的逻辑像

           songTextView.setText(nextSongName);
            albumTextView.setText(nextAlbumName);
            albumCover.setImageResource(nextAlbumCover);

更新:

你必须在构造函数中传递你的元素,如:

private TextView songTextView;
public ExternalOnClickListener(Context context,Textview songTextView) {
    this.songTextView=songTextview;
    this.context=context;
    preferenceConfig = new SharedPreferenceConfig(context);

}

0
投票

也许这就是解决方案。 (因为问题膨胀但未设置为活动的contentView)

  1. 传递为Activity而不是Context。
  2. 使用此而不是膨胀: activity.setContentView(R.layout.activity_now_playing)
  3. vi.findViewById需要改为 activity.findViewById(...)
© www.soinside.com 2019 - 2024. All rights reserved.