0

So I have a fragment that I need to be able to always update the text.

The text fields are:

  • Song
  • Artist

In my MainActivity I have a http request that keeps ping the server and getting the latest information.

That updates a global Class

package com.drn1.drn1

object DataHolder {
    private var Song = ""
    fun set_Song(s: String) {
        Song = s
    }

    fun get_Song(): String {
        return Song
    }

private var Artist = ""
    fun set_Artist(s: String) {
        Song = s
    }

    fun get_Artist(): String {
        return Artist
    }

    private var MDIA = "http://stream.radiomedia.com.au:8003/stream"
    fun set_Media(s: String) {
        MDIA = s
    }

    fun get_Media(): String {
        return MDIA
    }


}

The issue is I can't re-set the text in the fragment

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        //val context = inflater.context
        //binding = MplayerBinding.inflate(com.drn1.drn1.R.layout.fragment_mplayer)



        val root: View = inflater.inflate(R.layout.fragment_mplayer, container, false)
        val btn = root.findViewById(R.id.mPlayPP) as ImageButton
        val SG = root.findViewById(R.id.song) as TextView

        SG.setText(Song)
}

Wondering what is the correct way to do this?

RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • You should not make an HTTP request every time to keep updated data that's an overkill of resources that's [Polling](https://en.wikipedia.org/wiki/Polling_(computer_science)) . you need some Connection oriented approach to do it may be a Socket. – ADM May 25 '21 at 06:40
  • 1
    @ADM will look into this, but that does not solve the question of how I update the textView in a fragment – RussellHarrower May 25 '21 at 06:47
  • @RussellHarrower Take a look in to using LiveData with the repository pattern, the official google training resources cover it well – Ivan Wooll May 25 '21 at 06:53
  • You have to put this line "SG.setText(Song)" in the response of http request you called and delcare the "SG" textView globally. – Tausif May 25 '21 at 06:56

2 Answers2

1

onCreateView() is only run the first time the Activity is created.

It is not clear on your code where you are updating the DataHolder, however wherever you do it, you need to then call a method in your Activity to update the text view with the latest data.

Ryan
  • 948
  • 6
  • 20
0

As suggested by @Ryan, onCreateView() will get called only once. So You will need a class-level variable for song textview.(In your case make SG class level variable instead of local variable).

Then create a method to update the song text

fun updateSong(song: String) {
    SG.setText(Song)
}

At last, You have to call this method of fragment, Whenever you get the updated information.

In MainActivity, when You get the updated information, You will have an instance of this fragment. So you can call this method on that object

Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43