小编CEO*_*pps的帖子

Kotlin:内部类如何访问在外部类中声明为参数的变量?

我有一个DetailAdapter参数categoryId类型为 的类String。我需要在内部类中访问这个变量DetailHolder。我收到以下错误:

未解决的参考:categoryId

我该如何解决这个问题?

class DetailAdapter(lifecycleOwner: LifecycleOwner, categoryId: String) : FirebaseRecyclerAdapter<Detail, DetailAdapter.DetailHolder>(DetailAdapter.buildOptions(lifecycleOwner, categoryId)) {

companion object {
    private fun buildQuery(categoryId: String) = FirebaseDatabase.getInstance()
            .reference
            .child("").child("details").child(categoryId)
            .limitToLast(50)

    private fun buildOptions(lifecycleOwner: LifecycleOwner, categoryId: String) = FirebaseRecyclerOptions.Builder<Detail>()
            .setQuery(buildQuery(categoryId), Detail::class.java)
            .setLifecycleOwner(lifecycleOwner)
            .build()
}

class DetailHolder(val customView: View, var detail: Detail? = null) : RecyclerView.ViewHolder(customView) {
    private val TAG = DetailHolder::class.java.simpleName

    fun bind(detail: Detail) {
        with(detail) {
            customView.textView_name?.text = detail.detailName
            customView.textView_description?.text = detail.detailDescription

            val detailId = detail.detailId …
Run Code Online (Sandbox Code Playgroud)

android kotlin

10
推荐指数
1
解决办法
2100
查看次数

如何在不更改Kotlin中的基础对象的情况下将对象复制到另一个对象?

这是我的代码:

var header1: Record? = null
var header2: Record? = null

header2 = header1
header2.name = "new_name"
Run Code Online (Sandbox Code Playgroud)

但也有header1.name变化!

object duplicates kotlin

8
推荐指数
1
解决办法
5504
查看次数

将文件上传到 Firebase Storage 并获取 downloadUrl。如何在 Kotlin 函数中返回结果?

我有一个功能,可以使用uploadTask. 我按照文档中给出的说明进行操作。这是我的代码:

fun uploadAudioFile(file: File){
    val audioFilePathUri = Uri.fromFile(file)
    val ref = currentUserRef.child("audioFiles/" + System.currentTimeMillis() + "." + "m4a")
    val uploadTask = ref.putFile(audioFilePathUri)

    val urlTask =
        uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    throw it
                }
            }
            return@Continuation ref.downloadUrl
        }).addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val downloadUri = task.result
                Log.d("STORAGE_UTIL", "downloadUri: " + downloadUri)
            } else {
                // Handle failures
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

该函数工作正常并呈现正确的downloadUri

现在我的问题:我想重写这个函数,以便它返回downloadUri。像这样的东西:

 fun uploadAudioFile(file: File): Uri? …
Run Code Online (Sandbox Code Playgroud)

android kotlin firebase firebase-storage

5
推荐指数
1
解决办法
1062
查看次数

Kotlin:如果网络断开,使用ConnectivityManager检查网络状态将返回null.怎么会?

我尝试使用此功能检查我的网络状态(已连接或已断开连接):

// Check Network status
private fun isNetworkAvailable(): Boolean {
    val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE)
    return if (connectivityManager is ConnectivityManager) {
        val networkInfo = connectivityManager.activeNetworkInfo
        networkInfo.isConnected
    }
    else false
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个 java.lang.IllegalStateException:networkInfo不能为null - 在断开网络运行时出错.为什么?我该如何解决这个问题?

android connectivity kotlin

2
推荐指数
2
解决办法
6608
查看次数

升级到FirebaseUI 3.0后无法使用FirebaseRecyclerOptions检索数据

我正在使用,FirebaseRecyclerOptions因为我升级到新的FirebaseUI 3.0版本,但现在我无法从数据库中检索任何内容.过去在旧FirebaseRecylcerAdapter方法中使用的代码相同.好像它甚至没有进入onBindViewHolder.

初始化

linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView = findViewById(R.id.main_recycler);
        recyclerView.hasFixedSize();
        recyclerView.setLayoutManager(linearLayoutManager);    
queryDatbase = mDatabase.child("chatlist").child(cUser);
        queryDatbase.keepSynced(true);
Query query = queryDatbase.orderByChild("time");
        query.keepSynced(true);
Run Code Online (Sandbox Code Playgroud)

FirebaseRecyclerAdapter代码

firebaseoptions = new FirebaseRecyclerOptions.Builder<ConvModel>().setQuery(query,ConvModel.class).build();

    firebaseadapter = new FirebaseRecyclerAdapter<ConvModel, ChatlistHolder>(firebaseoptions) {
        @Override
        protected void onBindViewHolder(final ChatlistHolder holder, int position,final ConvModel model) {
            final String each_user_id = firebaseadapter.getRef(position).getKey();
            Toast.makeText(MainActivity.this, each_user_id, Toast.LENGTH_SHORT).show();
            //region SET EACH USER IMAGE and USERNAME
            assert each_user_id != null;
            mDatabase.child("users").child(each_user_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) { …
Run Code Online (Sandbox Code Playgroud)

android firebase android-recyclerview firebase-realtime-database firebaseui

2
推荐指数
1
解决办法
357
查看次数

如何在Android Kotlin的RecyclerView中为CheckBox添加isChecked和unChecked事件

我尝试在 Android Kotlin 中为选中/未选中(checkBox)事件实现 OnCheckedChangeListener。但它不起作用。这是我的代码:-

checkBox?.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
        override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
            if (checkBox!!.isChecked) {
                Toast.makeText(applicationContext, "checked ", Toast.LENGTH_LONG).show()
            }
            else{
                Toast.makeText(applicationContext, "unCkecked", Toast.LENGTH_LONG).show()
            }

        }
    })
Run Code Online (Sandbox Code Playgroud)

图片在这里是我想要的 :- 点击这里

checkbox android kotlin

1
推荐指数
1
解决办法
4346
查看次数

Kotlin:如何在Adapter类中调用DialogFragment?

我有一个MainAdapter.kt类来处理RecyclerView.在其Holder类中,我使用OnLongClickListener调用函数deleteCategory(categoryId)来删除Firebase数据库中的条目.这非常有效:

class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {
    private val TAG = CategoryHolder::class.java.simpleName

    fun bind(category: Category) {
        with(category) {
            customView.textView_name?.text = category.name
            customView.textView_description?.text = category.description

            val categoryId = category.id

            customView.setOnClickListener {
                    // do something
            }

            customView.setOnLongClickListener(
                    {
                       deleteCategory(categoryId)
                       true
                    }
            )
        }
    }

    private fun deleteCategory(categoryId: String) {
        val database = FirebaseDatabase.getInstance()

        val myRef = database.getReference("categories").child(categoryId)
        myRef.removeValue()
        Log.d(TAG, "Category with id " + categoryId + " deleted")
    }
}
Run Code Online (Sandbox Code Playgroud)

但我宁愿调用DialogFragment类中的函数而不是deleteCategory(id)函数,如下所示:

  // Create an instance of …
Run Code Online (Sandbox Code Playgroud)

android adapter kotlin firebase dialogfragment

0
推荐指数
1
解决办法
1903
查看次数