My purpose: I am using Realm as a database and I am making a note app .I suppose note page can be saved and there is new info (a new column) in the mainActivity .However , after clicking the Save button in the action bar . These can't work but no error is made .
Какие файлы у меня есть? 1.RealmBasicConfig 2.AddSaveNotesActivity 3.NotesAdapter 4.notes_rv_layout (для предустановленного представления первой заметки) 5.MainActivity (первая страница в приложении)
Какие решения я хочу: почему не работает notes_rv_layout, и содержимое заметок не может быть синхронизировано. Это должна быть проблема mainActivity (адаптер !!. notifyDataSetChanged - не работает?)
Коды MainActivity:
class Main2Activity : AppCompatActivity() {
private lateinit var notesList:ArrayList<BasicInfoConfig>
private lateinit var realm: Realm
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
handleIntent(intent)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
//custom init views
realm = Realm.getDefaultInstance()
//set onclicklistener
addNotes.setOnClickListener{
startActivity(Intent(this,AddSaveNotesActivity::class.java))
finish()
notesRV.layoutManager = LinearLayoutManager(this)
getAllNotes()
}
}
private fun getAllNotes() { //yellow : used
notesList = ArrayList()
val results:RealmResults<BasicInfoConfig> = realm.where<BasicInfoConfig>(BasicInfoConfig::class.java).findAll()
notesRV.adapter = NotesAdapter(this,results)
notesRV.adapter!!.notifyDataSetChanged() // to update // bec I deleted the way it saved // none info can be read
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntent(intent)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main2, menu)
// Associate searchable configuration with the SearchView
// Associate searchable configuration with the SearchView
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
(menu.findItem(R.id.search).actionView as SearchView).apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
}
return true
}
override fun onBackPressed() {
super.onBackPressed()
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
private fun handleIntent(intent: Intent) {
if (Intent.ACTION_SEARCH == intent.action) {
val query = intent.getStringExtra(SearchManager.QUERY)
//use the query to search your data somehow
}
}
}
коды NotesAdapterView
class NotesAdapter (private val context : Context? , private val notesList : RealmResults<BasicInfoConfig>)
: RecyclerView.Adapter<RecyclerView.ViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.notes_rv_layout, parent,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return notesList.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.notes_title.text = notesList[position]!!.title
holder.itemView.notes_body.text = notesList[position]!!.description
}
class ViewHolder(v:View?):RecyclerView.ViewHolder(v!!) {
//
val Ada_title = itemView.findViewById<TextView>(R.id.notes_title)
val Ada_date = itemView.findViewById<TextView>(R.id.notes_timeStamp)
}
Коды RealmBasicConfig:
package com.example.notepad_free
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class BasicInfoConfig(
//connecting basic info to Realm
@PrimaryKey
var id:Int ?= null,
var title:String?=null,
var description:String?=null
):RealmObject()
Скажите, пожалуйста, если нужны дополнительные коды, буду благодарен, если вы поможете!