我正在尝试使用 Kotlin 创建一个故事书应用程序,但我是初学者,正在学习编程。我尝试在 home 片段中使用本地 Json 文件(loadJsonFromAssets)和 glide 创建此应用程序,但出现此错误“lateinit property itemsList 尚未初始化”,我不知道如何修复它,请帮助我修复它。
class HomeFragment : Fragment() {
private lateinit var bookAdapter: BookAdapter
private lateinit var recyclerView: RecyclerView
private lateinit var itemsList: ArrayList<BooksData>
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
val view = inflater.inflate(R.layout.fragment_home, container, false)
// Initialize the RecyclerView
recyclerView = view.findViewById(R.id.bookRecyclerView)
recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
bookAdapter = BookAdapter(this@HomeFragment, itemsList)
recyclerView.adapter = bookAdapter
recyclerView.setHasFixedSize(true)
itemsList = ArrayList<BooksData>()
loadData()
return view
}
private fun loadData() {
val jsonString = loadJson("contents.json")
val items = parseJson(jsonString)
bookAdapter.setData(items)
}
private fun loadJson(contents: String): String {
val inputStream = requireContext().assets.open(contents)
val size = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
inputStream.close()
return String(buffer)
}
private fun parseJson(jsonString: String): ArrayList<BooksData> {
val jsonArray = JSONArray(jsonString)
val items = ArrayList<BooksData>()
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
val imageUrl = jsonObject.getString("imageUrl")
val bookName = jsonObject.getString("bookName")
items.add(BooksData(imageUrl, bookName))
}; return items
}
}
data class BooksData(val bookName:String, val imageUrl:String){
}
class BookAdapter(var context: HomeFragment, private var dataList: ArrayList<BooksData>) :
RecyclerView.Adapter<BookAdapter.BookViewHolder>() {
inner class BookViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var bookNameTextView: TextView = itemView.findViewById(R.id.bookName)
var bookImageView: ImageView = itemView.findViewById(R.id.imageUrl)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.book_category_item, parent, false)
return BookViewHolder(view)
}
override fun getItemCount(): Int = dataList.size
fun setData(data: ArrayList<BooksData>) {
dataList.clear()
dataList.addAll(data)
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: BookViewHolder, position: Int) {
val item = dataList[position]
holder.bookNameTextView.text = item.bookName
Glide.with(context).load(item.imageUrl).placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.vec_quiz).into(holder.bookImageView)
}
}
{
"items": \[
{
"bookName": "Basic Grammar",
"imageUrl": "https://drive.google.com/uc?id=1_eVoTO4IeTW1gAbNBb_0-GWASecBNGgS"
} ,
{
"bookName": "Basic Grammar",
"imageUrl": "https://drive.google.com/uc?id=1_eVoTO4IeTW1gAbNBb_0-GWASecBNGgS"
}
\]
}
请帮我解决这个错误,谢谢
在您的片段的
onCreateView
itemsList
在初始化之前正在使用:
bookAdapter = BookAdapter(this@HomeFragment, itemsList)
//....
itemsList = ArrayList<BooksData>()
您必须首先通过以下任一方式初始化
lateinit
属性 itemsList
:
itemsList = ArrayList<BooksData>()
bookAdapter = BookAdapter(this@HomeFragment, itemsList)
lateinit
修饰符:private var itemsList = ArrayList<BooksData>()