简体   繁体   中英

Displaying String on textview is not working

I'm new to android studio, bear with any misunderstandings on my part. I want to display a string of musical lyrics to a text view called lyrics_view which resides in my fragment_lyrics.xml file. I've looked around a lot and followed some similar examples but am not having any luck solving my issue. Here is some of my code. When I hit the play button, a song plays via the Spotify api and I also want to display those lyrics to lyrics_view using the musixMatch api. I can provide more code if needed. Am I even going about displaying this string correctly? Or is there a completely different way I should be doing it? Thank you for making effort with a long post.

playSongButton() which is in my MainActivity class. This is responsible for playing songs and making a call to a calculateLyrics() method.

 public void playSongButton(View view) {
    try {
        mPlayer.playUri(null, "spotify:track:15vzANxN8G9wWfwAJLLMCg", 0, 0);
        //where i believe i am having issues
        TextView textView = null;
        TextView displayTextView = (TextView) findViewById(R.id.lyrics_view);
        String lyricString = songLyrics.calculateLyrics();
        displayTextView.setText(lyricString);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

CalculateLyrics() which is in SongLyrics class

public SongLyrics(String songName, String artistName) {
    this.songName = "Paris";
    this.artistName = "The Chainsmokers";
}

public String calculateLyrics() {
    try {
        musixMatch = new MusixMatch(key.getKey());
        track = musixMatch.getMatchingTrack(songName, artistName);
        trackData = track.getTrack();
        trackId = trackData.getTrackId();
        lyrics = musixMatch.getLyrics(trackId);
         lyricString = lyrics.getLyricsBody();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lyricString;
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:orientation="horizontal"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0">

<EditText
    android:id="@+id/songName"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:ems="10"
    android:hint="@string/enter_song"
    android:inputType="text"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    android:textColor="@color/common_google_signin_btn_text_dark_focused"
    android:layout_marginTop="88dp"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="6dp" />

<EditText
    android:id="@+id/artistName"
    android:layout_width="0dp"
    android:layout_height="55dp"
    android:ems="10"
    android:hint="@string/enter_artist"
    android:inputType="text"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1"
    android:textColor="@color/common_google_signin_btn_text_dark_focused"
    tools:layout_editor_absoluteX="0dp"
    app:layout_constraintTop_toBottomOf="@+id/songName" />

<TextView
    android:text="Enter Song Info"
    android:layout_width="wrap_content"
    android:layout_height="69dp"
    android:id="@+id/enterSongInfo"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    android:textColor="@color/common_google_signin_btn_text_dark_focused"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginEnd="80dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="80dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="16dp"
    app:layout_constraintVertical_bias="0.0"
    app:layout_constraintHorizontal_bias="1.0"
    android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="16dp" />


<Button
    android:text="Play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/playButton"
    android:onClick="playSongButton"
    android:layout_marginStart="296dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="296dp"
    android:layout_marginTop="88dp"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:text="Pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/pauseButton"
    android:onClick="pauseSongButton"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/playButton"
    android:layout_marginStart="296dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="296dp" />

<fragment
    android:id="@+id/lyrics"
    android:layout_width="353dp"
    android:layout_height="329dp"
    android:layout_marginStart="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    android:layout_marginTop="208dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.71"
    class="com.example.joeberg.jams.LyricsFragment"/>


/>

</android.support.constraint.ConstraintLayout>

fragment_lyrics.xml

<android.support.constraint.ConstraintLayout       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=".MainActivity"
>
<TextView
    android:text="Lyrics"
    android:layout_width="93dp"
    android:layout_height="59dp"
    android:id="@+id/lyrics_heading"
    class="com.example.joeberg.jams.LyricsFragment"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="280dp"
    android:textColor="@color/common_google_signin_btn_text_dark_focused" />

<TextView
    class="com.example.joeberg.jams.LyricsFragment"
    android:layout_width="335dp"
    android:layout_height="271dp"
    android:id="@+id/lyrics_view"
    app:layout_constraintTop_toBottomOf="@+id/lyrics_heading"
    android:textAppearance="@style/TextAppearance.AppCompat"
    android:layout_marginStart="32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="32dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    app:layout_constraintHorizontal_bias="0.52" />
</android.support.constraint.ConstraintLayout>

You need to filter the results of your query to musixmatch. When I run a curl command with artist The Chainsmokers and track Paris you recieve 20 tracks returned from musixmatch (mostly club remixes). You have to write some code to find/filter the results to the one you want to display using the filter parameters mentioned in user documentation (f_ input parameters)


curl: curl --url " http://api.musixmatch.com/ws/1.1/track.search?q_artist=The%20Chainsmokers&q_track=Paris&apikey


partial output: {"message":{"header":{"status_code":200,"execute_time":0.019639015197754,"available":20},"body":{"track_list":[{"track":{"track_id":173322884,"track_name":"Paris (Mixed)","track_name_translation_list":[],"track_rating":1,"commontrack_id":96669304,"instrumental":0,"explicit":0,"has_lyrics":0,"has_subtitles":0,"has_richsync":0,"num_favourite":0,"album_id":32901374,"album_name":"Let's Dance, Vol. 2 (DJ Mix)","artist_id":26490468,"artist_name":"The Chainsmokers","track_share_url":"https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed?utm_source=application&utm_campaign=api&utm_medium=","track_edit_url":"https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed/edit?utm_source=application&utm_campaign=api&utm_medium=","restricted":0,"updated_time":"2019-06-12T15:47:28Z","primary_genres":{"music_genre_list":[]}}},{"track":{"track_id":126251071,"track_name":"Paris (Sbm X Geru Remix)","track_name_translation_list":[],"track_rating":5,"commontrack_id":69979021,"ins trumental":0,"explicit":0,"has_lyrics":1,"has_subtitles":0,"has_richs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM