简体   繁体   中英

Using shared preference in android - kotlin

So I have a List of product not so big, it's about ten product (it's a sample store app). I want to add a Button for user to click and add product to the cart. is it shared preference suitable for storing this amount of data? I also familiar with room database and know that shared preference is for storing very small amount of data. Here Code:

this is my list:

var list = ArrayList<Model>()
        list.add(Model("milk", "12000", R.drawable.milk))
        list.add(Model("yoghurt", "13000", R.drawable.yoghurt))
        list.add(Model("oil", "15000", R.drawable.oil))
        list.add(Model("canned tomato paste", "8000", R.drawable.tomato_paste))
        list.add(Model("hand wash", "18000", R.drawable.hand_wash))
        list.add(Model("ice cream", "5000", R.drawable.ice_cream))
        list.add(Model("spaggeti", "8000", R.drawable.spaggeti))
        list.add(Model("tomato", "7000", R.drawable.tomato))
        list.add(Model("cucumber", "5000", R.drawable.cucumber))
        list.add(Model("potato", "4000", R.drawable.potato))

Each row has just three value: title, price, image. using a add button to send a model of the list to the Cart. somthing like this.

btn.add.setonClicklistener {

// store each row that user clicked in the cart using shared preference
}

is it a good Idea using shared preference for this case?

I think it's perfectly fine. There is a nice library that can simplify this kind of SharedPreferences usage, it is called TinyDB. I use it myself. Go check it out: https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo

You can also implement this yourself.

If user could add food to db I would use Room, if not I would use SharedPreferences to store that.

Adding room to your project:

build.gradle (Project):

ext {
    roomVersion = '2.2.5'
}

build.gradle (Module)

apply plugin: 'kotlin-kapt'

build.gradle (Module)

implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

Food.kt

@Entity(tableName = "food")
data class Food(
    @PrimaryKey @NonNull val id: Int,
    val name: String,
    val price: String,
    @ColumnInfo(name = "drawable_path") val drawablePath: String) {

    override fun toString(): String {
        return "Food $id: name: $name, price: $price, drawable: $drawablePath"
 }

FoodDao.kt

@Dao
interface FoodDao {

    @Query("SELECT * FROM food WHERE id=:id LIMIT 1")
    fun getFood(id: Int): Food

    @Query("SELECT * FROM food")
    fun getAllFood(): List<Food>

    @Insert
    fun addFood(food: Food)
}

FoodRepository.kt

class FoodRepository(private val foodDao: FoodDao) {

    fun getFood(id: Int) = symptomDao.getFood(id)

    fun getAllFood() = symptomDao.getAllFood()
}

FoodDatabase.kt

@Database(entities = [Food::class], version = 1)
abstract class FoodDatabase : RoomDatabase() {

    abstract fun foodDao(): FoodDao

    companion object {

        @Volatile
        private var INSTANCE: FoodDatabase? = null

        fun getDatabase(context: Context): FoodDatabase? {
            if (INSTANCE == null) {
                synchronized(FoodDatabase::class.java) {
                    if (INSTANCE == null) {
                        INSTANCE = Room.databaseBuilder(
                            context.applicationContext,
                            FoodDatabase::class.java, "food_db"
                        ).build()
                    }
                }
            }
            return INSTANCE
        }
    }
}

The code is not tested, you may have to make small adjustments to make it compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM