如何从一个 activity 获取 edittext 值到下一个 activity 的 recyclerview?
How to fetch edittext value from one activity to recyclerview of next activity?
我正在尝试从一个 activity 中获取 edittext 值并在 recycleview 中显示该文本并使用 room DB 存储....
基本上这个想法是在 activity 地址中添加地址,当点击时它会重定向到下一页,用户在提交时获取地址表单,它将获取地址并添加到上一个 activity回收视图。
这是我的房间密码:
table:---
@Entity(tableName = "address")
class Address {
@PrimaryKey
var id = 0
@ColumnInfo(name = "address")
var address: String? = null
}
数据库:-
@Database(entities = [Address::class], version = 1)
abstract class Database : RoomDatabase() {
abstract fun AddressDao(): AddressDao
}
地址道:
@Dao
interface AddressDao {
@Insert
suspend fun addData(address: Address)
@Query("select * from address")
fun getAddressesWithChanges() :LiveData<List<Address>>
@Query("SELECT EXISTS (SELECT 1 FROM address WHERE id=:id)")
suspend fun isAddressAdded(id: Int): Int
@Delete
suspend fun delete(address: Address)
}
AddAddressActivity activity:
class AddAddressActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.addaddress)
val editText: EditText = findViewById(R.id.longaddress)
val application = application as CustomApplication
saveaddress.setOnClickListener {
val address = Address()
address.address = editText.getText().toString()
lifecycleScope.launch {
application.addressDao.addData(address)
finish()
}
}
}
}
地址活动:-
class AddressActivity : AppCompatActivity() {
private val adapter = AddressAdapter()
private lateinit var data: LiveData<List<Address>>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.address)
addbutton.findViewById<View>(R.id.addbutton).setOnClickListener {
val intent = Intent(this, AddAddressActivity::class.java)
startActivity(intent)
}
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter
val application = application as CustomApplication
data = application.database.AddressDao().getAddressesWithChanges()
data.observe(this, Observer { words1 ->
// Update the cached copy of the words in the adapter.
words1?.let { adapter.updateData(it) }})
}
}
地址适配器activity:-
class AddressAdapter: RecyclerView.Adapter<AddressAdapter.ViewHolder>() {
private var addresses: List<Address> = Collections.emptyList()
override fun onCreateViewHolder(viewGroup: ViewGroup, itemViewType: Int):
ViewHolder =
ViewHolder(LayoutInflater.from(viewGroup.context)
.inflate(R.layout.address_item, viewGroup, false) )
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val fl: Address = addresses[position]
viewHolder.tv.setText(fl.address)
}
override fun getItemCount(): Int = addresses.size
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tv: TextView
init {
tv = itemView.findViewById(R.id.ftv_name)
} }
fun updateData(addresses:
List<Address>) {
this.addresses = addresses
notifyDataSetChanged() // TODO: use ListAdapter if animations are needed
}
}
自定义应用程序:
class CustomApplication: Application() {
lateinit var database: Database
private set
lateinit var addressDao: AddressDao
private set
override fun onCreate() {
super.onCreate()
this.database = Room.databaseBuilder<Database>(
applicationContext,
Database::class.java, "database"
).build()
}
}
列表不显示 recycleview 返回地址时为空 activity
请帮忙..我是科特林和房间数据库的新手..
请将您的行替换为
val rv=findViewById<RecyclerView>(R.id.recyclerview)
您传递了错误的 ID addressrecyclerview
这就是它分配 null
的原因
abstract fun AddressDao(): AddressDao?
为什么这个可以为空?
val favoriteData: List<Table>?
为什么这个可以为空?
fun addData(favoriteList: Table?)
为什么这个可以为空?
fun delete(favoriteList: Table?)
为什么这个可以为空?
类型化可空性使您能够表示给定值 可以 为空的位置,在某种意义上 它不存在是有道理的.
以上两种情况都不代表这种情况。房间永远不会 return 你 null
而不是实际的 dao
.
Kotlin 的 null 安全性是允许您通过使其成为类型系统的一部分来消除潜在的错误情况,它不应该被用来创建诸如此类的“假”错误情况。如果您尝试将 null
添加到数据库而不是真实对象,则您的代码中可能存在错误,而 Kotlin 应该能够帮助您消除这种可能性。
至于你的实际问题,你在滥用 Room。
.allowMainThreadQueries()
是一种禁用错误检查的方法,但该错误是有原因的。您不应该在 UI 线程上读取数据,因为如果有足够的数据,您的应用将冻结 (ANR)。
代码应该如下所示:
@Entity(tableName = "address")
class Address {
@PrimaryKey
var id = 0
@ColumnInfo(name = "address")
var address: String? = null
}
数据库:-
@Database(entities = [Address::class], version = 1)
abstract class Database : RoomDatabase() {
abstract fun addressDao(): AddressDao
}
地址道:
@Dao
interface AddressDao {
@Insert
suspend fun addData(address: Address)
@Query("select * from address")
fun getAddressesWithChanges(): LiveData<List<Address>>
@Query("SELECT EXISTS (SELECT 1 FROM address WHERE id=:id)")
suspend fun isAddressAdded(id: Int): Int
@Delete
suspend fun delete(address: Address)
}
class CustomApplication: Application() {
lateinit var database: Database
private set
lateinit var addressDao: AddressDao
private set
override fun onCreate() {
super.onCreate()
this.database = Room.databaseBuilder<Database>(
applicationContext,
Database::class.java, "database"
).build()
addressDao = database.addressDao()
}
}
AndroidManifest.xml
<application android:name=".CustomApplication"
...
地址activity:
import androidx.lifecycle.observe
class AddressActivity : AppCompatActivity() {
private val adapter = AddressAdapter()
private lateinit var data: LiveData<List<Address>>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.address_activity)
findViewById<View>(R.id.addButton).setOnClickListener {
val intent = Intent(this, AddAddressActivity::class.java)
startActivity(intent)
}
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter
val application = application as CustomApplication
val addressDao = application.addressDao
data = addressDao.getAddressesWithChanges()
data.observe(this) {
adapter.updateData(it)
}
}
}
地址适配器:
class AddressAdapter: RecyclerView.Adapter<AddressAdapter.ViewHolder>() {
private var addresses: List<Table> = Collections.emptyList()
override fun onCreateViewHolder(viewGroup: ViewGroup, itemViewType: Int): ViewHolder =
ViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.address_item, viewGroup, false))
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val address = addresses[position]
viewHolder.tv.setText(address.address)
}
override fun getItemCount(): Int = addresses.size
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tv = itemView.findViewById(R.id.ftv_name)
}
fun updateData(addresses: List<Address>) {
this.addresses = addresses
notifyDataSetChanged() // TODO: use ListAdapter if animations are needed
}
}
地址activity:-
class AddAddressActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.addaddress)
val editText: EditText = findViewById(R.id.longaddress)
val application = application as CustomApplication
val addressDao = application.addressDao
saveaddress.setOnClickListener {
val address = Address()
address.address = editText.getText().toString()
lifecycleScope.launch {
addressDao.addData(address)
finish()
}
}
}
}
您还可以参考 https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#6
我正在尝试从一个 activity 中获取 edittext 值并在 recycleview 中显示该文本并使用 room DB 存储....
基本上这个想法是在 activity 地址中添加地址,当点击时它会重定向到下一页,用户在提交时获取地址表单,它将获取地址并添加到上一个 activity回收视图。
这是我的房间密码:
table:---
@Entity(tableName = "address")
class Address {
@PrimaryKey
var id = 0
@ColumnInfo(name = "address")
var address: String? = null
}
数据库:-
@Database(entities = [Address::class], version = 1)
abstract class Database : RoomDatabase() {
abstract fun AddressDao(): AddressDao
}
地址道:
@Dao
interface AddressDao {
@Insert
suspend fun addData(address: Address)
@Query("select * from address")
fun getAddressesWithChanges() :LiveData<List<Address>>
@Query("SELECT EXISTS (SELECT 1 FROM address WHERE id=:id)")
suspend fun isAddressAdded(id: Int): Int
@Delete
suspend fun delete(address: Address)
}
AddAddressActivity activity:
class AddAddressActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.addaddress)
val editText: EditText = findViewById(R.id.longaddress)
val application = application as CustomApplication
saveaddress.setOnClickListener {
val address = Address()
address.address = editText.getText().toString()
lifecycleScope.launch {
application.addressDao.addData(address)
finish()
}
}
}
}
地址活动:-
class AddressActivity : AppCompatActivity() {
private val adapter = AddressAdapter()
private lateinit var data: LiveData<List<Address>>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.address)
addbutton.findViewById<View>(R.id.addbutton).setOnClickListener {
val intent = Intent(this, AddAddressActivity::class.java)
startActivity(intent)
}
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter
val application = application as CustomApplication
data = application.database.AddressDao().getAddressesWithChanges()
data.observe(this, Observer { words1 ->
// Update the cached copy of the words in the adapter.
words1?.let { adapter.updateData(it) }})
}
}
地址适配器activity:-
class AddressAdapter: RecyclerView.Adapter<AddressAdapter.ViewHolder>() {
private var addresses: List<Address> = Collections.emptyList()
override fun onCreateViewHolder(viewGroup: ViewGroup, itemViewType: Int):
ViewHolder =
ViewHolder(LayoutInflater.from(viewGroup.context)
.inflate(R.layout.address_item, viewGroup, false) )
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val fl: Address = addresses[position]
viewHolder.tv.setText(fl.address)
}
override fun getItemCount(): Int = addresses.size
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var tv: TextView
init {
tv = itemView.findViewById(R.id.ftv_name)
} }
fun updateData(addresses:
List<Address>) {
this.addresses = addresses
notifyDataSetChanged() // TODO: use ListAdapter if animations are needed
}
}
自定义应用程序:
class CustomApplication: Application() {
lateinit var database: Database
private set
lateinit var addressDao: AddressDao
private set
override fun onCreate() {
super.onCreate()
this.database = Room.databaseBuilder<Database>(
applicationContext,
Database::class.java, "database"
).build()
}
}
列表不显示 recycleview 返回地址时为空 activity
请帮忙..我是科特林和房间数据库的新手..
请将您的行替换为
val rv=findViewById<RecyclerView>(R.id.recyclerview)
您传递了错误的 ID addressrecyclerview
这就是它分配 null
abstract fun AddressDao(): AddressDao?
为什么这个可以为空?
val favoriteData: List<Table>?
为什么这个可以为空?
fun addData(favoriteList: Table?)
为什么这个可以为空?
fun delete(favoriteList: Table?)
为什么这个可以为空?
类型化可空性使您能够表示给定值 可以 为空的位置,在某种意义上 它不存在是有道理的.
以上两种情况都不代表这种情况。房间永远不会 return 你 null
而不是实际的 dao
.
Kotlin 的 null 安全性是允许您通过使其成为类型系统的一部分来消除潜在的错误情况,它不应该被用来创建诸如此类的“假”错误情况。如果您尝试将 null
添加到数据库而不是真实对象,则您的代码中可能存在错误,而 Kotlin 应该能够帮助您消除这种可能性。
至于你的实际问题,你在滥用 Room。
.allowMainThreadQueries()
是一种禁用错误检查的方法,但该错误是有原因的。您不应该在 UI 线程上读取数据,因为如果有足够的数据,您的应用将冻结 (ANR)。
代码应该如下所示:
@Entity(tableName = "address")
class Address {
@PrimaryKey
var id = 0
@ColumnInfo(name = "address")
var address: String? = null
}
数据库:-
@Database(entities = [Address::class], version = 1)
abstract class Database : RoomDatabase() {
abstract fun addressDao(): AddressDao
}
地址道:
@Dao
interface AddressDao {
@Insert
suspend fun addData(address: Address)
@Query("select * from address")
fun getAddressesWithChanges(): LiveData<List<Address>>
@Query("SELECT EXISTS (SELECT 1 FROM address WHERE id=:id)")
suspend fun isAddressAdded(id: Int): Int
@Delete
suspend fun delete(address: Address)
}
class CustomApplication: Application() {
lateinit var database: Database
private set
lateinit var addressDao: AddressDao
private set
override fun onCreate() {
super.onCreate()
this.database = Room.databaseBuilder<Database>(
applicationContext,
Database::class.java, "database"
).build()
addressDao = database.addressDao()
}
}
AndroidManifest.xml
<application android:name=".CustomApplication"
...
地址activity:
import androidx.lifecycle.observe
class AddressActivity : AppCompatActivity() {
private val adapter = AddressAdapter()
private lateinit var data: LiveData<List<Address>>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.address_activity)
findViewById<View>(R.id.addButton).setOnClickListener {
val intent = Intent(this, AddAddressActivity::class.java)
startActivity(intent)
}
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recyclerView.adapter = adapter
val application = application as CustomApplication
val addressDao = application.addressDao
data = addressDao.getAddressesWithChanges()
data.observe(this) {
adapter.updateData(it)
}
}
}
地址适配器:
class AddressAdapter: RecyclerView.Adapter<AddressAdapter.ViewHolder>() {
private var addresses: List<Table> = Collections.emptyList()
override fun onCreateViewHolder(viewGroup: ViewGroup, itemViewType: Int): ViewHolder =
ViewHolder(LayoutInflater.from(viewGroup.context).inflate(R.layout.address_item, viewGroup, false))
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val address = addresses[position]
viewHolder.tv.setText(address.address)
}
override fun getItemCount(): Int = addresses.size
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tv = itemView.findViewById(R.id.ftv_name)
}
fun updateData(addresses: List<Address>) {
this.addresses = addresses
notifyDataSetChanged() // TODO: use ListAdapter if animations are needed
}
}
地址activity:-
class AddAddressActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.addaddress)
val editText: EditText = findViewById(R.id.longaddress)
val application = application as CustomApplication
val addressDao = application.addressDao
saveaddress.setOnClickListener {
val address = Address()
address.address = editText.getText().toString()
lifecycleScope.launch {
addressDao.addData(address)
finish()
}
}
}
}
您还可以参考 https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#6