Prompt Detail:
Sure, here's an example implementation of a RecyclerView to display messages retrieved from a Room database:
1. Create two XML files for left and right message items. For example, you can create `left_msg_item.xml` and `right_msg_item.xml`.
2. Create two ViewHolder classes, one for each message type:
```kotlin
class LeftMessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// Bind left message data to the views in left_msg_item.xml
}
class RightMessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// Bind right message data to the views in right_msg_item.xml
}
```
3. Create a RecyclerView adapter that extends `RecyclerView.Adapter`:
```kotlin
class MessageAdapter(private val messageList: List<Message>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun getItemViewType(position: Int): Int {
return messageList[position].msg_type
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == 0) {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.left_msg_item, parent, false)
LeftMessageViewHolder(view)
} else {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.right_msg_item, parent, false)
RightMessageViewHolder(view)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val message = messageList[position]
if (holder.itemViewType == 0) {
(holder as LeftMessageViewHolder).bind(message)
} else {
(holder as RightMessageViewHolder).bind(message)
}
}
override fun getItemCount() = messageList.size
}
```
4. Create a ViewModel to interact with the Room database:
```kotlin
class MessageViewModel(application: Application) : AndroidViewModel(application) {
private val repository: MessageRepository
val allMessages: LiveData<List<Message>>
init {
val messageDao = MessageDatabase.getDatabase(application).messageDao()
repository = MessageRepository(messageDao)
allMessages = repository.allMessages
}
fun insert(message: Message) = viewModelScope.launch(Dispatchers.IO) {
repository.insert(message)
}
}
```
5. Create a Repository to interact with the Room database:
```kotlin
class MessageRepository(private val messageDao: MessageDao) {
val allMessages: LiveData<List<Message>> = messageDao.getAllMessages()
suspend fun insert(message: Message) {
messageDao.insert(message)
}
}
```
6. Create a DAO to define Room database operations:
```kotlin
@Dao
interface MessageDao {
@Query("SELECT * FROM message ORDER BY chat_id DESC")
fun getAllMessages(): LiveData<List<Message>>
@Insert
suspend fun insert(message: Message)
}
```
7. Finally, in your activity or fragment, you can set up the RecyclerView like this:
```kotlin
class MainActivity : AppCompatActivity() {
private lateinit var messageViewModel: MessageViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
val sendButton = findViewById<Button>(R.id.chatSend)
val inputEditText = findViewById<EditText>(R.id.chatInput)
recyclerView.adapter = MessageAdapter(emptyList())
messageViewModel = ViewModelProvider(this).get(MessageViewModel::class.java)
messageViewModel.allMessages.observe(this, Observer { messages ->
recyclerView.adapter = MessageAdapter(messages)
recyclerView.scrollToPosition(messages.size - 1)
})
sendButton.setOnClickListener {
val inputMessage = inputEditText.text.toString().trim()
if (inputMessage.isNotEmpty())
Add a comment