How to fetch edittext value from one activity to recyclerview of next activity?

杀马特。学长 韩版系。学妹 提交于 2020-08-17 12:00:50

问题


I'm trying to fetch edittext value from one activity and displaying that text in recycleview and storing using room DB....

Basically the idea is adding the address in activity address when clicks on plus it will redirect to next page where the user gets the address form onsubmit it will fetch address and add to previous activity recycleview.

here is my code for room:

table:---

@Entity(tableName = "address")
class Address {
@PrimaryKey
var id = 0

@ColumnInfo(name = "address")
var address: String? = null
}

Database:-

@Database(entities = [Address::class], version = 1)
 abstract class Database : RoomDatabase() {
abstract fun AddressDao(): 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()
           }
         }
       }
      }

AddressActivity:-

     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) }})


}
  }

AddressAdapter 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
}

}

CustomApplication:

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()

}

}

list is not displaying recycleview is empty when came back to address activity

please help ..I'm new to kotlin and room db..


回答1:


abstract fun AddressDao(): AddressDao?

Why is this nullable?

val favoriteData: List<Table>?

Why is this nullable?

fun addData(favoriteList: Table?)

Why is this nullable?

fun delete(favoriteList: Table?)

Why is this nullable?


Typed nullability is to enable you to represent where a given value can be null, in the sense that it makes sense for it to be absent.

Neither of the above cases represent such a scenario. Room will never return you null instead of an actual dao.

Kotlin's null safety is to allow you to eliminate potential error cases by making it part of the type system, it shouldn't be used to create "false" error cases such as these. If you're trying to add null to a database instead of a real object, you've probably got a bug in your code, and Kotlin should be able to help you eliminate that possibility.



As for your actual question, you're misusing Room.

.allowMainThreadQueries() is a way to disable an error check, but that error is there for a reason. You're not supposed to be reading the data on the UI thread, because with enough data, your app will freeze (ANR).

The code is supposed to look something like this:

@Entity(tableName = "address")
class Address {
    @PrimaryKey
    var id = 0

    @ColumnInfo(name = "address")
    var address: String? = null
}

Database:-

@Database(entities = [Address::class], version = 1)
abstract class Database : RoomDatabase() {
    abstract fun addressDao(): 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"
    ...

Address 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)
        }
    }
}

AddressAdapter:

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
    }
}

AddAddress 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()
            }
        }
    }
}

You can potentially also refer to https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#6




回答2:


Please replace your line with

  val  rv=findViewById<RecyclerView>(R.id.recyclerview)

You have passed the wrong id addressrecyclerview that's why it was assigning null



来源:https://stackoverflow.com/questions/63243505/how-to-fetch-edittext-value-from-one-activity-to-recyclerview-of-next-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!