Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- ๊ฐ๋ฐ
- JavaScript
- ์ค๋ผํด
- ์ค๋ธ์
- database
- ์ํ
- ๋ฆฌ์กํธ
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์ฝ๋ฉ
- ์ปดํจํฐ๊ณผํ
- css
- K๋ฐฐํฐ๋ฆฌ๋ ๋ณผ๋ฃจ์
- ํ์ด์ฌ
- ๊น๋ฏธ๊ฒฝ์๋งํ์์
- ์นํ์ด์ง๋ง๋ค๊ธฐ
- Java
- ์๋ฐ
- html
- ๋ง์ผ๋ด๊ฐ์ธ์์๋ค์์ฐ๋ค๋ฉด
- ๋ ์
- ์นํผ๋ธ๋ฆฌ์ฑ
- ํ๋ก๊ทธ๋๋ฐ
- ๋ผํ๋ผ์ค์๋ง๋
- ComputerScience
- K๋ฐฐํฐ๋ฆฌ
- Python
- ์ฑ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ๋ฐ์ํ
Archives
- Today
- Total
JiYoung Dev ๐ฅ
[Android] ํฌ์ผ๋ชฌ ๋๊ฐ ๋ง๋ค๊ธฐ (Volley, RecyclerView) (2023.07.24~25) ๋ณธ๋ฌธ
full stack/์๋๋ก์ด๋
[Android] ํฌ์ผ๋ชฌ ๋๊ฐ ๋ง๋ค๊ธฐ (Volley, RecyclerView) (2023.07.24~25)
Shinjio 2023. 7. 25. 09:36
MainActivity
package com.sjy.pokemon
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONObject
import java.util.ArrayList
//ViewHolder : ํ๊ฐ์ ์์ดํ
๋ทฐ๋ฅผ ๊ตฌ์ฑ
//Adapter : ViewHolder ์์ฑ -> ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ
//MainActivity : Adapter ์์ฑ, layout ๊ฒฐ์
class MainActivity : AppCompatActivity() {
lateinit var rcPokemon : RecyclerView
lateinit var reqQueue : RequestQueue
var pokemonList = ArrayList<PokemonVO>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rcPokemon = findViewById(R.id.rcPokemon)
reqQueue = Volley.newRequestQueue(this@MainActivity)
//1t์ธ๋ -> 151
for(id in 1 until 152){
val imgUrl = "https:/raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/$id.png"
val dataUrl = "https://pokeapi.co/api/v2/pokemon-species/$id"
val request = object : StringRequest(
Request.Method.GET,
dataUrl,
{
response ->
val result = JSONObject(response)
val name = result.getJSONArray("names").getJSONObject(2).getString("name");
val pokemon = PokemonVO(id,imgUrl,name)
pokemonList.add(pokemon)
val adapter:PokemonAdapter = PokemonAdapter(pokemonList, applicationContext) //์ด๋ํฐ ์์ฑ
rcPokemon.layoutManager = GridLayoutManager(applicationContext, 2)
rcPokemon.adapter = adapter
},{
error ->
Log.d("error", error.toString())
Toast.makeText(this, "error๋ฐ์", Toast.LENGTH_SHORT).show()
}
){} //object ์ฌ์ฉ์ body๊ฐ ๊ผญ ์์ด์ผ ํจ -> {}
reqQueue.add(request)
}
}
}
VO
package com.sjy.pokemon
data class PokemonVO(val pokemonId : Int, val pokemonImgPath : String, val pokemonNm : String)
ViewHolder
package com.sjy.pokemon
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class PokemonViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView){
lateinit var ivPokemon:ImageView
lateinit var tvPokemon:TextView
init { //component ์ด๊ธฐํ
ivPokemon = itemView.findViewById(R.id.ivPokemon)
tvPokemon = itemView.findViewById(R.id.tvPokemon)
}
}
Adapter
package com.sjy.pokemon
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
class PokemonAdapter(var datas:ArrayList<PokemonVO>, var context:Context) : RecyclerView.Adapter<PokemonViewHolder>(){
//ViewHolder ์์ฑ
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PokemonViewHolder {
return PokemonViewHolder(
//View์์ฑ
//attachToRoot : Root(๋ถ๋ชจ)์๊ฒ ๋ถ์ผ๊ฑด๋ฐ, ์์ฑ ํ ๋ฐ๋ก ๋ถ์ด๋ ค๋ฉด --> true, ์ด๋ฐ๊ฐ ๋ถ์ด๋ ค๋ฉด --> false
LayoutInflater.from(parent.context).inflate(R.layout.pokemon_item_view, parent, false)
)
}
//์์ดํ
(ํฌ์ผ๋ชฌ) → ViewHolder ๋ฐ์ธ๋ฉ
//๊ฐ์ง๊ณ ์ค๋ ์์ดํ
๊ฐ์
override fun getItemCount(): Int {
return datas.size
}
//์์ดํ
๋ค์ ViewHolder์ ๋ฐ์ธ๋ฉํด์ฃผ๋ ๋ฉ์๋
//์์ดํ
-> ํฌ์ผ๋ชฌ : pokemonID, pokemonImgPath, pokemon
override fun onBindViewHolder(holder: PokemonViewHolder, position: Int) {
val pokemon = datas[position] //ํน์ ์ธ๋ฑ์ค์ ์๋ ํฌ์ผ๋ชฌ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
//ํฌ์ผ๋ชฌ ์ด๋ฏธ์ง : Glide ๋ผ์ด๋ธ๋ฒ๋ฆฌ -> HTTP ํต์ ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ๋ก๋ํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
Glide.with(context).load(pokemon.pokemonImgPath).into(holder.ivPokemon)
//ํฌ์ผ๋ชฌ ์ด๋ฆ
holder.tvPokemon.text = pokemon.pokemonNm
holder.itemView.setOnClickListener(){
//intent ์ฌ์ฉ -> DetailActivity๋ก ์ ํ
val intent = Intent(context, DetailActivity::class.java)
//์๋๋ก์ด๋ ์ฑ์์ ์๋ก์ด ์กํฐ๋นํฐ๊ฐ ์คํ๋ ๋๋ง๋ค
//๊ธฐ์กด์ ์ฌ์ฉํ๋ ์กํฐ๋นํฐ๋ ์คํ์ ์์ด๊ฒ ๋จ
//→ ์ํ์ง ์๋ ๊ฒฐ๊ณผ๊ฐ ๋์ถ๋ ์ ์์ด FLAG๋ฅผ ํ์ฉํ์ฌ ์กฐ์ ๊ฐ๋ฅ
//FLAG_ACTIVITY_NEW_TASK : ์๋ก์ด ์์
์์
//-> ์ผ๋ฐ์ ์ผ๋ก Adapter ๋ด์์ startActivity ์ฌ์ฉ ์ ์ฌ์ฉ
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//pokemonID -> Int
//pokemonImgPath -> String
//pokemonNm -> String
intent.putExtra("id", pokemon.pokemonId.toInt())
intent.putExtra("imgUrl", pokemon.pokemonImgPath.toString())
intent.putExtra("name", pokemon.pokemonNm.toString())
context.startActivity(intent)
}
}
}
DetailActivity
package com.sjy.pokemon
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TableRow
import android.widget.TextView
import androidx.core.view.isVisible
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide
import org.json.JSONObject
import org.w3c.dom.Text
class DetailActivity : AppCompatActivity() {
lateinit var ivPokemon : ImageView
lateinit var tvName : TextView
lateinit var tvType1 : TextView
lateinit var tvType2 : TextView
lateinit var tvHeight : TextView
lateinit var tvWeight : TextView
lateinit var tblRow : TableRow
lateinit var reqQueue : RequestQueue
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
//component ๊ฐ์ ธ์ค๊ธฐ -> ์ด๋ฏธ์ง, ํ
์คํธ
ivPokemon = findViewById(R.id.ivDetailPokemon)
tvName = findViewById(R.id.tvPokeName)
tvType1 = findViewById(R.id.tvType1)
tvType2 = findViewById(R.id.tvType2)
tvHeight = findViewById(R.id.tvHeight)
tvWeight = findViewById(R.id.tvWeight)
tblRow = findViewById(R.id.tblRow)
reqQueue = Volley.newRequestQueue(this)
//intent -> ์ด๋ฆ, ์ด๋ฏธ์ง๊ฒฝ๋ก(Glide), id(์์ธ ์ ๋ณด ๊ฐ์ ธ์ฌ ๋ ์ฌ์ฉ)
//์์ธ์ ๋ณด url : https://pokeapi.co/api/v2/pokemon/$id
val intent : Intent = intent
var id : Int = intent.getIntExtra("id", -1)
var imgUrl : String? = intent.getStringExtra("imgUrl")
var name : String? = intent.getStringExtra("name")
//response -> JSONObject ํํ๋ก ๋ณํ -> result
//2๋ฒ์งธ ํ์
์๋ ๊ฒฝ์ฐ๊ฐ ์์ ! >> ์ฃผ์
//type = result.getJSONArray("types")
//type.getJSONObject(index).getJSONObject("type").getString("name")
// index -> 0,1 (1๋ฒ์งธ ์์ ์ ์์) ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊ฑด์ง ์๊ฐ
//result.getDouble("weight") ๋ฌด๊ฒ
//result.getDouble("height") ํฌ๊ธฐ
Glide.with(this).load(imgUrl).into(ivPokemon)
tvName.text = name
val request = object : StringRequest(
Request.Method.GET,
"https://pokeapi.co/api/v2/pokemon/$id",
{
response ->
val result = JSONObject(response)
val type = result.getJSONArray("types")
var type1 : String = ""
var type2 : String = ""
type1 = type.getJSONObject(0).getJSONObject("type").getString("name")
if(type.length()==1){
tblRow.visibility = View.GONE
}else{
type2 = type.getJSONObject(1).getJSONObject("type").getString("name")
}
val weight = result.getDouble("weight")
val height = result.getDouble("height")
tvType1.text = type1
tvType2.text = type2
tvWeight.text = weight.toString() + "kg"
tvHeight.text = height.toString() + "m"
val typeList = HashMap<String, Int>()
typeList.put("electric", Color.rgb(255,255,102))
typeList.put("water", Color.rgb(153,255,255))
typeList.put("fire", Color.rgb(204,0,0))
typeList.put("grass", Color.rgb(51,255,102))
typeList.put("poison", Color.rgb(153,0,204))
typeList.put("bug", Color.rgb(51,102,102))
typeList.put("ice", Color.rgb(102,204,255))
typeList.put("ground", Color.rgb(153,102,0))
typeList.put("rock", Color.rgb(51,51,51))
typeList.put("normal", Color.rgb(0,0,0))
typeList.get(type1)?.let { ivPokemon.setBackgroundColor(it) }
},
{
error ->
Log.d("error", error.toString())
}
){}
reqQueue.add(request)
}
}
'full stack > ์๋๋ก์ด๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] android studio ํฐํธ ์ ์ฉ (2023.07.26) (0) | 2023.07.26 |
---|---|
[Android] ํ๋๊ทธ๋จผํธ(Fragment) (2023.07.25) (0) | 2023.07.25 |
[Android] RecyclerView (2023.07.20, 24) (0) | 2023.07.20 |
[Android] visibility & enabled ์์ฑ (2023.07.19) (0) | 2023.07.19 |
[Android] JSON, GSON (2023.07.19) (0) | 2023.07.19 |