我正在处理一个项目,在该项目中,我必须在回收器视图中加载Sqlite数据,并且我正在使用ROOM。以下是我的适配器代码:-
package com.example.demorecyclerview.adapter
import android.os.AsyncTask
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.room.Room
import com.example.demorecyclerview.MainActivity
import com.example.demorecyclerview.R
import com.example.demorecyclerview.database.contactDatabase
import com.example.demorecyclerview.entity.Contact
import java.lang.Exception
import java.lang.ref.WeakReference
class ContactAdapter() :
RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
/**
* Provide a reference to the type of views that you are using
* (custom ViewHolder).
*/
ContactAdapter.Companion.insertContactAsyncTask(this)
private val name= arrayOf("abc","xyz","pqr")
private val numbers= arrayOf("123","456","789")
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var nameTextView:TextView=view.findViewById(R.id.textViewName)
var mobTextView:TextView=view.findViewById(R.id.textViewMobile)
// val textView: TextView
//
// init {
// // Define click listener for the ViewHolder's View.
// textView = view.findViewById(R.id.textView)
// }
}
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.card_contacts, viewGroup, false)
return ViewHolder(view)
}
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
// Get element from your dataset at this position and replace the
// contents of the view with that element
viewHolder.nameTextView.text=name[position]
viewHolder.mobTextView.text=numbers[position]
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount() = name.size
companion object {
class insertContactAsyncTask internal constructor(context: ContactAdapter) : AsyncTask<Int, String, String?>() {
var res:Long = 0;
var context=context
private val activityReference: WeakReference<ContactAdapter> = WeakReference(context)
override fun doInBackground(vararg params: Int?): String? {
try{
val db = Room.databaseBuilder(
context.,
contactDatabase::class.java, "quest_contacts"
).build()
val contacts:List<Contact> = db.contactDao().getAllContacts()
Log.d("Results","$contacts")
return contacts.toString()
}
catch(e: Exception)
{
Log.e("Exception","$e")
}
return ""
}
// override fun onPostExecute(result: String?) {
// return result
//
// }
}
}
}
由于它是一个普通类,所以我应该在Room.databaseBuilder((中传递什么作为上下文ContactAdapter.Companion.insertContactAsyncTask(此(显示错误";期望成员声明";。请帮我解决这个问题。提前感谢。。。。
您将ContactAdapter.Companion.insertContactAsyncTask(this)
放置在类级别上。在类级别上,只能声明类的字段或方法,不能在那里执行代码。所以你必须声明它,而不是创建一个对象:
class ContactAdapter() :
RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
private val getContacts = ContactAdapter.Companion.insertContactAsyncTask(this)
......
}
然后,您必须在希望执行异步任务的位置执行异步任务。例如,它在构造函数中执行:
constructor {
getContacts.execute()
}
至于Room.databaseBuilder()
的上下文,它不是ContactAdapter
。它是android.content
包中的类Context
。给定您的代码,将其传递给insertContactAsyncTask
的一种方法是将类型为Context
的参数添加到ContactAdapter
,然后将此参数传递到insertContactAsyncTask
。这里有一个例子:
class ContactAdapter(val context: Context) :
RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
private val getContacts = ContactAdapter.Companion.insertContactAsyncTask(context)
......
companion object {
class insertContactAsyncTask internal constructor(context: Context) : AsyncTask<Int, String, String?>() {
var res:Long = 0;
var context=context
private val activityReference: WeakReference<ContactAdapter> = WeakReference(context)
override fun doInBackground(vararg params: Int?): String? {
try{
val db = Room.databaseBuilder(
context,
contactDatabase::class.java, "quest_contacts"
).build()
val contacts:List<Contact> = db.contactDao().getAllContacts()
Log.d("Results","$contacts")
return contacts.toString()
}
catch(e: Exception)
{
Log.e("Exception","$e")
}
return ""
}
}
}