kotlin 类型不匹配:推断的类型是CategoryQuran,但不是MainAdapter,onSelectData

gcuhipw9  于 2023-01-02  发布在  Kotlin
关注(0)|答案(1)|浏览(154)

我只是想代码获取API,但当我写了一些代码。它似乎错误,我花了几个小时,但我不明白的重点,你们能帮助我吗?
这是活动(活动古兰经)

class CategoryQuran : AppCompatActivity() {

    var mainAdapter: MainAdapter? = null
    var mProgressBar: ProgressDialog? = null
    var modelMain: MutableList<ModelMain> = ArrayList()

    private lateinit var adapter: MainAdapter
    private lateinit var postArrayList: ArrayList<MainAdapter>

    private lateinit var progressDialog: ProgressDialog

    private val TAG = "MAIN_TAG"
    private var isSearch = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail_artikel)

        //setup progress dialog
        progressDialog = ProgressDialog (this)
        progressDialog.setTitle("Mohon Tunggu")

        mProgressBar = ProgressDialog(this)
        mProgressBar!!.setTitle("Mohon Tunggu")
        mProgressBar!!.setCancelable(false)
        mProgressBar!!.setMessage("Sedang menampilkan data...")

        llAbout.setOnClickListener {
            startActivity(Intent(this@CategoryQuran, AboutActivity::class.java)) }

        llPP.setOnClickListener {
            startActivity(Intent(this@CategoryQuran, PrivacyPolicyActivity::class.java)) }

        llDisclaimer.setOnClickListener {
            startActivity(Intent(this@CategoryQuran, DisclaimerActivity::class.java)) }


        rvListArticles.setHasFixedSize(true)
        rvListArticles.setLayoutManager(LinearLayoutManager(this))

        //get data
        listArticle

        //search
        searchBtn.setOnClickListener {

        }



    }

    private val listArticle: Unit
        private get() {
            mProgressBar!!.show()
            AndroidNetworking.get(BloggerApi.ListPost)
                .setPriority(Priority.MEDIUM)
                .build()
                .getAsJSONObject(object : JSONObjectRequestListener {
                    override fun onResponse(response: JSONObject) {
                        try {
                            mProgressBar!!.dismiss()
                            val playerArray = response.getJSONArray("items")
                            for (i in 0 until playerArray.length()) {
                                val jsonObject1 = playerArray.getJSONObject(i)
                                val dataApi = ModelMain()

                                dataApi.title = jsonObject1.getString("title")
                                dataApi.content = jsonObject1.getString("content")
                                dataApi.labels = jsonObject1.getString("labels")
                                dataApi.url = jsonObject1.getString("url")

                                val datePost = jsonObject1.getString("published")
                                val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                                val outputFormat = SimpleDateFormat("dd-MM-yyyy")
                                val date = inputFormat.parse(datePost)
                                val datePostConvert = outputFormat.format(date)
                                dataApi.published = datePostConvert

                                val jsonObject2 = jsonObject1.getJSONObject("author")
                                val authorPost = jsonObject2.getString("displayName")
                                dataApi.author = authorPost

                                val jsonObject3 = jsonObject2.getJSONObject("image")
                                val authorImage = jsonObject3.getString("url")
                                dataApi.authorImage = Uri.parse("http:$authorImage").toString()
                                modelMain.add(dataApi)
                                showListArticle()
                            }
                        } catch (e: JSONException) {
                            e.printStackTrace()
                            Toast.makeText(this@CategoryQuran,
                                "Gagal menampilkan data!", Toast.LENGTH_SHORT).show()
                        } catch (e: ParseException) {
                            e.printStackTrace()
                            Toast.makeText(this@CategoryQuran,
                                "Gagal menampilkan data!", Toast.LENGTH_SHORT).show()
                        }
                    }

                    override fun onError(anError: ANError) {
                        mProgressBar!!.dismiss()
                        Toast.makeText(this@CategoryQuran,
                            "Tidak ada jaringan internet!", Toast.LENGTH_SHORT).show()
                    }
                })
        }

    private fun showListArticle(){
        mainAdapter = MainAdapter(this@CategoryQuran, modelMain, this )
        rvListArticles!!.adapter = mainAdapter
    }

    private fun searchPosts(query: String) {

        mainAdapter = MainAdapter(this@CategoryQuran, modelMain, this)
        rvListArticles!!.adapter = mainAdapter

    }

    override fun onSelected(modelMain: ModelMain) {
        val intent = Intent(this@CategoryQuran, DetailArtikelActivity::class.java)
        intent.putExtra("detailArtikel", modelMain)
        startActivity(intent)
    }

}

我在**'this'中出错**

private fun showListArticle(){
        mainAdapter = MainAdapter(this@CategoryQuran, modelMain, this )
        rvListArticles!!.adapter = mainAdapter
    }

    private fun searchPosts(query: String) {

        mainAdapter = MainAdapter(this@CategoryQuran, modelMain, this)
        rvListArticles!!.adapter = mainAdapter

    }

两者均显示“类型不匹配:推断类型为CategoryQuran,但应为MainAdapter.onSelectData!'
你们能帮帮我吗?拜托

nxowjjhe

nxowjjhe1#

好了,问题是我必须在我的类中添加OnSelectData,在我的情况下,我必须做的是添加MainAdaper。OnSelectData
所以在它之前:

class CategoryQuran : AppCompatActivity() {

在它之后是这样的:

class CategoryQuran : AppCompatActivity(), MainAdapter.OnSelectData {

这是我的错,我太匆忙,当我编码它

相关问题