Android ListView数据更新问题:updateData和deleteData无法正常工作
Android ListView数据更新问题:updateData和deleteData无法正常工作
在Android应用中使用ListView展示数据,并通过ContentProvider进行数据增删改查操作时,有时会遇到updateData和deleteData按钮无法实现预期功能,并且程序闪退的问题。本文将分析代码问题,并提供详细的解决方案。
问题代码:
class MainActivity : AppCompatActivity() {
var bookId: String? = null
private lateinit var adapter: ArrayAdapter<String>
private val dataList = mutableListOf<String>()
private val bookSet = mutableSetOf<String>()
@SuppressLint("Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList)
val listView: ListView = findViewById(R.id.bookslistview)
listView.adapter = adapter
val addData:Button=findViewById(R.id.addData)
addData.setOnClickListener {
// 添加数据
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book")
val values = contentValuesOf("name" to "A Clash of Kings", "author" to "George Martin", "pages" to 1040, "price" to 22.85)
val newUri = contentResolver.insert(uri, values)
bookId = newUri?.pathSegments?.get(1)
// 将数据添加到列表中
if (bookId != null && !bookSet.contains(bookId)) {
val bookInfo = "${values["name"]}- ${values["author"]}- ${values["pages"]}Info)"
bookSet.add(bookId!!)
adapter.notifyDataSetChanged()
}
}
val queryData:Button=findViewById(R.id.queryData)
queryData.setOnClickListener {
dataList.clear()
bookSet.clear()
// 查询数据
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book")
contentResolver.query(uri, null, null, null, null)?.apply {
while (moveToNext()) {
val name = getString(getColumnIndex("name"))
val author = getString(getColumnIndex("author"))
val pages = getInt(getColumnIndex("pages"))
val price = getDouble(getColumnIndex("price"))
Log.d("MainActivity", "book name is $name")
Log.d("MainActivity", "book author is $author")
Log.d("MainActivity", "book pages is $pages")
Log.d("MainActivity", "book price is $price")
val bookInfo = "$name - $author - $pages - $price"
dataList.add(bookInfo)
bookId?.let { it1 -> bookSet.add(it1) }
}
adapter.notifyDataSetChanged() // 通知适配器数据发生变化,更新ListView的显示
close()
}
}
val updateData:Button=findViewById(R.id.updateData)
updateData.setOnClickListener {
// 更新数据
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book")
contentResolver.query(uri, null, null, null, null)?.use { cursor ->
while (cursor.moveToNext()) {
val bookId = cursor.getString(cursor.getColumnIndex("_id"))
if (bookSet.contains(bookId)) {
val values = contentValuesOf("name" to "A Storm of Swords", "author" to "George Martin", "pages" to 1216, "price" to 24.05)
contentResolver.update(Uri.parse("$uri/$bookId"), values, null, null)
val updatedBookInfo = "${values["name"]}- ${values["author"]}- ${values["pages"]} - ${values["price"]}"
val bookIndex = dataList.indexOfFirst { it.startsWith(bookId) }
if (bookIndex != -1) {
dataList[bookIndex] = updatedBookInfo
}
}
}
adapter.notifyDataSetChanged()
}
}
val deleteData:Button=findViewById(R.id.deleteData)
deleteData.setOnClickListener {
// 删除数据
bookId?.let {
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/ book/$it")
contentResolver.delete(uri, null, null)
}
}
}
}
问题分析:
该代码存在以下几个问题,导致updateData和deleteData按钮无法正常工作,并且程序闪退:
- 添加数据时,没有将bookInfo添加到dataList中,导致列表没有更新。
- 查询数据时,没有将bookInfo添加到dataList中,导致列表没有更新。
- 更新数据时,没有将更新后的bookInfo添加到dataList中,导致列表没有更新。
- 删除数据时,删除的uri路径中有多余的空格。
解决方案:
-
添加数据时,将bookInfo添加到dataList中。
if (bookId != null && !bookSet.contains(bookId)) { val bookInfo = "${values["name"]}- ${values["author"]}- ${values["pages"]} - ${values["price"]}" bookSet.add(bookId!!) dataList.add(bookInfo) // 添加到dataList中 adapter.notifyDataSetChanged() } -
查询数据时,将bookInfo添加到dataList中。
while (moveToNext()) { ... val bookInfo = "$name - $author - $pages - $price" dataList.add(bookInfo) // 添加到dataList中 bookId?.let { it1 -> bookSet.add(it1) } } adapter.notifyDataSetChanged() -
更新数据时,将更新后的bookInfo添加到dataList中。
while (cursor.moveToNext()) { ... val updatedBookInfo = "${values["name"]}- ${values["author"]}- ${values["pages"]} - ${values["price"]}" val bookIndex = dataList.indexOfFirst { it.startsWith(bookId) } if (bookIndex != -1) { dataList[bookIndex] = updatedBookInfo // 更新dataList中的数据 } } adapter.notifyDataSetChanged() -
删除数据时,删除的uri路径中有多余的空格。
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book/$it")
修改完毕后的代码:
class MainActivity : AppCompatActivity() {
var bookId: String? = null
private lateinit var adapter: ArrayAdapter<String>
private val dataList = mutableListOf<String>()
private val bookSet = mutableSetOf<String>()
@SuppressLint("Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList)
val listView: ListView = findViewById(R.id.bookslistview)
listView.adapter = adapter
val addData: Button = findViewById(R.id.addData)
addData.setOnClickListener {
// 添加数据
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book")
val values = contentValuesOf("name" to "A Clash of Kings", "author" to "George Martin", "pages" to 1040, "price" to 22.85)
val newUri = contentResolver.insert(uri, values)
bookId = newUri?.pathSegments?.get(1)
// 将数据添加到列表中
if (bookId != null && !bookSet.contains(bookId)) {
val bookInfo = "${values["name"]}- ${values["author"]}- ${values["pages"]} - ${values["price"]}"
bookSet.add(bookId!!)
dataList.add(bookInfo)
adapter.notifyDataSetChanged()
}
}
val queryData: Button = findViewById(R.id.queryData)
queryData.setOnClickListener {
dataList.clear()
bookSet.clear()
// 查询数据
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book")
contentResolver.query(uri, null, null, null, null)?.apply {
while (moveToNext()) {
val name = getString(getColumnIndex("name"))
val author = getString(getColumnIndex("author"))
val pages = getInt(getColumnIndex("pages"))
val price = getDouble(getColumnIndex("price"))
Log.d("MainActivity", "book name is $name")
Log.d("MainActivity", "book author is $author")
Log.d("MainActivity", "book pages is $pages")
Log.d("MainActivity", "book price is $price")
val bookInfo = "$name - $author - $pages - $price"
dataList.add(bookInfo)
bookId?.let { it1 -> bookSet.add(it1) }
}
adapter.notifyDataSetChanged()
close()
}
}
val updateData: Button = findViewById(R.id.updateData)
updateData.setOnClickListener {
// 更新数据
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book")
contentResolver.query(uri, null, null, null, null)?.use { cursor ->
while (cursor.moveToNext()) {
val bookId = cursor.getString(cursor.getColumnIndex("_id"))
if (bookSet.contains(bookId)) {
val values = contentValuesOf("name" to "A Storm of Swords", "author" to "George Martin", "pages" to 1216, "price" to 24.05)
contentResolver.update(Uri.parse("$uri/$bookId"), values, null, null)
val updatedBookInfo = "${values["name"]}- ${values["author"]}- ${values["pages"]} - ${values["price"]}"
val bookIndex = dataList.indexOfFirst { it.startsWith(bookId) }
if (bookIndex != -1) {
dataList[bookIndex] = updatedBookInfo
}
}
}
adapter.notifyDataSetChanged()
}
}
val deleteData: Button = findViewById(R.id.deleteData)
deleteData.setOnClickListener {
// 删除数据
bookId?.let {
val uri = Uri.parse("content://com.example.shiyan2app_a.provider/book/$it")
contentResolver.delete(uri, null, null)
}
}
}
}
总结:
通过以上修改,可以解决updateData和deleteData按钮无法正常工作,并且程序闪退的问题。在开发Android应用时,需要注意数据更新和删除操作时,及时更新ListView数据,并确保URI路径正确,避免出现错误。
原文地址: https://www.cveoy.top/t/topic/egm 著作权归作者所有。请勿转载和采集!