【发布时间】:2021-06-01 11:23:18
【问题描述】:
我正在实施一个解决方案,其中 a 会看到联系人列表以及当前优先级 - 高、中和低。然后,用户可以使用对话框通过优先级更改状态,并且房间数据库已更新,但 UI 未按预期更新。只有当我重新创建活动时才会出现更改。我没有使用 ViewModel。
我需要在下面的代码中更改什么才能发生同样的情况?`
class CrmContacts : Fragment(), SearchView.OnQueryTextListener {
private lateinit var progressBar: ProgressDialog
var firebaseUser: FirebaseUser? = null //the used id of the user using the app
var managecontactsadapter: ManageContactsAdapter? = null
var reference: DatabaseReference? = null
var rvCRM: RecyclerView? = null
private val _contsCRMLiveData = MutableLiveData<ArrayList<Contact>>()
private val crmContactsLiveData: LiveData<ArrayList<Contact>> = _contsCRMLiveData
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_crm_contacts, container, false)
val srchView: SearchView = view.findViewById(R.id.search_user_contacts)
//Search
val searchPlateId: Int = srchView.context.resources
.getIdentifier("android:id/search_plate", null, null)
val searchPlate: View =
srchView.findViewById(searchPlateId)
searchPlate.setBackgroundColor(Color.TRANSPARENT)
val linearLayout1 = srchView.getChildAt(0) as LinearLayout
val linearLayout2 = linearLayout1.getChildAt(2) as LinearLayout
val linearLayout3 = linearLayout2.getChildAt(1) as LinearLayout
val autoComplete = linearLayout3.getChildAt(0) as AutoCompleteTextView
autoComplete.textSize = 14f
srchView.setOnQueryTextListener(this)
srchView.isFocusable = false
//Progress Bar
progressBar = ProgressDialog(activity)
progressBar.setCancelable(false)
progressBar.setMessage("Loading...")
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
rvCRM = view.findViewById(R.id.rvCRM)
rvCRM!!.setHasFixedSize(true)
rvCRM!!.layoutManager = LinearLayoutManager(activity)
displayContacts()
crmContactsLiveData.observe(viewLifecycleOwner, Observer {
rvCRM!!.adapter = managecontactsadapter
})
contact_fab.setOnClickListener {
val intent = Intent(activity, EditContact::class.java)
intent.putExtra("Edit", false)
this.startActivity(intent)
}
crmFilter.setOnClickListener {
val options = arrayOf<CharSequence>(
"Lead",
"Qualified",
"Proposal",
"Client",
"Invoiced",
"Unpaid",
"Clear Filter"
)
var salesTag = ""
val builder: AlertDialog.Builder = AlertDialog.Builder(activity)
builder.setTitle("Filter by?")
builder.setItems(options, DialogInterface.OnClickListener { _, which ->
if (which == 0) {
salesTag = "High"
filtercontactby(salesTag)
}
if (which == 1) {
salesTag = "Medium"
filtercontactby(salesTag)
}
if (which == 2) {
salesTag = "Low"
filtercontactby(salesTag)
}
})
builder.show()
}
}
private fun filtercontactby(salesTag: String) {
val mupdateList = ArrayList<Contact>()
val db = AppDatabase.getDatabase(requireContext())
val contact = db.contactsDao().getFilterContacts(salesTag)
if(contact.isEmpty())
{
val nocontacts = view?.findViewById<TextView>(R.id.noContacts)
nocontacts!!.visibility = View.VISIBLE
val notice = "uh oh! No Contacts have been tagged as " + "$salesTag!"
nocontacts.text = notice
}
mupdateList.clear()
for (i in contact) {
mupdateList.add(i)
}
managecontactsadapter = ManageContactsAdapter(requireContext(), mupdateList)
rvCRM!!.adapter = managecontactsadapter
}
private fun displayContacts() {
val mupdateList = ArrayList<Contact>()
val db = AppDatabase.getDatabase(requireContext())
val contact = db.contactsDao().getContacts()
val nocontacts = view?.findViewById<TextView>(R.id.noContacts)
if(contact.isEmpty())
{
nocontacts!!.visibility = View.VISIBLE
nocontacts.text = "uh oh! Please use the add clients/contacts button below!"
} else {
nocontacts!!.visibility = View.GONE
}
for (i in contact) {
mupdateList.add(i)
}
managecontactsadapter = ManageContactsAdapter(requireContext(), mupdateList)
rvCRM!!.adapter = managecontactsadapter
}
private fun searchForContacts(searchString: String) {
val mupdateList = ArrayList<Contact>()
val nocontacts = view?.findViewById<TextView>(R.id.noContacts)
val db = AppDatabase.getDatabase(requireContext())
val contact = db.contactsDao().getBySearch(searchString)
if(contact.isEmpty())
{
nocontacts!!.visibility = View.VISIBLE
nocontacts.text = "uh oh! Search is empty, try again?!"
} else {
nocontacts!!.visibility = View.GONE
}
mupdateList.clear()
for (i in contact) {
mupdateList.add(i)
}
managecontactsadapter = ManageContactsAdapter(requireContext(), mupdateList)
rvCRM!!.adapter = managecontactsadapter
}
//Search Contacts using SearchView
override fun onQueryTextSubmit(query: String?): Boolean {
managecontactsadapter!!.notifyDataSetChanged()
if (query != null) {
searchForContacts("%${query}%")
}
return true
}
override fun onQueryTextChange(keyWord: String?): Boolean {
if (keyWord != null) {
searchForContacts("%${keyWord}%")
}
return true
}
}
RoomDAO 返回一个对象列表 - LiveData
适配器类代码:
class ManageContactsAdapter(
mContext: Context,
mupdateList: ArrayList<Contact>
) :
RecyclerView.Adapter<ManageContactsAdapter.MyViewHolder?>() {
private val layoutInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
private val mContext: Context
private val mupdateList: List<Contact>
init {
this.mContext = mContext
this.mupdateList = mupdateList
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var contLabel: TextView? = null
var ContactName: TextView? = null
var ContactDetails: LinearLayout? = null
private var ContactProfileImage: ShapeableImageView
private var contFavEdit: ImageButton
private lateinit var salesLabel: CardView
init {
contLabel = itemView.findViewById(R.id.contLabel)
ContactProfileImage = itemView.findViewById(R.id.ContactProfileImage)
ContactName = itemView.findViewById(R.id.ContactName)
ContactDetails = itemView.findViewById(R.id.ContactDetails)
contFavEdit = itemView.findViewById(R.id.contFavEdit)
salesLabel = itemView.findViewById(R.id.salesLabel)
}
}
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): MyViewHolder {
return MyViewHolder(layoutInflater.inflate(R.layout.contacts_row, parent, false))
}
override fun getItemCount(): Int {
return mupdateList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val contact = mupdateList[position]
with(holder.itemView) {
ContactName.text = contact.name
if(contact.label != null){
contLabel.text = contact.label
if (contact.label == "High" || contact.label == "high"){
contLabel.setBackgroundColor(resources.getColor(R.color.ColorPurple))
} else if (contact.label == "Medium"){
contLabel.setBackgroundColor(resources.getColor(R.color.ColorBlueNote))
} else if (contact.label == "Low"){
contLabel.setBackgroundColor(resources.getColor(R.color.ColorMaroon))
}
}
if(contact.favorite == true){
holder.itemView.contFavEdit.setImageResource(R.drawable.ic_favsel)
} else {
holder.itemView.contFavEdit.setImageResource(R.drawable.ic_fav)
}
Picasso.get().load(contact.photoUri)
.placeholder(R.drawable.ic_baseline_whatshot_24).fit().centerCrop().into(
ContactProfileImage
)
ContactDetails.removeAllViews()
contact.numbers.forEach {
val detail = layoutInflater.inflate(
R.layout.row_contact_data,
ContactDetails,
false
)
detail.imgIcon.setImageResource(R.drawable.ic_local_phone_black_24dp)
detail.ContactData.text = it
ContactDetails.addView(detail)
}
contact.emails.forEach {
val detail = layoutInflater.inflate(
R.layout.row_contact_data,
ContactDetails,
false
)
detail.imgIcon.setImageResource(R.drawable.ic_mail_black_24dp)
detail.ContactData.text = it
ContactDetails.addView(detail)
}
}
holder.itemView.setOnClickListener {
val intent = Intent(mContext, ContactDetailsHome::class.java)
intent.putExtra("contact", contact as Serializable)
intent.putExtra("Edit", true)
mContext.startActivity(intent)
}
holder.itemView.contFavEdit.setOnClickListener {
if(contact.favorite == true){
contact.favorite = false
holder.itemView.contFavEdit.setImageResource(R.drawable.ic_fav)
updatefav(contact,false)
} else {
contact.favorite = true
holder.itemView.contFavEdit.setImageResource(R.drawable.ic_favsel)
updatefav(contact,true)
}
}
holder.itemView.setOnLongClickListener {
val options = arrayOf<CharSequence>(
"High",
"Medium",
"Low",
)
var salesTag = ""
val builder: AlertDialog.Builder = AlertDialog.Builder(mContext)
builder.setTitle("Contact is a?")
builder.setItems(options, DialogInterface.OnClickListener { _, which ->
if (which == 0) {
salesTag = "High"
updateDB(contact, salesTag)
}
if (which == 1) {
salesTag = "Medium"
updateDB(contact, salesTag)
}
if (which == 2) {
salesTag = "Low"
updateDB(contact, salesTag)
}
})
builder.show()
true
}
}
private fun updateDB(contactL: Contact, salesTag: String) {
val db = AppDatabase.getDatabase(mContext)
val ev = Contact(
contactL.id,
contactL.name,
contactL.lookupKey,
contactL.photoUri,
salesTag,
null,
contactL.favorite
)
db.contactsDao().updateContact(ev)
}
private fun updatefav(contactL: Contact, fav: Boolean) {
val db = AppDatabase.getDatabase(mContext)
db.contactsDao().updatefav(contactL.id, fav)
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}
【问题讨论】:
-
你能粘贴你的适配器类的代码吗?
-
嗨 che10,已添加相同的代码...
标签: android kotlin android-room android-livedata