강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

김유정님의 프로필 이미지
김유정

작성한 질문수

[왕초보편] 앱 8개를 만들면서 배우는 안드로이드 코틀린(Android Kotlin)

BookMarkActivity에 사진이 안뜹니다! (MainActivity에는 사진 떴음)

작성

·

342

0

1. 망고플레이트 앱 만들기 -> 데이터베이스에 있는 북마크 정보 불러오기 (11분)

2.  내가 작성한 코드

RVAdapter.kt

import android.content.Context
import android.text.Layout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class RVAdapter(val context : Context, val List : MutableList<ContentsModel>) : RecyclerView.Adapter<RVAdapter.ViewHolder>() {
    
//1.
    
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVAdapter.ViewHolder {
        
//rv_item layout inflate
        
val v= LayoutInflater.from(parent.context).inflate(R.layout.rv_item, parent, false)
        
return ViewHolder(v)
    }

    interface ItemClick
    {
        
fun onClick(view : View, position: Int)
    }
    
var itemClick : ItemClick ?= null


    
//2.ViewHolder Hold하고 있는 위젯에 데이터를 설정하는 형태
    
override fun onBindViewHolder(holder: RVAdapter.ViewHolder, position: Int) {
        holder.binditmes(
List[position])

        
if(itemClick != null) {
            holder?.
itemView.setOnClickListener v->
                
itemClick!!.onClick(v,position)
            }
        
}

    }

    
//3.
    
override fun getItemCount(): Int {
        
return List.size
    
}


    
inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {

        
fun binditmes(item : ContentsModel) {

            
val rv_text = itemView.findViewById<TextView>(R.id.rvTextArea)
            
val rv_img = itemView.findViewById<ImageView>(R.id.rvImageArea)

            
//item: List[position]
            
rv_text.text = item.titleText

            
//Glide : 이미지 로딩 라이브러리
            
Glide.with(context)
                .load(item.
ImageUrl)
                .into(rv_img)

        }
    }
}

 

ContentsModel.kt

data class ContentsModel (
   
val url : String = "",
   
val ImageUrl : String = "",
   
val titleText : String = ""
)

 

 

BookmarkActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.ValueEventListener
import com.google.firebase.database.ktx.database
import com.google.firebase.ktx.Firebase

class BookmarkActivity : AppCompatActivity() {
    
private lateinit var auth: FirebaseAuth
    
private val contentsModel mutableListOf<ContentsModel>()

    
override fun onCreate(savedInstanceState: Bundle?) {

        
super.onCreate(savedInstanceState)
        setContentView(R.layout.
activity_bookmark)

        
val database = Firebase.database
        
val myRef = database.getReference("bookrmark_ref")
        
auth = Firebase.auth



        
val recyclerView = findViewById<RecyclerView>(R.id.rv)
        
val rvAdapter = RVAdapter(this,contentsModel)
        recyclerView.
adapter = rvAdapter
        recyclerView.
layoutManager = GridLayoutManager(this2)

        
//데베  읽기
        
myRef.child(auth.currentUser?.uid.toString())
            .addValueEventListener(
object : ValueEventListener {
                
override fun onDataChange(snapshot: DataSnapshot) {

                    
for (dataModel in snapshot.children) {
                        Log.d(
"BOOKmark", dataModel.toString())
                        
contentsModel.add(dataModel.getValue(ContentsModel::class.java)!!)
                    }
                    
//동기화
                    
rvAdapter.notifyDataSetChanged()
                }
                
override fun onCancelled(error: DatabaseError) {
                    Log.e(
"Bookmark","dbError")
                }
            })



    }
}

 

 

rv_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
android:layout_width="match_parent"
    
android:layout_height="220dp"
    
xmlns:app="http://schemas.android.com/apk/res-auto"
    
android:orientation="vertical"
    
android:background="@drawable/radius"
    
android:layout_margin="5dp">

    <
androidx.cardview.widget.CardView
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
app:cardCornerRadius="30dp">

        <
ImageView
            
android:id="@+id/rvImageArea"
            
android:layout_marginTop="10dp"
            
android:layout_marginBottom="10dp"
            
android:scaleType="fitXY"
            
android:src="@drawable/ic_launcher_background"
            
android:layout_width="match_parent"
            
android:layout_height="120dp"/>

    </
androidx.cardview.widget.CardView>

    <
TextView
        
android:id="@+id/rvTextArea"
        
android:textStyle="bold"
        
android:textSize="20sp"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:text="text"
        
android:layout_gravity="center"
        
android:layout_marginTop="30dp"/>

</
LinearLayout>

 

 

Bookmark.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".BookmarkActivity"
    
android:orientation="vertical">

    <
TextView
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:gravity="center"
        
android:text="북마크"
        
android:textSize="20sp"
        
android:textStyle="bold"
        
android:layout_marginTop="10dp"
        
app:layout_constraintTop_toTopOf="parent" />


    <
androidx.recyclerview.widget.RecyclerView
        
android:id="@+id/rv"
        
android:layout_marginTop="50dp"
        
