无法导入适配器

时间:2019-06-11 16:13:13

标签: android kotlin import recycler-adapter

我正在学习Android的Kotlin和Mvvm。我正在使用回收站视图,并且当我尝试设置适配器时,无法导入Adapter类,我不知道问题是否出在代码中,因为Android Studio让我在Adapter类而不是Adapter类中导入ViewHolder类

FrontPageActivity.kt

package com.jmyp.resport.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.jmyp.resport.model.New
import com.jmyp.resport.R
import com.jmyp.resport.viewmodel.NewViewModel

class FrontPageActivity : AppCompatActivity() {

    lateinit var adapter : NewsAdapter // I can not import this but i can 
                                       // NewsAdapter.NewViewHolder

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

        var viewModelNews = ViewModelProviders.of(this).get(NewViewModel::class.java)
        viewModelNews.getNews().observe(this, Observer<ArrayList<New>> { news ->
            adapter = NewsAdapter(this, news)
        })

    }
}

NewsAdapter.kt

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.jmyp.resport.model.New
import com.jmyp.resport.R
import com.jmyp.resport.view.FrontPageActivity
import kotlinx.android.synthetic.main.row_front_page.view.*



class NewsAdapter(private val context: FrontPageActivity, private val news : ArrayList<New>) : RecyclerView.Adapter<NewsAdapter.NewViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewViewHolder {
        return NewViewHolder(LayoutInflater.from(context).inflate(R.layout.row_front_page,parent,false))
    }

    override fun getItemCount(): Int {
        return news.size
    }

    override fun onBindViewHolder(holder: NewViewHolder, position: Int) {
        holder.title.text = news.get(position).titulo
    }

    class NewViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val cover = itemView.iv_cover
        val title = itemView.tv_title
    }

}

1 个答案:

答案 0 :(得分:0)

您似乎在Adapter类中缺少包装。添加它,然后重试:)