我有一个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) 这是我的代码:
var header1: Record? = null
var header2: Record? = null
header2 = header1
header2.name = "new_name"
Run Code Online (Sandbox Code Playgroud)
但也有header1.name变化!
我有一个功能,可以使用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) 我尝试使用此功能检查我的网络状态(已连接或已断开连接):
// 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 - 在断开网络运行时出错.为什么?我该如何解决这个问题?
我正在使用,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
我尝试在 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)
图片在这里是我想要的 :- 点击这里
我有一个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 ×6
kotlin ×6
firebase ×3
adapter ×1
checkbox ×1
connectivity ×1
duplicates ×1
firebaseui ×1
object ×1