0

I am trying to get a list of songs marked as favorite and display in the favorite fragment. however the app is crashing every time I try to open the favorite screen. I have made the database object static in MainActivity(holds all the fragments)

MainActivity.Kt

class MainActivity : AppCompatActivity() {

    var drawerLayout: DrawerLayout?=null

    object static{
        var favData : EchoDatabase?=null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        favData = EchoDatabase(this)
        val homeFragment = HomeFragment()
        this.supportFragmentManager
            .beginTransaction()
            .add(R.id.fragmentContainer,homeFragment,"HomeFragment")
            .commit()
        initializedrawerlayout()
        initialiseRecyclerView(this)
    }

SongPlaying.kt (The favourite Songs are sent from here)

favbutton?.setOnClickListener {
            if(fav)
            {
                favbutton?.setBackgroundResource(R.drawable.favorite_off)
                favData?.deleteSongs(song!![position].songID)
                fav=false
            }
            else
            {
                fav=true
                favbutton?.setBackgroundResource(R.drawable.favorite_on)
                favData?.storeSongs(song!![position])

            }
        }

Favourites.Kt

package com.example.echo.Fragments

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.RelativeLayout
import android.widget.TextView
import com.example.echo.Adapter.SongListAdapter
import com.example.echo.DataModels.Songs
import com.example.echo.Database.EchoDatabase
import com.example.echo.MainActivity
import com.example.echo.MainActivity.static.favData
import com.example.echo.R

class Favourites : Fragment() {

    var mcontext : Activity?=null
    var favsonglistRV : RecyclerView?=null
    var favsonglistVisibleLayout: RelativeLayout?=null
    var favnoSongs : RelativeLayout?=null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        var view = inflater.inflate(R.layout.fragment_favourites,container,false)
        favsonglistRV = view.findViewById<RecyclerView>(R.id.fav_songsRV)
        favsonglistVisibleLayout = view.findViewById<RelativeLayout>(R.id.fav_songslist_visible_layouyt)
        favnoSongs = view.findViewById<RelativeLayout>(R.id.fav_nosongs)
        return view
    }

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        mcontext =context as Activity
    }

    override fun onAttach(activity: Activity?) {
        super.onAttach(activity)
        mcontext =activity
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        if(MainActivity.static.favData!!.checkifdata() && MainActivity.static.favData!!.getsongs()!=null)
        {
            favsonglistVisibleLayout?.visibility = View.VISIBLE
            favnoSongs?.visibility = View.INVISIBLE
            intitialiseRV(favData!!.getsongs(),mcontext as Context)
        }
        else
        {
            favsonglistVisibleLayout?.visibility = View.INVISIBLE
            favnoSongs?.visibility = View.VISIBLE
        }
    }

    fun intitialiseRV(songs: ArrayList<Songs>, context: Context)
    {
        favsonglistRV?.layoutManager = LinearLayoutManager(context)
        var adapter = SongListAdapter(songs,context)
        favsonglistRV?.adapter =adapter
    }
}

EchoDatabase.KT

package com.example.echo.Database

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.example.echo.DataModels.Songs
import com.example.echo.Database.EchoDatabase.static.db_name
import java.lang.Exception

class EchoDatabase : SQLiteOpenHelper {
    object static
    {
        val db_name ="fav_song_db"
    }

    val tname = "fav_song_list"
    val _id ="song_ID"
    val title ="song_title"
    val artist = "song_artist"
    val path = "song_path"

    constructor(
        context: Context?,
        name: String?,
        factory: SQLiteDatabase.CursorFactory?,
        version: Int
    ) : super(context, name, factory, version)

    constructor(context: Context?) : super(context,db_name,null,1)

    override fun onCreate(db: SQLiteDatabase?) {
        var query = "CREATE TABLE $tname($_id LONG,$title STRING,$artist STRING,$path STRING );"
        db?.execSQL(query)
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        TODO("Not yet implemented")
    }

    fun storeSongs (i : Songs)
    {
        val db = this.writableDatabase
        var contentValues = ContentValues()
        contentValues.put(_id,i.songID)
        contentValues.put(title,i.songTitle)
        contentValues.put(artist,i.songArtist)
        contentValues.put(path,i.songData)
        db.insert(tname,null,contentValues)
        db.close()
    }

    fun checkSong(songID: Long): Boolean {
        val db = this.readableDatabase
        var query = "SELECT * FROM $tname WHERE $_id = $songID;"
        var cursor = db.rawQuery(query,null)
        try {
            if(cursor.moveToFirst())
            {
                db.close()
                return true
            }
        }
        catch (e: Exception)
        {
            e.printStackTrace()
        }
        db.close()
        return false
    }

    fun deleteSongs(songs: Long) {
        val db = this.writableDatabase
        db.delete(tname, "$_id=$songs", null)
        db.close()
    }

    fun checkifdata (): Boolean
    {
        val db = this.readableDatabase
        var query = "SELECT * FROM $tname"
        var cursor = db.rawQuery(query,null)
        try {
            if(cursor.moveToFirst())
            {
                db.close()
                return true
            }
        }
        catch (e: Exception)
        {
            e.printStackTrace()
        }
        db.close()
        return false
    }

    fun getsongs(): ArrayList<Songs>
    {
        var favlist: ArrayList<Songs>?=null
        val db = this.readableDatabase
        var query = "SELECT * FROM $tname"
        var cursor = db.rawQuery(query,null)
        try {
            if(cursor.moveToFirst())
            {

            }
        }
        catch (e: Exception)
        {
            e.printStackTrace()
        }
        return favlist!!
    }

}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

0 Answers0