Ad

Android LayoutInflater Not Working In A Separate Class And No Errors Occurr

I'm having a separate class for onClickListener and I'm trying to inflate a view to change text with click here's the activity code

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

Layout

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

Class

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

The toast gets displayed correctly and I get the correct song name but text views and the image are not changing and I'm not getting any errors. I tried to replace the inflater code with this line

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

But still the same result. How can I correctly inflate the layout from a class like this?

Ad

Answer

i think your way is wrong ,you don't need infalter , could define your image and text where

playNext.setOnClickListener(new ExternalOnClickListener(this)); called then in your custom class pass your element to constructor , now you got your views in custom class and dont need define theme in on click just do your logic like

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

UPDATE:

you must pass your element in constructor as new like :

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

}
Ad
source: stackoverflow.com
Ad