2017-01-25 7 views
0

私はrecyclerViewを実装している基本的なAndroidアプリでAnkoを使用しています。 onCreateViewHolder()メソッドでは、タイプミスマッチと言ってコンパイル時にエラーが発生します。以下のコードでは他のすべてがうまくいきます。Ankoのタイプミスマッチエラーが必要ですAnkoContext <ViewGroup> Found AnkoContext <Context>

class ListAdapter(val arrayList: ArrayList<String> = ArrayList<String>()) : RecyclerView.Adapter<Holder>() { 

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder? { 

     //type Mismatch error required AnkoContext<ViewGroup> Found AnkoContext<Context> 
     return Holder(ItemUI().createView(AnkoContext.create(parent!!.context))) 

    } 

    override fun onBindViewHolder(holder: Holder, position: Int) { 
     val item = arrayList.get(position) 
     holder.bind(item) 
    } 

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

} 

class ItemUI : AnkoComponent<ViewGroup> { 
    override fun createView(ui: AnkoContext<ViewGroup>): View { 
     return with(ui) { 

      verticalLayout { 
       lparams(width = matchParent, height = dip(48)) 
       horizontalPadding = dip(16) 

       var name=textView { 
        id = 7 
        singleLine = true 
        textSize = 16f 
       } 
       name.onClick { 
        toast("Hi, ${name.text}!") 
       } 
      } 
     } 
    } 
} 

class Holder(itemView: View) : RecyclerView.ViewHolder(itemView){ 
    val name: TextView = itemView.find(1) 

    fun bind(nm: String) { 
     name.text = nm 
    } 
} 

私は間違った構文を使用していた場合、私に知らせたりrecyclerviewの実装が誤っ

後半に答えて申し訳ありませんが、ちょうど私がその存在に気づいアンコとrecyclerViewアダプタの私のコードを拾い読み

答えて

2

でください。 AnkoContextのためのクリエイターは、次のシグネチャである:

AnkoContext.create(ctx: Context, owner: ViewGroup, setContentView: Boolean = false) 
AnkoContext.create(ctx: Context, setContentView: Boolean = false) 

IDE(Androidのメーカーが)あなたは間違っているラインを強調しています。私は最初のものを使用した:

AnkoContext.create(parent!!.context, parent) 
関連する問題