android:layout_width="match_parent"
        
android:layout_height="match_parent" />

</
androidx.constraintlayout.widget.ConstraintLayout>

3. 

북마크 눌렀을 때 나오는 화면

 

메인 화면

답변 6

0

개복치개발자님의 프로필 이미지
개복치개발자
지식공유자

이제 거의 다 원인을 찾은 것 같네요

firebase 데이터베이스에 imgurl이 제대로 저장되어 있는지 확인해주세요

만약 저장되어 있다면, 불러오는 부분에서 오타가 있거나 실수가 있습니다.

저장되어 있지 않다면, 저장하는 부분에서 오타가 있거나 실수가 있습니다.

이 부분 한번 확인해보시고 알려주세요.

0

김유정님의 프로필 이미지
김유정
질문자

W/Glide: Load failed for  with size [514x315]

    class com.bumptech.glide.load.engine.GlideException: Failed to load resource

<mainactivity>

2021-08-29 13:51:01.613 20299-20299/org.techtown.mango_content D/DEBUG: ContentsModel(url=https://www.mangoplate.com/restaurants/LIakXRkYhiQ2, ImageUrl=https://mp-seoul-image-production-s3.mangoplate.com/333500/465017_1596542599031_34420?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80, titleText=King수제만두)

2021-08-29 13:51:01.755 20299-20299/org.techtown.mango_content D/DEBUG: ContentsModel(url=https://www.mangoplate.com/restaurants/mrRRAMenpscd, ImageUrl=https://mp-seoul-image-production-s3.mangoplate.com/365877/1601611_1580881025165_20835?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80, titleText=초원샤브샤브뷔페)

2021-08-29 13:51:01.767 20299-20299/org.techtown.mango_content D/DEBUG: ContentsModel(url=https://www.mangoplate.com/restaurants/RZtDXQuloF38, ImageUrl=https://mp-seoul-image-production-s3.mangoplate.com/382404/172778_1599266560896_70079?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80, titleText=후라토식당)

2021-08-29 13:51:01.775 20299-20299/org.techtown.mango_content D/DEBUG: ContentsModel(url=https://www.mangoplate.com/restaurants/FULimTkj6Xis, ImageUrl=https://mp-seoul-image-production-s3.mangoplate.com/383001/1829944_1608745967201_10564?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80, titleText=피그인더가든)

2021-08-29 13:51:01.791 20299-20299/org.techtown.mango_content D/DEBUG: ContentsModel(url=https://www.mangoplate.com/restaurants/LIakXRkYhiQ2, ImageUrl=https://mp-seoul-image-production-s3.mangoplate.com/333500/465017_1596542599031_34420?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80, titleText=King수제만두)

2021-08-29 13:51:01.802 20299-20299/org.techtown.mango_content D/DEBUG: ContentsModel(url=https://www.mangoplate.com/restaurants/mrRRAMenpscd, ImageUrl=https://mp-seoul-image-production-s3.mangoplate.com/365877/1601611_1580881025165_20835?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80, titleText=초원샤브샤브뷔페)

<bookmarkActivity>

2021-08-29 13:51:14.769 20299-20299/org.techtown.mango_content D/DEBUG: ContentsModel(url=https://www.mangoplate.com/restaurants/LIakXRkYhiQ2, ImageUrl=, titleText=King수제만두)

0

개복치개발자님의 프로필 이미지
개복치개발자
지식공유자

엇.. 그런데 텍스트가 나왔다면 아무것도 안 나올리가 없는데... 

아래와 같이 찍으면 어떻게 나오나요? 추가사항 부분 확인해주세요

fun binditmes(item : ContentsModel) {

            
val rv_text = itemView.findViewById<TextView>(R.id.rvTextArea)
            
val rv_img = itemView.findViewById<ImageView>(R.id.rvImageArea)
            

!!!!!!!!!!!!//추가사항
Log.d("DEBUG", item.toString)

            //item: List[position]
            
rv_text.text = item.titleText

            
//Glide : 이미지 로딩 라이브러리
            
Glide.with(context)
                .load(item.
ImageUrl)
                .into(rv_img)

        }

0

김유정님의 프로필 이미지
김유정
질문자

1. mainActivity 코드

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private val items = mutableListOf<ContentsModel>()

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val bookmarkBtn = findViewById<TextView>(R.id.bookmarkBtn)
bookmarkBtn.setOnClickListener {
val intent= Intent(this, BookmarkActivity::class.java)
startActivity(intent)
}


items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/LIakXRkYhiQ2",
"https://mp-seoul-image-production-s3.mangoplate.com/333500/465017_1596542599031_34420?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"King수제만두"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/mrRRAMenpscd",
"https://mp-seoul-image-production-s3.mangoplate.com/365877/1601611_1580881025165_20835?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"초원샤브샤브뷔페"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/RZtDXQuloF38",
"https://mp-seoul-image-production-s3.mangoplate.com/382404/172778_1599266560896_70079?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"후라토식당"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/FULimTkj6Xis",
"https://mp-seoul-image-production-s3.mangoplate.com/383001/1829944_1608745967201_10564?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"피그인더가든"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/LIakXRkYhiQ2",
"https://mp-seoul-image-production-s3.mangoplate.com/333500/465017_1596542599031_34420?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"King수제만두"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/mrRRAMenpscd",
"https://mp-seoul-image-production-s3.mangoplate.com/365877/1601611_1580881025165_20835?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"초원샤브샤브뷔페"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/RZtDXQuloF38",
"https://mp-seoul-image-production-s3.mangoplate.com/382404/172778_1599266560896_70079?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"후라토식당"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/FULimTkj6Xis",
"https://mp-seoul-image-production-s3.mangoplate.com/383001/1829944_1608745967201_10564?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"피그인더가든"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/LIakXRkYhiQ2",
"https://mp-seoul-image-production-s3.mangoplate.com/333500/465017_1596542599031_34420?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"King수제만두"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/mrRRAMenpscd",
"https://mp-seoul-image-production-s3.mangoplate.com/365877/1601611_1580881025165_20835?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"초원샤브샤브뷔페"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/RZtDXQuloF38",
"https://mp-seoul-image-production-s3.mangoplate.com/382404/172778_1599266560896_70079?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"후라토식당"
)
)

items.add(
ContentsModel(
"https://www.mangoplate.com/restaurants/FULimTkj6Xis",
"https://mp-seoul-image-production-s3.mangoplate.com/383001/1829944_1608745967201_10564?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80",
"피그인더가든"
)
)

//adapter연결
val recyclerView = findViewById<RecyclerView>(R.id.rv)
val rvAdapter = RVAdapter(baseContext, items)
recyclerView.adapter = rvAdapter
recyclerView.layoutManager = GridLayoutManager(this, 2)

rvAdapter.itemClick = object : RVAdapter.ItemClick {
override fun onClick(view: View, position: Int) {

val intent = Intent(baseContext, ViewActivity::class.java)
intent.putExtra("url", items[position].url)
intent.putExtra("title", items[position].titleText)
intent.putExtra("imageUrl", items[position].ImageUrl)

startActivity(intent)
}
}
}
}



2. 메인 액티비티 실행할 때만 로그 정보 뜨고 북마크 액티비티 실행할 땐 로그가 안 뜹니다..!!

<메인 액티비티>
2021-08-28 15:45:29.768 14426-14426/org.techtown.mango_content D/POINT!!: https://mp-seoul-image-production-s3.mangoplate.com/333500/465017_1596542599031_34420?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80 2021-08-28 15:45:29.879 14426-14426/org.techtown.mango_content D/POINT!!: https://mp-seoul-image-production-s3.mangoplate.com/365877/1601611_1580881025165_20835?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80 2021-08-28 15:45:29.885 14426-14426/org.techtown.mango_content D/POINT!!: https://mp-seoul-image-production-s3.mangoplate.com/382404/172778_1599266560896_70079?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80 2021-08-28 15:45:29.888 14426-14426/org.techtown.mango_content D/POINT!!: https://mp-seoul-image-production-s3.mangoplate.com/383001/1829944_1608745967201_10564?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80 2021-08-28 15:45:29.893 14426-14426/org.techtown.mango_content D/POINT!!: https://mp-seoul-image-production-s3.mangoplate.com/333500/465017_1596542599031_34420?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80 2021-08-28 15:45:29.895 14426-14426/org.techtown.mango_content D/POINT!!: https://mp-seoul-image-production-s3.mangoplate.com/365877/1601611_1580881025165_20835?fit=around|512:512&crop=512:512;*,*&output-format=jpg&output-quality=80


0

개복치개발자님의 프로필 이미지
개복치개발자
지식공유자

강의자료에 있는 소스코드와 한번 비교해보셔도 좋습니다.

다른점을 알려주시면 더 원인을 찾기가 쉬워요~

0

개복치개발자님의 프로필 이미지
개복치개발자
지식공유자

안녕하세요? 메인에서 뜬다면 여기에도 당연히 떠야 할 텐데 신기하네용..!

2가지의 정보를 확인해볼까요?

1. MainActivity와 BookmarkActivity에서 rvAdapter를 사용하는 방식이 동일한지?

(지금은 메인액티비티 소스가 없네요)

2. 아래 부분에서 로그를 찍어서 메인액티비티를 실행할 때와 북마크 액티비티를 실행할 때 로그의 차이를 알려주세요!

fun binditmes(item : ContentsModel) {

            
val rv_text = itemView.findViewById<TextView>(R.id.rvTextArea)
            
val rv_img = itemView.findViewById<ImageView>(R.id.rvImageArea)

            
//item: List[position]
            
rv_text.text = item.titleText
            !!로그추가!!
            Log.d("POINT!!", item.ImageUrl.toString())
            
//Glide : 이미지 로딩 라이브러리
            
Glide.with(context)
                .load(item.
ImageUrl)
                .into(rv_img)

        }

김유정님의 프로필 이미지
김유정

작성한 질문수

질문하기