debut recycler view /!\ bug n'affiche pas les données

master
Bastien OLLIER 2 years ago
parent 2aa3d6c49c
commit b01e3afc52

@ -37,6 +37,7 @@ android {
}
//./gradlew build --refresh-dependencies
dependencies {
//implementation ('org.maplibre.gl:android-sdk:9.5.2')
@ -47,24 +48,29 @@ dependencies {
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.fragment:fragment-ktx:1.5.6'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.android.material:material:1.8.0'
kapt 'androidx.room:room-compiler:2.5.1'
implementation 'androidx.room:room-ktx:2.5.1'
implementation "androidx.room:room-ktx:2.5.1"
kapt "androidx.room:room-compiler:2.5.1"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.1"
modules {
module("org.jetbrains.kotlin:kotlin-stdlib-jdk7") {
replacedBy("org.jetbrains.kotlin:kotlin-stdlib", "kotlin-stdlib-jdk7 is now part of kotlin-stdlib")
}
module("org.jetbrains.kotlin:kotlin-stdlib-jdk8") {
replacedBy("org.jetbrains.kotlin:kotlin-stdlib", "kotlin-stdlib-jdk8 is now part of kotlin-stdlib")
}
}
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

@ -7,23 +7,25 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:name=".MappingApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Mapping"
tools:targetApi="33">
android:supportsRtl="true">
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true"
android:theme="@style/Theme.AppCompat.Light">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>

@ -0,0 +1,44 @@
package fr.iut.mapping
import android.app.Activity
import android.content.Entity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import fr.iut.mapping.Model.RestaurantData
import fr.iut.mapping.database.DAO.RestaurantDAO
import fr.iut.mapping.database.Entity.RestaurantEntity
class SecondFragment: Fragment(R.layout.fragment_likes_page) {
private val restaurantViewModel: RestaurantViewModel by viewModels {
RestaurantViewModelFactory((requireActivity().application as MappingApplication).repository)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_likes_page, container, false)
val recyclerView = rootView.findViewById<RecyclerView>(R.id.recyclerview)
val adapter = RestaurantAdapter()
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
restaurantViewModel.restaurantLikes.observe(viewLifecycleOwner) { restaurantEntities ->
val restaurantDataList: MutableList<RestaurantData?> =
restaurantEntities.map { entity ->
RestaurantData(entity.lat, entity.lon, entity.name, entity.phone, entity.adress)
}.toMutableList()
Log.d("debug1", restaurantDataList.toString())
adapter.submitList(restaurantDataList)
Log.d("debug2", restaurantDataList.toString())
}
return rootView
}
}

@ -1,5 +0,0 @@
package fr.iut.mapping
import androidx.fragment.app.Fragment
class SecondFragment:Fragment(R.layout.fragment_likes_page) {
}

@ -0,0 +1,16 @@
package fr.iut.mapping
import android.app.Application
import fr.iut.mapping.database.RestaurantDatabase
import fr.iut.mapping.database.RestaurantRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
class MappingApplication: Application() {
val applicationScope = CoroutineScope(SupervisorJob())
val database by lazy { RestaurantDatabase.getDatabase(this,applicationScope) }
val repository by lazy { RestaurantRepository(database.restaurantDAO()) }
}

@ -0,0 +1,70 @@
package fr.iut.mapping
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import fr.iut.mapping.Model.RestaurantData
class RestaurantAdapter : ListAdapter<RestaurantData, RestaurantAdapter.RestaurantViewHolder>(RESTAURANT_COMPARATOR) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RestaurantViewHolder {
Log.d("debug","onCreateViewHolder")
return RestaurantViewHolder.create(parent)
}
override fun onBindViewHolder(holder: RestaurantViewHolder, position: Int) {
Log.d("debug","onBindViewHolder")
val current = getItem(position)
holder.bind(current.name)
}
override fun getItemCount(): Int {
Log.d("debug","getItemCount")
Log.d("debug",currentList.toString())
return currentList.size
}
class RestaurantViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val restaurantItemView: TextView = itemView.findViewById(R.id.nameRestaurant)
fun bind(text: String?) {
Log.d("debug","bind")
restaurantItemView.text = text
}
companion object {
fun create(parent: ViewGroup): RestaurantViewHolder {
Log.d("debug","create")
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.recyclerviewitem, parent, false)
return RestaurantViewHolder(view)
}
}
}
companion object {
private val RESTAURANT_COMPARATOR = object : DiffUtil.ItemCallback<RestaurantData>() {
override fun areItemsTheSame(oldItem: RestaurantData, newItem: RestaurantData): Boolean {
Log.d("debug","areItemsTheSame")
return oldItem === newItem
}
override fun areContentsTheSame(oldItem: RestaurantData, newItem: RestaurantData): Boolean {
Log.d("debug","areContentsTheSame")
return oldItem.name == newItem.name &&
oldItem.lat == newItem.lat &&
oldItem.lon == newItem.lon &&
oldItem.adress == newItem.adress &&
oldItem.phone == newItem.phone
}
}
}
}

@ -16,4 +16,7 @@ interface RestaurantDAO {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(Restaurant: RestaurantEntity)
@Query("DELETE FROM restaurant_table")
suspend fun deleteAll()
}

@ -4,8 +4,11 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import fr.iut.mapping.database.DAO.RestaurantDAO
import fr.iut.mapping.database.Entity.RestaurantEntity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
@Database(entities = arrayOf(RestaurantEntity::class), version = 1, exportSchema = false)
abstract class RestaurantDatabase : RoomDatabase() {
@ -16,16 +19,41 @@ abstract class RestaurantDatabase : RoomDatabase() {
@Volatile
private var INSTANCE: RestaurantDatabase? = null
fun getDatabase(context: Context): RestaurantDatabase {
fun getDatabase(context: Context, scope: CoroutineScope): RestaurantDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext, RestaurantDatabase::class.java, "restaurant_database"
).build()
).addCallback(RestaurantDatabaseCallback(scope)).build()
INSTANCE = instance
instance
}
}
}
private class RestaurantDatabaseCallback(private val scope: CoroutineScope) : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
INSTANCE?.let { database ->
scope.launch {
populateDatabase(database.restaurantDAO())
}
}
}
suspend fun populateDatabase(restaurantDao: RestaurantDAO) {
// Delete all content here.
restaurantDao.deleteAll()
// Add sample words.
var resto = RestaurantEntity(0,0.0,0.0,"SUPER RESTO","0000","11 rue")
restaurantDao.insert(resto)
resto = RestaurantEntity(0,1.0,1.0,"SUPER RESTO2","00002","22 rue")
restaurantDao.insert(resto)
}
}
}

@ -20,6 +20,7 @@
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="@android:color/holo_blue_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"

@ -2,11 +2,23 @@
<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">
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/likesPage" />
android:layout_height="wrap_content"
android:text="test"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="0dp"
android:layout_height="0dp"
tools:listitem="@layout/recyclerviewitem"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/nameRestaurant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light" />
</LinearLayout>
Loading…
Cancel
Save