问题-我有两个活动。第一个活动用于以卡视图的形式显示所有数据。单击第二项活动可加载相应卡片视图的特定数据。用户可以从第二个活动中删除/更新已加载的数据,不仅如此,还可以创建新数据。
每次我尝试创建或更新“便笺”时,都必须重新启动整个应用程序才能看到回收者视图中的更改。但是,当我删除便笺时,由于已经看到更改,因此不必重启应用程序。 如何如何解决?
我尝试过的- 1-将读取数据并绑定到适配器的代码复制到onRestart()方法。没有变化。
2-在几乎所有可能适用的地方都称为 notifyDataSetChanged()。
主要活动
class MainActivity : AppCompatActivity() {
var dbHandler: PediaDatabase? = null
var adapter: PediaAdapter? = null
var layoutManager: RecyclerView.LayoutManager? = null
var list: ArrayList<UserNotes>? = ArrayList()
var listItems: ArrayList<UserNotes>? = ArrayList()
val PREFS_NAME: String = "MYPREFS"
var myPrefs: SharedPreferences? = null
var first_run: Boolean = true
val REQUEST_CODE: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showOneTimeMessage()
invalidateOptionsMenu()
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
window.navigationBarColor = Color.BLACK
}
override fun onStart() {
super.onStart()
dbHandler = PediaDatabase(this)
adapter = PediaAdapter(this, listItems!!)
layoutManager = LinearLayoutManager(this)
recyclerViewID.adapter = adapter
recyclerViewID.layoutManager = layoutManager
list = dbHandler!!.readAllNotes()
for(reader in list!!.iterator())
{
var note = UserNotes()
note.noteTitle = reader.noteTitle
note.noteText = reader.noteText
note.noteID = reader.noteID
note.noteDate = reader.noteDate
listItems!!.add(note)
}
adapter!!.notifyItemRangeChanged(0, adapter!!.getItemCount());
adapter!!.notifyDataSetChanged()
if(dbHandler!!.totalNotes() == 0) {
recyclerViewID.visibility = View.GONE
}
else{
recyclerViewID.visibility = View.VISIBLE
showWhenEmptyID.visibility = View.GONE
}
}
override fun onRestart() {
super.onRestart()
var tempData: SharedPreferences = getSharedPreferences(PREFS_NAME,0) //fetch data
if(tempData.contains("updated")) {
Log.d("UPDATEDLOL", "TRUE")
if (tempData.getBoolean("updated", false) == true)
{
adapter!!.notifyItemChanged(cardID)
adapter!!.notifyDataSetChanged()
recreate()
sharedPrefs = getSharedPreferences(PREFS_SHARE, 0)
val editor: SharedPreferences.Editor = (sharedPrefs as SharedPreferences).edit()
editor.putBoolean("updated", false).commit()
}
}
if(tempData.contains("deleted")) {
if (tempData.getBoolean("deleted", false) == true)
{
//adapPos takes adapter position from Adapter class
listItems!!.removeAt(cardID)
adapter!!.notifyItemRemoved(cardID)
adapter!!.notifyItemRangeChanged(cardID, listItems!!.size)
adapter!!.notifyDataSetChanged()
recyclerViewID.removeAllViews()
sharedPrefs = getSharedPreferences(PREFS_SHARE, 0)
var editor: SharedPreferences.Editor = (sharedPrefs as SharedPreferences).edit()
editor.putBoolean("deleted", false).commit() //TODO CHANGED HERE
}
}
if(tempData.contains("created")) {
if (tempData.getBoolean("created", false) == true)
{
adapter!!.notifyItemInserted(cardID)
adapter!!.notifyItemRangeInserted(cardID, listItems!!.size)
adapter!!.notifyDataSetChanged()
recyclerViewID.invalidate()
recyclerViewID.removeAllViews()
sharedPrefs = getSharedPreferences(PREFS_SHARE, 0)
val editor: SharedPreferences.Editor = (sharedPrefs as SharedPreferences).edit()
editor.putBoolean("created", false).commit()
}
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.top_menu, menu)
val item = menu!!.findItem(R.id.delete_note_menu)
item.setVisible(false)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if(item!!.itemId == R.id.add_note_menu){
var isNewNote = Intent(this, ReferenceActivity::class.java)
isNewNote.putExtra("isNewNote", true)
startActivityForResult(isNewNote, REQUEST_CODE)
}
if(item!!.itemId == R.id.delete_note_menu)
{
Toast.makeText(this,"DELETED", Toast.LENGTH_SHORT).show()
}
return super.onOptionsItemSelected(item)
}
override fun startActivityForResult(intent: Intent?, requestCode: Int) {
super.startActivityForResult(intent, requestCode)
}
private fun showOneTimeMessage()
{
var data: SharedPreferences = getSharedPreferences(PREFS_NAME, 0)
if(data.contains("isShown"))
{
first_run = data.getBoolean("isShown", true)
}
Log.d("FIRST_RUN", first_run.toString())
if(first_run) {
val oneTimeMsg = SweetAlertDialog(this)
oneTimeMsg.setTitleText("Hey there!")
oneTimeMsg.setContentText("Thank you for downloading! Please don`t forget to rate our app :)").show()
oneTimeMsg.setConfirmClickListener(object : SweetAlertDialog.OnSweetClickListener {
override fun onClick(sweetAlertDialog: SweetAlertDialog?) {
oneTimeMsg.dismissWithAnimation()
}
}).show()
myPrefs = getSharedPreferences(PREFS_NAME, 0)
var editor: SharedPreferences.Editor = (myPrefs as SharedPreferences).edit()
editor.putBoolean("isShown", false)
editor.commit()
}
}
第二活动
class ReferenceActivity : AppCompatActivity() {
var dbHandler: PediaDatabase? = null
var note = UserNotes()
var existingNote = UserNotes()
var noteExisted: Boolean = false
var cardAdapterPos: Int? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_reference)
getSupportActionBar()!!.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar()!!.setCustomView(R.layout.custom_toolbar);
val dateTxtView = findViewById<View>(resources.getIdentifier("action_bar_title", "id", packageName)) as TextView
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
window.navigationBarColor = Color.BLACK
dbHandler = PediaDatabase(this)
var data = intent
var isNewNote = intent
if(isNewNote != null)
if(isNewNote.extras.getBoolean("isNewNote") != true)
{
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
if(data != null)
{
noteExisted = true
this.cardAdapterPos = data.extras.getInt("cardPosition")
cardID = data.extras.getInt("cardID")
existingNote = dbHandler!!.readNote(cardID)
refTitleID.setText(existingNote.noteTitle, TextView.BufferType.EDITABLE)
refTextID.setText(existingNote.noteText, TextView.BufferType.EDITABLE)
dateTxtView.text = existingNote.noteDate.toString()
}
}else{
dateTxtView.text = "New note"
}
}
override fun onStop() {
super.onStop()
var title: String = refTitleID.text.toString().trim()
var text: String = refTextID.text.toString().trim()
if(existingNote.noteText == text && existingNote.noteTitle == title)
finish()
if(noteExisted)
{
if(TextUtils.isEmpty(title))
title = "No title"
existingNote.noteTitle = title
existingNote.noteText = text
// existingNote.noteDate = System.currentTimeMillis().toString() //TODO TRY THIS
dbHandler!!.updateNote(existingNote)
//TODO BACK TO MAIN DATA HERE!!!!
sharedPrefs = getSharedPreferences(PREFS_SHARE, 0)
val share_editor: SharedPreferences.Editor = (sharedPrefs as SharedPreferences).edit()
share_editor.putBoolean("updated", true).commit()
finish()
}
else
{
if(TextUtils.isEmpty(title) && TextUtils.isEmpty(text))
{
finish()
}
else
{
if(TextUtils.isEmpty(title))
title = "No title"
note.noteTitle = title
note.noteText = text
dbHandler!!.createNote(note)
sharedPrefs = getSharedPreferences(PREFS_SHARE,0)
val editor: SharedPreferences.Editor = (sharedPrefs as SharedPreferences).edit()
editor.putBoolean("created",true).commit()
finish()
}
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.top_menu, menu)
val addItem: MenuItem = menu!!.findItem(R.id.add_note_menu)
val delItem:MenuItem = menu.findItem(R.id.delete_note_menu)
addItem.setVisible(false)
delItem.setVisible(false)
if(noteExisted)
delItem.setVisible(true)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if(item!!.itemId == R.id.delete_note_menu)
{
val dialogMsg = SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
dialogMsg.setTitleText("Are you sure?")
dialogMsg.setContentText("You won`t be able to recover this note!")
dialogMsg.setConfirmText("Yes,delete it!")
dialogMsg.setConfirmClickListener(object: SweetAlertDialog.OnSweetClickListener {
override fun onClick(sweetAlertDialog: SweetAlertDialog?) {
dialogMsg.dismissWithAnimation()
dbHandler!!.deleteNote(cardID)
sharedPrefs = getSharedPreferences(PREFS_SHARE,0)
//TODO HANDLES BACKGROUND BROADCASTING
val editor: SharedPreferences.Editor = (sharedPrefs as SharedPreferences).edit()
editor.putBoolean("deleted", true).commit()
var successMsg = SweetAlertDialog(sweetAlertDialog!!.context, SweetAlertDialog.SUCCESS_TYPE)
successMsg.setTitleText("Note deleted!")
successMsg.setContentText("So long,note").show()
successMsg.setCancelable(false)
//TODO Disable 'OK' button on successMsg dialogbox
Handler().postDelayed({
successMsg.dismissWithAnimation()
finish()
}, 1200)
}
}).show()
}
return super.onOptionsItemSelected(item)
}
}