//MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var model: SongViewModel
private var songs: Array<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
model = ViewModelProvider(this)[SongViewModel::class.java] // this: context
/**
* [songViewModel] 해시맵으로 생각함
* get...Model 대신 사용
*/
model.requestSong()
model.songs.observe(this, Observer<ArrayList<String>> {
// Toast.makeText(this, model.songs.value.toString(), Toast.LENGTH_LONG).show()
songs = model.songs.value?.toTypedArray()
/**
* 단지 asynchronous(비동기성의)하게 호출하기 때문에 (observer 사용하지 않은 경우)
* request가 콜백함수를 통해 값을 넣고 다시 반환해줌
* 초기화 값이 빈배열이기 때문에 이 부분(songs = ...)을 그냥 지나치게 되면서 그대로 출력됨
*/
binding.listSongs.adapter = ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, songs as Array<out String>)
})
}
}
//SongViewModel.kt
class SongViewModel(application: Application) : AndroidViewModel(application) {
companion object {
/**
* Request Tag 생성
*/
const val QUEUE_TAG = "SongVolleyRequest"
}
private val list = ArrayList<String>()
val songs = MutableLiveData<ArrayList<String>>()
private var queue: RequestQueue
init {
songs.value = list
queue = Volley.newRequestQueue(getApplication())
}
fun requestSong() {
val url = "https://expreesongdb-hoaos.run.goorm.io"
val request = JsonArrayRequest(
Request.Method.GET,
url,
null,
{
list.clear() // 리스트 비워주기
parseJson(it)
songs.value = list //songs의 값에 넣어줘야 observer가 동작함
Toast.makeText(getApplication(), list.toString(), Toast.LENGTH_LONG).show()
},
{
Toast.makeText(getApplication(), it.toString(), Toast.LENGTH_LONG).show()
}
)
request.tag = QUEUE_TAG
queue.add(request)
}
//volley -> pascal case
private fun parseJson(items: JSONArray) {
for (i in 0 until items.length()) {
val item: JSONObject = items[i] as JSONObject
val id = item.getInt("id")
val title = item.getString("title")
val singer = item.getString("singer")
list.add("$title ($singer)")
}
}
override fun onCleared() {
super.onCleared()
queue.cancelAll(QUEUE_TAG)
}
}
val songs = MutableLiveData<ArrayList<String>>()