kotlin 声明GroupieViewHolder时显示错误

kwvwclae  于 2022-11-16  发布在  Kotlin
关注(0)|答案(1)|浏览(135)

我新的Kotlinandroid开发,并试图建立一个信使遵循YouTube教程,让我们建立应用程序(https://www.youtube.com/watch?v=SuRiwVF5bzs&list=PL0dzCUj1L5JE-jiBHjxlmXEkQkum_M3R-&index=4〉从19:33)我发现如果GroupieViewHolder不工作,我花了4个小时的时间,但仍然无法找到arror.
下面是我所尝试的。
这是我的项目级别gradle.file '

buildscript {
    ext.kotlin_version = '1.6.21'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.google.gms:google-services:4.3.14'

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false

}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的应用程序级别gradle.file

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.google.gms.google-services'
    id 'kotlin-android-extensions'
}

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdk 32

    androidExtensions {
        experimental = true
    }

    defaultConfig {
        applicationId "com.example.assignment"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.google.firebase:firebase-auth-ktx:21.1.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation platform('com.google.firebase:firebase-bom:31.0.1')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-storage-ktx'
    implementation 'com.google.firebase:firebase-database-ktx'

    implementation 'de.hdodenhof:circleimageview:3.1.0'

    //Progress dialog library
    implementation 'com.jpardogo.googleprogressbar:library:1.2.0'

    //TODO https://github.com/lisawray/groupie  cannnot add groupie into gradle.
    //implementation "com.github.lisawray.groupie:groupie:$groupie_version"
    implementation "com.github.lisawray.groupie:groupie:2.10.0"
    implementation 'com.github.lisawray.groupie:groupie-kotlin-android-extensions:2.10.0'

    implementation 'com.squareup.picasso:picasso:2.71828'
}

这是设置:gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }  
       jcenter() 
    }
}
rootProject.name = "Assignment"
include ':app'

这是活动文件(NewMessageActivity.kt)

package com.example.assignment

import android.content.ClipData
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.renderscript.Sampler
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.squareup.picasso.Picasso
import com.xwray.groupie.GroupieAdapter
import kotlinx.android.synthetic.main.activity_new_message.*

import com.xwray.groupie.kotlinandroidextensions.Item
import com.xwray.groupie.kotlinandroidextensions.GroupieViewHolder
import kotlinx.android.synthetic.main.song.*  //THIS CANNOT BE IMPORTED
import kotlinx.android.synthetic.main.my_item_layout.* //THIS CANNOT BE IMPORTED
import kotlinx.android.synthetic.main.user_row_new_message.view.*

class NewMessageActivity : AppCompatActivity() {

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

        supportActionBar?.title="Select User"

        fetchUsers()
    }

    private fun fetchUsers() {
        val ref = FirebaseDatabase.getInstance().getReference("/Users")
        ref.addListenerForSingleValueEvent(object: ValueEventListener {
            override fun onDataChange(p0: DataSnapshot) {
                val adapter = GroupieAdapter<GroupieViewHolder>() //HERE IS THE ERROR

                p0.children.forEach {
                    Log.d("NewMessage", it.toString())
                    val user = it.getValue(User::class.java)
                    if(user != null) {
                        adapter.add(UserItem(user))
                    }

                }
                recyclerview_newmessage.adapter=adapter
            }

            override fun onCancelled(p0: DatabaseError) {
                TODO("Not yet implemented")
            }
        }
    }
}
class UserItem(val user: User): Item<GroupieViewHolder>() { //HERE IS THE ERROR
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        viewHolder.itemView.username_textview_new_message.text = user.username
        Picasso.get().load(user.profileImageUrl).into(viewHolder.itemView.imageview_new_message)
    }

    override fun getLayout(): Int {
        return R.layout.user_row_new_message
    }
}

下面是当我将鼠标悬停到GroupieViewHolder时显示的错误

2guxujil

2guxujil1#

请按照以下步骤操作,
1.kotlin-android-extension已弃用。请使用ViewBindingDataBinding以便正常工作。

//remove deprecated 
 implementation 'com.github.lisawray.groupie:groupie-kotlin-android-extensions:2.10.0'

//for viewbinding ,replace with
implementation "com.github.lisawray.groupie:groupie-viewbinding:2.10.0"

1.将Groupie Libary更新到最新版本(2. 10. 1),增加了对Kotlin版本1. 6(check more about here.)的支持。
一旦你添加,它会像下面. x1c 0d1x
1.在android标记内的应用程序级别分级文件中启用视图绑定数据绑定
视图绑定{已启用=真}

1.如果您不知道viewBinding,请了解它(这很容易理解)。
1.现在您必须在NewMessageActivity.kt中将用户项更改为以下内容,

//item with viewBinding.
 class UserItem(val user: User): BindableItem<UserRowNewMessageBinding>() {

 // Other implementations...
 override fun bind(viewBinding: UserRowNewMessageBinding, position: Int) {
     viewBinding.usernameTextviewNewMessage.text = user.username
     Picasso.get()
            .load(user.profileImageUrl)
            .into(viewBinding.imageviewNewMessage)
 }

 override fun getLayout(): Int = R.layout.user_row_new_message

 override fun initializeViewBinding(view: View): UserRowNewMessageBinding {
     return UserRowNewMessageBinding.bind(view)
 }
 }

*UserRowNewMessageBinding-您的项布局绑定ID。

  • viewBinding.usernameTextviewNewMessage-您的项目文本视图绑定ID。
  • viewBinding.imageviewNewMessage-您的项目图像视图绑定ID。

1.更改newMessageActivity.ktfetchUser()GroupieAdapter的初始化,如下所示

val adapter = GroupieAdapter() //remove the Generic Type here. which is no longer need.

年级文件中更改任何内容后,请不要忘记同步、重建

提示,如果您是Android开发新手,请始终从最新版本的视频、博客或至少2年前的视频/博客中学习。

相关问题