작성
·
621
0
FirstFragmentadapter.kt
package com.example.yeatlen.Fragment
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.yeatlen.R
class FirstFragAdapter(val context: Context, val list: ArrayList<ContentsListModel>):BaseAdapter(){
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view : View
val holder : ViewHolder
if(convertView==null){
view = LayoutInflater.from(context).inflate(R.layout.listview_item, null)
holder = ViewHolder()
holder.view_image1 = view.findViewById(R.id.lv_image_area)
holder.view_text1 = view.findViewById(R.id.lv_textview_1)
holder.view_text2 = view.findViewById(R.id.lv_textview_2)
holder.view_text3 = view.findViewById(R.id.lv_textview_3)
}else {
holder = convertView.tag as ViewHolder
view = convertView
}
val item = list[position]
holder.view_text1?.text= item.title
return view
}
override fun getItem(p0: Int): Any {
return list.get(p0)
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getCount(): Int {
return list.size
}
private class ViewHolder{
var view_image1 : ImageView? = null
var view_text1 : TextView? = null
var view_text2 : TextView? = null
var view_text3 : TextView? = null
}
}
FirstFragment.kt
package com.example.yeatlen.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.yeatlen.R
import kotlinx.android.synthetic.main.fragment_first.view.*
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
/**
* A simple [Fragment] subclass.
*/
class FirstFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view : View = inflater.inflate(R.layout.fragment_first, container, false)
val list_array = arrayListOf<ContentsListModel>(
ContentsListModel("a","a",1,"c"),
ContentsListModel("a","b",1,"c"),
ContentsListModel("a","ab",1,"c"),
ContentsListModel("a","ab",1,"c"),
ContentsListModel("a","f",1,"c"),
ContentsListModel("a","d",1,"c"),
ContentsListModel("a","c",1,"c")
)
val list_adapter = FirstFragAdapter(requireContext(),list_array)
view.listview_first_fragment.adapter = list_adapter
return view
}
}
ContentsListModel.kt
package com.example.yeatlen.Fragment
data class ContentsListModel(
var image : String,
var title : String,
var number : Int,
var category : String
)
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/lv_image_area"
android:src="@drawable/ai"
android:layout_margin="20dp"
android:layout_height="80dp"
android:layout_width="80dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/lv_textview_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="제목 영역입니다."
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/lv_textview_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="최근 리뷰가 10개 입니다." />
<TextView
android:id="@+id/lv_textview_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="분류입니다." />
</LinearLayout>
</LinearLayout>
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.FirstFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview_first_fragment"/>
</FrameLayout>
입니다. 코드상에 붉은 줄로 오류같은건 안뜨는데 아이콘클릭해서 화면넘어가면 바로 앱이 종료되버립니다.
답변 7
0
안녕하세요 KILLUA님. 아래의 링크 이용해서 listview를 구현해보시는 것도 좋은 방법으로 보입니다.
꼭 정답은 하나가 아니니, 이 방법으로 안되면 다른 방법을 시도해 보시는 것이 더 빠를 수도 있습니다. 감사합니다.
https://blog.yena.io/studynote/2017/12/01/Android-Kotlin-ListView.html
https://www.youtube.com/watch?v=EwwdQt3_fFU
0
0
0
0
2020-07-05
오후 7:34 Android Studio and plugin updates available
Plugins: Firebase Services, Kotlin
Update...
오후 7:34 Android Studio and plugin updates available
Component: Android SDK Platform-Tools
Update...
오후 7:34 * daemon not running; starting now at tcp:5037
오후 7:34 * daemon started successfully
오후 7:34 Gradle sync started
오후 7:34 Gradle sync finished in 3 s 614 ms (from cached state)
오후 7:34 Executing tasks: [:app:assembleDebug] in project C:\Users\wjd32\AndroidStudioProjects\yeatlen
오후 7:34 Emulator: dsound: Could not initialize DirectSoundCapture
오후 7:34 Emulator: dsound: Reason: No sound driver is available for use, or the given GUID is not a valid DirectSound device ID
오후 7:34 Emulator: dsound: Attempt to initialize voice without DirectSoundCapture object
오후 7:34 Emulator: dsound: Attempt to initialize voice without DirectSoundCapture object
오후 7:34 Emulator: audio: Failed to create voice `goldfish_audio_in'
오후 7:34 Emulator: C:\Users\wjd32\AppData\Local\Android\Sdk\emulator\qemu\windows-x86_64\qemu-system-i386.exe: warning: opening audio input failed
오후 7:34 Emulator: dsound: Attempt to initialize voice without DirectSoundCapture object
오후 7:34 Emulator: dsound: Attempt to initialize voice without DirectSoundCapture object
오후 7:34 Emulator: audio: Failed to create voice `adc'
오후 7:34 Emulator: dsound: Attempt to initialize voice without DirectSoundCapture object
오후 7:34 Emulator: dsound: Attempt to initialize voice without DirectSoundCapture object
오후 7:34 Emulator: audio: Failed to create voice `adc'
오후 7:35 Gradle build finished in 23 s 152 ms
오후 7:35 Install successfully finished in 394 ms.: App restart successful without requiring a re-install.
오후 7:35 Emulator: emulator: INFO: QtLogger.cpp:68: Critical: Uncaught TypeError: Cannot read property 'update' of undefined (qrc:/html/js/location-mock-web-channel.js:130, (null))
이벤트로그는 이렇게만 뜨고 아이콘클릭시 그냥 앱이 종료되버립니다.
0
0