从Singleton类获取未解决的参考

时间:2018-11-01 23:33:07

标签: android-studio kotlin singleton

我在android studio和Kotlin中工作。我正在使用Singleton类,因此我的困惑是,当我尝试将其添加到setupRecyclerView函数中时,我的getInstance函数没有被调用,并且一直说这是一个未解决的引用。我还遇到一个问题,当我尝试引用NewsListActivity.SimpleItemRecyclerViewAdapter时,它也一直得到未解决的引用。任何帮助将不胜感激。

package edu.uw.amjadz.material_news

import android.content.Context
import android.graphics.Bitmap
import android.util.LruCache
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.ImageLoader
import com.android.volley.toolbox.Volley

class Singleton private constructor(context: Context){

    private var INSTANCE: edu.uw.amjadz.material_news.Singleton? = null
    
    fun getInstance(context: Context): edu.uw.amjadz.material_news.Singleton {
        if(INSTANCE == null){
            INSTANCE = Singleton(context)
        }
        return INSTANCE as edu.uw.amjadz.material_news.Singleton
    }

    val requestQueue: RequestQueue by lazy {
        Volley.newRequestQueue(context.applicationContext)
    }

    val imageLoader: ImageLoader by lazy {
        ImageLoader(requestQueue,
            object : ImageLoader.ImageCache{
                private  val cache = LruCache<String, Bitmap>(20)
                override fun getBitmap(url: String?): Bitmap {
                    return  cache.get(url)
                }

                override fun putBitmap(url: String?, bitmap: Bitmap?) {
                    cache.put(url, bitmap)
                }
            }
        )

    }

    fun <T> add(req: Request<T>){
        requestQueue.add(req)
    }

}
 private fun setupRecyclerView(recyclerView: RecyclerView) {
        recyclerView.setAdapter(null)
        val key = getString(R.string.NEWS_API_KEY)
        val url = "https://newsapi.org/v2/everything?q=bitcoin&from=2018-10-01&sortBy=publishedAt&apiKey=$key"

        val jsonObjectRequest = JsonObjectRequest(
            Request.Method.GET, url, null,
            Response.Listener { response ->
                recyclerView.adapter = NewsListActivity.SimpleItemRecyclerViewAdapter(this, DummyContent.parseData(response.toString()), twoPane)

            },
            Response.ErrorListener { error ->

            }
        )
        Singleton.getInstance(this).add(jsonObjectRequest)
        if (twoPane){
            recyclerView.layoutManager = LinearLayoutManager(this)

        } else {
            recyclerView.layoutManager = GridLayoutManager(this, 2)

        }
    }
}

    class SimpleItemRecyclerViewAdapter(
        private val parentActivity: NewsListActivity,
        private val values: List<DummyContent.DummyItem>,
        private val twoPane: Boolean
    ) :
        RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder>() {

        private val onClickListener: View.OnClickListener

        init {
            onClickListener = View.OnClickListener { v ->
                val item = v.tag as DummyContent.DummyItem
                if (twoPane) {
                    val fragment = NewsDetailFragment().apply {
                        arguments = Bundle().apply {
                            putString(NewsDetailFragment.ARG_ITEM_ID, item.id)
                        }
                    }
                    parentActivity.supportFragmentManager
                        .beginTransaction()
                        .replace(R.id.news_detail_container, fragment)
                        .commit()
                } else {
                    val intent = Intent(v.context, NewsDetailActivity::class.java).apply {
                        putExtra(NewsDetailFragment.ARG_ITEM_ID, item.id)
                    }
                    v.context.startActivity(intent)
                }
            }
        }

0 个答案:

没有答案