如何解决在Kotlin中实现ROOM数据库后出现错误
等级(模块):
def room_ver = "2.2.5"
dependencies {
implementation "androidx.room:room-ktx:" + room_ver
kapt "androidx.room:room-compiler:" + room_ver
androidTestImplementation "androidx.room:room-testing:" + room_ver
}
错误:
尝试添加:
gradle (app level)
dependencies {
classpath "com.android.tools.build:gradle:3.1.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
gradle:模块
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-kapt'
Dashboard.kt
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnScrollChangedListener
import android.view.inputmethod.EditorInfo
import android.widget.TextView.GONE
import android.widget.TextView.OnEditorActionListener
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.linearlayoutmanager
import com.ddnsoftech.mrenvois.R
import com.ddnsoftech.mrenvois.common.APIService.Const
import com.ddnsoftech.mrenvois.common.APIService.IntentKeys
import com.ddnsoftech.mrenvois.common.`interface`.BackButtonClickIntefrace
import com.ddnsoftech.mrenvois.common.helpers.UserSession
import com.ddnsoftech.mrenvois.common.util.SnackNotify
import com.ddnsoftech.mrenvois.common.util.hideKeyboard
import com.ddnsoftech.mrenvois.model.Restaurant
import com.ddnsoftech.mrenvois.view.Activities.RestaurantsDetails.ResturantsDetailsActivity
import com.ddnsoftech.mrenvois.view.Adapter.CategoryAdapter
import com.ddnsoftech.mrenvois.view.Adapter.ResturantAdapter
import com.ddnsoftech.mrenvois.view.BaseActivity.BaseActivity
import com.ddnsoftech.mrenvois.view.ServiceNotAvailable.ServiceNotAvailableActivity
import kotlinx.android.synthetic.main.filer_search_view.*
import kotlinx.android.synthetic.main.fragment_dashboard.*
// Todo: Rename parameter arguments,choose names that match
// the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
private const val ARG_ParaM1 = "param1"
private const val ARG_ParaM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [DashboardFragment.newInstance] factory method to
* create an instance of this fragment.
*/
private enum class DashboardListType {
FILTER_VIEW,HOME_VIEW
}
enum class FilterType {
popular,promo,location
}
class DashboardFragment : Fragment(),BackButtonClickIntefrace {
val viewmodel = DashboardFragmentVM()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
// param1 = it.getString(ARG_ParaM1)
// param2 = it.getString(ARG_ParaM2)
}
hideKeyboard()
}
lateinit var popularRestaurantAdapter: ResturantAdapter
lateinit var nearRestaurantAdapter: ResturantAdapter
lateinit var categoryAdapter: CategoryAdapter
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dashboard,container,false)
}
override fun onStart() {
super.onStart()
txtDeliveringTo.text = "${getString(R.string.delivering_to)} ${UserSession.shared.currentAddress?.address ?: ""}"
}
override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
super.onViewCreated(view,savedInstanceState)
initialSetup()
getHomePageData()
}
companion object {
@JvmStatic
fun newInstance() =
DashboardFragment().apply {
// arguments = Bundle().apply {
// putString(ARG_ParaM1,param1)
// putString(ARG_ParaM2,param2)
// }
}
}
@SuppressLint("ClickableViewAccessibility")
fun initialSetup() {
viewmodel.clearFilter()
popularRestaurantAdapter = ResturantAdapter(viewmodel.popular_restaurant,requireActivity())
nearRestaurantAdapter = ResturantAdapter(viewmodel.near_restaurants,requireActivity(),GridLayoutManager.HORIZONTAL)
categoryAdapter = CategoryAdapter(viewmodel.categories,requireContext())
popularRestaurantAdapter.onItemSelect = { restaurant ->
redirectToRestaurantDetails(restaurant)
}
nearRestaurantAdapter.onItemSelect = { restaurant ->
redirectToRestaurantDetails(restaurant)
}
categoryAdapter.onItemSelect = { index,value ->
viewmodel.restaurants.clear()
viewmodel.categories[index] = value
getHomePageData()
}
val manager1 = GridLayoutManager(requireActivity(),1,GridLayoutManager.HORIZONTAL,false)
recyclerViewMostPopular.layoutManager = manager1
val manager2 = GridLayoutManager(requireActivity(),false)
recyclerViewNearBy.layoutManager = manager2
val manager3 = linearlayoutmanager(requireActivity(),linearlayoutmanager.HORIZONTAL,false)
// GridLayoutManager(requireActivity(),4,false)
recyclerViewCategory.layoutManager = manager3
recyclerViewMostPopular.adapter = popularRestaurantAdapter
recyclerViewNearBy.adapter = nearRestaurantAdapter
recyclerViewCategory.adapter = categoryAdapter
scrollView.viewTreeObserver
.addOnScrollChangedListener(OnScrollChangedListener {
if (scrollView != null) {
val bottom: Int =
scrollView.getChildAt(scrollView.childCount - 1)
.height - scrollView.height - scrollView.scrollY
// if (scrollView.scrollY === 0) {
// //top detected
// SnackNotify.showMessage("Reached to top",relyMain,SnackNotify.MSGType.SUCCESS)
// }
if (scrollView.scrollY == 120) {
hideKeyboard()
}
if (bottom == 0) {
//bottom detected
if (viewmodel.isFilterapplied()) {
getHomePageData()
}
//SnackNotify.showMessage("Reached to Bottom",SnackNotify.MSGType.SUCCESS)
}
}
})
swipeRefreshLayout.setonRefreshListener {
viewmodel.restaurants.clear()
getHomePageData()
}
/// Ser view handler
// etSearchView.
setupSearchView()
setUpFilerView()
reloadList()
}
fun redirectToRestaurantDetails(restaurant: Restaurant) {
var intent = Intent(requireActivity(),ResturantsDetailsActivity::class.java)
intent.putExtra(IntentKeys.RESTAURANT,restaurant)
activity?.startActivity(intent)
}
fun setUpFilerView() {
lnrPopular.setonClickListener { view ->
onFilterButtonClick(view)
}
lnrPromos.setonClickListener { view ->
onFilterButtonClick(view)
}
lnrLocation.setonClickListener { view ->
onFilterButtonClick(view)
}
txtNearByViewAll.setonClickListener {
onFilterButtonClick(lnrLocation)
}
txtPopularViewAll.setonClickListener {
onFilterButtonClick(lnrPopular)
}
txtDeliveringTo.setonClickListener {
(requireActivity() as? BaseActivity)?.openSetLocation()
}
}
fun setupSearchView() {
etSearchView.setonTouchListener(View.OnTouchListener { _,event ->
val DRAWABLE_RIGHT = 2
if (event.action == MotionEvent.ACTION_UP) {
if (event.rawX >= etSearchView.right - etSearchView.compoundDrawables[DRAWABLE_RIGHT].bounds.width()
) {
viewmodel.restaurants.clear()
getHomePageData()
true
}
}
false
})
etSearchView.setonEditorActionListener(OnEditorActionListener { v,actionId,event ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
viewmodel.restaurants.clear()
getHomePageData()
true
} else false
})
// etSearchView.doOnTextChanged { text,start,before,count ->
// }
}
fun getHomePageData() {
hideKeyboard()
viewmodel.search = etSearchView.text?.toString()
swipeRefreshLayout.isRefreshing = true
viewmodel.getHomeData { success,errMsg ->
swipeRefreshLayout.isRefreshing = false
if (success) {
reloadList()
}else {
SnackNotify.showMessage(errMsg,SnackNotify.MSGType.ERROR)
reloadList()
}
}
}
fun getSearchData() {
}
fun reloadList() {
if (viewmodel.isFilterapplied()) {
if (nearRestaurantAdapter.scrollDirection != GridLayoutManager.VERTICAL) {
val handler = nearRestaurantAdapter.onItemSelect
nearRestaurantAdapter = ResturantAdapter(viewmodel.restaurants,GridLayoutManager.VERTICAL)
nearRestaurantAdapter.onItemSelect = handler
val manager2 = GridLayoutManager(requireActivity(),GridLayoutManager.VERTICAL,false)
recyclerViewNearBy.layoutManager = manager2
recyclerViewNearBy.adapter = nearRestaurantAdapter
}
lnrMostPopularContainer.visibility = View.GONE
lnrTopCategoryContainer.visibility =
if (viewmodel.categories.count() == 0) View.GONE else View.VISIBLE
lnrNearByContainer.visibility =
if (viewmodel.restaurants.count() == 0) View.GONE else View.VISIBLE
viewnorecordFound.visibility =
if (viewmodel.restaurants.count() != 0) View.GONE else View.VISIBLE
//popularRestaurantAdapter.updateList(viewmodel.popular_restaurant)
nearRestaurantAdapter.updateList(viewmodel.restaurants)
categoryAdapter.updateList(viewmodel.categories)
txtNearByTitle.text = getString(R.string.restaurants)
txtNearByViewAll.visibility = View.GONE
reloadFilterTab(true)
}else{
if (nearRestaurantAdapter.scrollDirection != GridLayoutManager.HORIZONTAL) {
nearRestaurantAdapter = ResturantAdapter(viewmodel.near_restaurants,GridLayoutManager.HORIZONTAL)
val manager2 = GridLayoutManager(requireActivity(),false)
recyclerViewNearBy.layoutManager = manager2
recyclerViewNearBy.adapter = nearRestaurantAdapter
}
txtNearByTitle.text = getString(R.string.near_by)
txtNearByViewAll.visibility = View.VISIBLE
viewnorecordFound.visibility = if (viewmodel.near_restaurants.count() == 0 && viewmodel.popular_restaurant.count() == 0) View.VISIBLE else View.GONE
lnrMostPopularContainer.visibility =
if (viewmodel.popular_restaurant.count() == 0) View.GONE else View.VISIBLE
lnrNearByContainer.visibility =
if (viewmodel.near_restaurants.count() == 0) View.GONE else View.VISIBLE
lnrTopCategoryContainer.visibility =
if (viewmodel.categories.count() == 0) View.GONE else View.VISIBLE
popularRestaurantAdapter.updateList(viewmodel.popular_restaurant)
nearRestaurantAdapter.updateList(viewmodel.near_restaurants)
categoryAdapter.updateList(viewmodel.categories)
reloadFilterTab(false)
}
}
fun reloadFilterTab(filterapplied:Boolean) {
if (filterapplied && viewmodel.filterType != null)
{
when (viewmodel.filterType!!) {
FilterType.popular -> {
lnrPopular.background = resources.getDrawable(R.drawable.filter_active_border)
lnrPromos.background = resources.getDrawable(R.drawable.rounded_border_tab)
lnrLocation.background = resources.getDrawable(R.drawable.rounded_border_tab)
}
FilterType.promo -> {
lnrPopular.background = resources.getDrawable(R.drawable.rounded_border_tab)
lnrPromos.background = resources.getDrawable(R.drawable.filter_active_border)
lnrLocation.background = resources.getDrawable(R.drawable.rounded_border_tab)
}
FilterType.location -> {
lnrPopular.background = resources.getDrawable(R.drawable.rounded_border_tab)
lnrPromos.background = resources.getDrawable(R.drawable.rounded_border_tab)
lnrLocation.background = resources.getDrawable(R.drawable.filter_active_border)
}
}
}else{
lnrPopular.background = resources.getDrawable(R.drawable.rounded_border_tab)
lnrPromos.background = resources.getDrawable(R.drawable.rounded_border_tab)
lnrLocation.background = resources.getDrawable(R.drawable.rounded_border_tab)
}
}
override fun onBackClicked(): Boolean{
// Todo("Not yet implemented")
if (viewmodel.isFilterapplied())
{
viewmodel.clearFilter()
getHomePageData()
return true
}
return false
}
fun onFilterButtonClick(v: View) {
when (v) {
lnrPopular -> {
viewmodel.filterType = if( viewmodel.filterType == FilterType.popular ) null else FilterType.popular
}
lnrPromos -> {
viewmodel.filterType = if( viewmodel.filterType == FilterType.promo ) null else FilterType.promo
}
lnrLocation -> {
viewmodel.filterType = if( viewmodel.filterType == FilterType.location ) null else FilterType.location
}
}
reloadFilterTab(viewmodel.filterType != null)
viewmodel.restaurants.clear()
getHomePageData()
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。