kotlin 我怎样才能得到这种数据的实时数据库作出正确的mojo类?

jckbn6z7  于 2023-01-26  发布在  Kotlin
关注(0)|答案(1)|浏览(64)

第一个c0d1x第一个c1d1x第一个c2d1x
这里我想提取匹配的Mojo类,但分别有不正确的数据类型来检索数组结构的数组。我无法检索所有数据,由于“匹配”匹配模型数据无法检索,它给我以下错误。

data class MatchModel(

@Expose
@PropertyName("matchDate")
var matchDate: String? = "",

@Expose
@PropertyName("matchId")
var matchId: String? = "",

@Expose
@PropertyName("matchNo")
var matchNo: String? = "",

@Expose
@PropertyName("matchPriority")
var matchPriority: String? = "",

@Expose
@PropertyName("matchResult")
var matchResult: String? = "",

@Expose
@PropertyName("matchStatus")
var matchStatus: String? = "",

@Expose
@PropertyName("matchType")
var matchType: String? = "",

@Expose
@PropertyName("matchTime")
var matchTime: String? = "",

@Expose
@PropertyName("seriesId")
var seriesId: String? = "",

@Expose
@PropertyName("team1Code")
var team1Code: String? = "",

@Expose
@PropertyName("team1FullName")
var team1FullName: String? = "",

@Expose
@PropertyName("team1Over")
var team1Over: String? = "",

@Expose
@PropertyName("team1Score")
var team1Score: String? = "",

@Expose
@PropertyName("team2Code")
var team2Code: String? = "",

@Expose
@PropertyName("team2FullName")
var team2FullName: String? = "",

@Expose
@PropertyName("team2Over")
var team2Over: String? = "",

@Expose
@PropertyName("team2Score")
var team2Score: String? = "",

@Expose
@PropertyName("venue")
var venue: String? = "",

): Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString())

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(matchDate)
        parcel.writeString(matchId)
        parcel.writeString(matchNo)
        parcel.writeString(matchPriority)
        parcel.writeString(matchResult)
        parcel.writeString(matchStatus)
        parcel.writeString(matchType)
        parcel.writeString(matchTime)
        parcel.writeString(seriesId)
        parcel.writeString(team1Code)
        parcel.writeString(team1FullName)
        parcel.writeString(team1Over)
        parcel.writeString(team1Score)
        parcel.writeString(team2Code)
        parcel.writeString(team2FullName)
        parcel.writeString(team2Over)
        parcel.writeString(team2Score)
        parcel.writeString(venue)
    }
    
    override fun describeContents(): Int {
        return 0
    }
    
    companion object CREATOR : Parcelable.Creator<MatchModel> {
        override fun createFromParcel(parcel: Parcel): MatchModel {
            return MatchModel(parcel)
        }
    
        override fun newArray(size: Int): Array<MatchModel?> {
            return arrayOfNulls(size)
        }
    }

}

import android.os.Parcel
import android.os.Parcelable
import com.google.firebase.database.PropertyName
import com.google.gson.annotations.Expose

data class SeriesModel(
@Expose
@PropertyName("match")
var match: ArrayList<MatchModel> = ArrayList(),

    @Expose
    @PropertyName("seriesFromDate")
    var seriesFromDate: String? = null,
    
    @Expose
    @PropertyName("seriesId")
    var seriesId: String? = null,
    
    @Expose
    @PropertyName("seriesName")
    var seriesName: String? = null,
    
    @Expose
    @PropertyName("seriesPriority")
    var seriesPriority: String? = null,
    
    @Expose
    @PropertyName("seriesToDate")
    var seriesToDate: String? = null,
    
    @Expose
    @PropertyName("totalMatches")
    var totalMatches: String? = null,
    
    @Expose
    @PropertyName("year")
    var year: String? = null,
    
    ) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.createTypedArrayList(MatchModel) as ArrayList<MatchModel>,
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString())
    
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeTypedList(match)
        parcel.writeString(seriesFromDate)
        parcel.writeString(seriesId)
        parcel.writeString(seriesName)
        parcel.writeString(seriesPriority)
        parcel.writeString(seriesToDate)
        parcel.writeString(totalMatches)
        parcel.writeString(year)
    }
    
    override fun describeContents(): Int {
        return 0
    }
    
    companion object CREATOR : Parcelable.Creator<SeriesModel> {
    
        override fun createFromParcel(parcel: Parcel): SeriesModel {
            return SeriesModel(parcel)
        }
    
        override fun newArray(size: Int): Array<SeriesModel?> {
            return arrayOfNulls(size)
        }
    
    }

}

class MainActivity : AppCompatActivity() {

    private lateinit var seriesListAdapter: SeriesListAdapter
    private lateinit var binding: ActivityMainBinding
    var fabVisible = false
    
    private var seriesModelArrayList:ArrayList<SeriesModel> = ArrayList()
    private lateinit var databaseReference: DatabaseReference
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        binding = DataBindingUtil.setContentView(this@MainActivity,R.layout.activity_main)
        setContentView(binding.root)
    
        setSupportActionBar(binding.toolbar)
    
    
        databaseReference = FirebaseDatabase.getInstance().getReference("series")
    
        callSeriesList()
        setViews()
    
    
    }
    
    private fun callSeriesList() {
    
        databaseReference.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
    
                val arrayListSeries: ArrayList<SeriesModel?> = ArrayList()
    
                for (seriesSnapShot in snapshot.children) {
                    val series: SeriesModel? = seriesSnapShot.getValue(SeriesModel::class.java) // error throw on this line while series fatch
                    arrayListSeries.add(series)
                }
    
                if(arrayListSeries.size >0){
                    seriesModelArrayList.clear()
                    seriesModelArrayList = arrayListSeries.filterNotNull() as ArrayList<SeriesModel>
                }
    
                println("Test Array Series on MainActivity Size--> " + arrayListSeries.size.toString())
                println("Test Array  series on MainActivity Size--> " + seriesModelArrayList.size.toString())
    
                setRecyclerView()
    
                if(seriesModelArrayList.size >0){
                    setRecyclerViewVisibility(true)
                }else{
                    setRecyclerViewVisibility(false)
                }
    
            }
    
            override fun onCancelled(error: DatabaseError) {
    
                Log.d("Test Error", error.message)
            }
    
        })
    
    
    }
    
    private fun setViews() {
    
        fabVisible = false
    
        binding.fabAdd.setOnClickListener { view ->
            if (!fabVisible) {
               fabButtonVisibility(true)
            }else{
              fabButtonVisibility(false)
            }
    
        }
    
        binding.linearAddSeries.setOnClickListener {
            gotoAddSeriesActivity()
        }
    
        binding.fabAddSeries.setOnClickListener{
            gotoAddSeriesActivity()
        }
    
        binding.linearAddMatch.setOnClickListener {
            gotoAddMatchActivity()
        }
    
        binding.fabAddMatch.setOnClickListener{
            gotoAddMatchActivity()
        }
    
    }
    
    private fun fabButtonVisibility(b: Boolean) {
        if(b){
            binding.linearAddMatch.visibility = View.GONE
            binding.linearAddSeries.visibility = View.VISIBLE
    
            binding.fabAdd.setImageDrawable(ContextCompat.getDrawable(this@MainActivity,R.drawable.ic_cancel))
            fabVisible = true
        }else{
            binding.linearAddMatch.visibility = View.GONE
            binding.linearAddSeries.visibility = View.GONE
            binding.fabAdd.setImageDrawable(ContextCompat.getDrawable(this@MainActivity,R.drawable.ic_add))
            fabVisible=false
        }
    }
    
    private fun gotoAddSeriesActivity() {
        fabButtonVisibility(false)
    
        val mIntentAddSeries: Intent =  Intent(this@MainActivity,AddSeriesActivity::class.java)
        startActivity(mIntentAddSeries)
    }
    
    private fun gotoAddMatchActivity() {
        fabButtonVisibility(false)
    
        val mIntentAddMatch: Intent =  Intent(this@MainActivity,AddMatchActivity::class.java)
        startActivity(mIntentAddMatch)
    }
    
    
    private fun setRecyclerView() {
        binding.includeMain.recyclerViewSeries.layoutManager = LinearLayoutManagerWrapper(this@MainActivity, LinearLayoutManager.VERTICAL, false)
    
       /* val firebaseRecyclerOptions : FirebaseRecyclerOptions<SeriesModel> = FirebaseRecyclerOptions.Builder<SeriesModel>().setQuery(
            FirebaseDatabase.getInstance().reference.child("series"),SeriesModel::class.java).setLifecycleOwner(this).build()*/
    
        seriesListAdapter = SeriesListAdapter(this@MainActivity, seriesModelArrayList)
    
        seriesListAdapter.setOnItemClickListener(object : SeriesListAdapter.OnItemClickListener{
            override fun onItemClick(position: Int,seriesModel: SeriesModel) {
    
                val mIntent = Intent(this@MainActivity, MatchListActivity::class.java)
                mIntent.putExtra("seriesModel",seriesModel)
                startActivity(mIntent)
            }
    
            override fun onItemEditClickListener(position: Int, seriesModel: SeriesModel) {
                val mIntent = Intent(this@MainActivity, AddSeriesActivity::class.java)
                mIntent.putExtra("seriesModel",seriesModel)
                mIntent.putExtra("isEdit",true)
                startActivity(mIntent)
            }
    
            override fun onItemDeleteClickListener(position: Int, seriesModel: SeriesModel) {
    
                val builder = AlertDialog.Builder(this@MainActivity)
    
                // Set a title for alert dialog
                builder.setTitle("Confirm Delete this series...")
    
                // Show a message on alert dialog
                builder.setMessage("This  series and their Match will be Deleted. Are you sure to delete Series?")
    
                // Set the positive button
                builder.setPositiveButton(
                    "Delete"
                ) { dialog: DialogInterface, _: Int ->
    
                    // here code to delete
                    dialog.dismiss()
                }
    
                // Set the negative button
                builder.setNegativeButton(
                    "Cancel"
                ) { dialog: DialogInterface, _: Int -> dialog.dismiss() }
    
                // Create the alert dialog
                val dialog = builder.create()
    
                // Finally, display the alert dialog
                dialog.setCanceledOnTouchOutside(false)
                dialog.show()
    
                // Get the alert dialog buttons reference
                val positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
                val negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
                positiveButton.setTextColor(
                    resources.getColor(R.color.colorSecondaryDark, theme)
                )
                positiveButton.setBackgroundColor(
                    resources.getColor(R.color.colorWhite, theme)
                )
                negativeButton.setTextColor(
                    resources.getColor(R.color.colorPrimaryDark, theme)
                )
                negativeButton.setBackgroundColor(
                    resources.getColor(R.color.colorWhite, theme)
                )
    
            }
    
        })
    
        //mBinding.recyclerViewSeriesList.itemAnimator = DefaultItemAnimator()
        binding.includeMain.recyclerViewSeries.adapter = seriesListAdapter
    
    
    }
    
    private fun setRecyclerViewVisibility(b:Boolean){
        if(b){
            binding.includeMain.txtNoDataSeriesList.visibility= View.GONE
            binding.includeMain.recyclerViewSeries.visibility= View.VISIBLE
    
        }else{
    
            binding.includeMain.recyclerViewSeries.visibility= View.GONE
            binding.includeMain.txtNoDataSeriesList.visibility= View.VISIBLE
        }
    }
    
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }
    
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    } 
}
tzdcorbm

tzdcorbm1#

我认为问题就在这里:

data class SeriesModel(
@Expose
@PropertyName("match")
var match: ArrayList<MatchModel> = ArrayList(),
  ...

由于您使用的是ArrayList,因此SDK期望在数据库中找到MatchModel对象的数组
但是如果我们看一下数据库的第二个屏幕截图,你实际上有每个MatchModel在一个按键下,这意味着你代码中正确的Map应该是:

data class SeriesModel(
@Expose
@PropertyName("match")
var match: Map<String, MatchModel> = HashMap(),
  ...

相关问题