Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Why can’t the database still not save the data with my current TypeConverter?

Posted on November 14, 2022

Solution 1 :

Your DB contains Lists of Genre, Platform, ReleaseDate and Video. SQLite supports column types of INTEGER, REAL, TEXT and BLOB. You must provide methods for conversion of your List types to/from String(TEXT) or one of the other supported SQLite types.

For example:

@TypeConverter
public static List<Genre> toGenreList(String value) {
    // TODO conversion code
}

@TypeConverter
public static String toString(List<Genre> value) {
    // TODO conversion code
}

Problem :

I am stuck with implementing a TypeConverter to my Database. I have added the TypeConverters but it still keeps saying that it cannot figure out how to save the field into the database. Or maybe I have missed something? I was following this article to create TypeConverters (https://android.jlelse.eu/room-persistence-library-typeconverters-and-database-migration-3a7d68837d6c), which is with my knowledge so far a bit hard to understand.
Any help would be appreciated!

MyGame.java:

package com.riceplant.capstoneproject.room;

import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

import com.riceplant.capstoneproject.data.Cover;
import com.riceplant.capstoneproject.data.Genre;
import com.riceplant.capstoneproject.data.Platform;
import com.riceplant.capstoneproject.data.ReleaseDate;
import com.riceplant.capstoneproject.data.Video;

import java.util.List;

@Entity(tableName = "game")
public class MyGame {

    @PrimaryKey(autoGenerate = true)
    private Integer mId;
    private Cover mCover;
    private String mName;
    private Double mPopularity;
    private String mSummary;
    private List<Genre> mGenres;
    private List<Platform> mPlatform;
    private Double mRating;
    private List<ReleaseDate> mReleaseDate;
    private List<Video> mVideos;

    @Ignore
    public MyGame() {
    }

    public MyGame(Integer id,
                  Cover cover,
                  String name,
                  Double popularity,
                  String summary,
                  List<Genre> genres,
                  List<Platform> platform,
                  Double rating,
                  List<ReleaseDate> releaseDate,
                  List<Video> videos) {
        mId = id;
        mCover = cover;
        mName = name;
        mPopularity = popularity;
        mSummary = summary;
        mGenres = genres;
        mPlatform = platform;
        mRating = rating;
        mReleaseDate = releaseDate;
        mVideos = videos;
    }

    public Integer getId() {
        return mId;
    }

    public void setId(Integer id) {
        id = mId;
    }

    public Cover getCover() {
        return mCover;
    }

    public void setCover(Cover cover) {
        cover = mCover;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        name = mName;
    }

    public Double getPopularity() {
        return mPopularity;
    }

    public void setPopularity(Double popularity) {
        popularity = mPopularity;
    }

    public String getSummary() {
        return mSummary;
    }

    public void setSummary(String summary) {
        summary = mSummary;
    }

    public List<Genre> getGenres() {
        return mGenres;
    }

    public void setGenres(List<Genre> genres) {
        genres = mGenres;
    }

    public List<Platform> getPlatform() {
        return mPlatform;
    }

    public void setPlatform(List<Platform> platform) {
        platform = mPlatform;
    }

    public Double getRating() {
        return mRating;
    }

    public void setRating(Double rating) {
        rating = mRating;
    }

    public List<ReleaseDate> getReleaseDate() {
        return mReleaseDate;
    }

    public void setReleaseDate(List<ReleaseDate> releaseDate) {
        releaseDate = mReleaseDate;
    }

    public List<Video> getVideos() {
        return mVideos;
    }

    public void setVideos(List<Video> videos) {
        videos = mVideos;
    }
}

Converters.java

package com.riceplant.capstoneproject.room;

import androidx.room.TypeConverter;

import com.riceplant.capstoneproject.data.Cover;
import com.riceplant.capstoneproject.data.Genre;
import com.riceplant.capstoneproject.data.Platform;
import com.riceplant.capstoneproject.data.Video;

public class Converters {
    @TypeConverter
    public static Cover toCover(String value) {
        return value == null ? null : new Cover();
    }

    @TypeConverter
    public static String toString(Cover value) {
        return value == null ? null : value.getUrl();
    }

    @TypeConverter
    public static Genre toGenre(String value) {
        return value == null ? null : new Genre();
    }

    @TypeConverter
    public static String toString(Genre value) {
        return value == null ? null : value.getName();
    }

    @TypeConverter
    public static Platform toPlatform(String value) {
        return value == null ? null : new Platform();
    }

    @TypeConverter
    public static String toString(Platform value) {
        return value == null ? null : value.getName();
    }

    @TypeConverter
    public static Video toString(String value) {
        return value == null ? null : new Video();
    }

    @TypeConverter
    public static String toVideo(Video value) {
        return value == null ? null : value.getVideoId();
    }
}

GameRoomDatabase.java

package com.riceplant.capstoneproject.room;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;

@Database(entities = {MyGame.class}, version = 3, exportSchema = false)
@TypeConverters({Converters.class})
public abstract class GameRoomDatabase extends RoomDatabase {
    private static final String LOG_TAG = GameRoomDatabase.class.getSimpleName();
    private static final Object LOCK = new Object();
    private static final String DATABASE_NAME = "gameslist";
    private static GameRoomDatabase sInstance;

    public static GameRoomDatabase getInstance(Context context) {
        if (sInstance == null) {
            synchronized (LOCK) {
                sInstance = Room.databaseBuilder(context.getApplicationContext(),
                        GameRoomDatabase.class, GameRoomDatabase.DATABASE_NAME)
                        .fallbackToDestructiveMigration()
                        .build();
            }
        }
        return sInstance;
    }

    public abstract GameDao gameDao();
}

GameDao.java

package com.riceplant.capstoneproject.room;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface GameDao {

    @Query("SELECT * FROM game ORDER BY mId")
    LiveData<List<MyGame>> loadAllGames();

    @Insert
    void insertGame(MyGame myGame);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateGame(MyGame myGame);

    @Delete
    void deleteGame(MyGame myGame);

    @Query("SELECT * FROM game WHERE mId = :id")
    MyGame loadGameById(int id);
}

GameViewModel

package com.riceplant.capstoneproject.room;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.List;

public class GameViewModel extends AndroidViewModel {

    private LiveData<List<MyGame>> games;

    public GameViewModel(@NonNull Application application) {
        super(application);
        GameRoomDatabase database = GameRoomDatabase.getInstance(this.getApplication());
        games = database.gameDao().loadAllGames();
    }

    public LiveData<List<MyGame>> getGames() {
        return games;
    }
}

Comments

Comment posted by riceplant

Thank you very much, I will try to fix it 🙂

READ  [ANSWERED] Non-deprecated way to hide status bar on android?
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress