這個問題在這里已經有了答案: 無法反序列化物件。類未定義無引數建構式。如果您使用 ProGuard,請確保未剝離這些建構式 2 個答案 3 小時前關閉。
我試圖將資料從集合中獲取到 ArrayList 中,以便我可以在 RecycleView 上進一步顯示它。
資料類如下:
data class Proyecto(
val alcances :String,
val alumnos :HashMap<String, Any>,
val areasConoc :String,
val asignaturas :String,
val cliente :String,
val colaboradores :String,
val compDes :String,
val compPrev :String,
val coordinador: String,
val departamentos :String,
val impacto :String,
val institucion :String,
val justificacion :String,
val limityrest :String,
val materiaEje :String,
val periodo : HashMap<String, Any>,
val plan :String,
val planteamiento :String,
val profeResp :String,
val tipoejec :String,
val tituloproyecto :String
)
我只需要將欄位tituloproyecto、coordinador 和cliente 放入recyclerview,所以這是我的recyclerview 配接器:
類 MyAdapter(私有 val projectList:ArrayList):RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_item,parent,false)
return MyViewHolder(itemView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val proyecto : Proyecto = projectList[position]
holder.tituloproyecto.text = proyecto.tituloproyecto
holder.coordinador.text = proyecto.coordinador
holder.cliente.text = proyecto.cliente
}
override fun getItemCount(): Int {
return projectList.size
}
public class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val tituloproyecto : TextView = itemView.findViewById(R.id.tv_titulo_rv)
val coordinador: TextView = itemView.findViewById(R.id.tv_coordinador_rv)
val cliente: TextView = itemView.findViewById(R.id.tv_cliente_rv)
}
}
這是將顯示 recyclerview 的 Activity 代碼: class ProyectosAlumnoActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var projectArrayList: ArrayList<Proyecto>
private lateinit var myAdapter: MyAdapter
private lateinit var db : FirebaseFirestore
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_proyectos_alumno)
supportActionBar?.setTitle("Proyectos de Lorenzo")
recyclerView = findViewById(R.id.recyclerView_al)
recyclerView.layoutManager = LinearLayoutManager (this)
recyclerView.setHasFixedSize(true)
projectArrayList = arrayListOf()
myAdapter = MyAdapter(projectArrayList)
recyclerView.adapter = myAdapter
EventChangeListener()
}
private fun EventChangeListener() {
db = FirebaseFirestore.getInstance()
// db.collection("Proyecto").get().addOnSuccessListener { }
db.collection("Proyecto")
.addSnapshotListener(object : EventListener<QuerySnapshot>{
override fun onEvent(value: QuerySnapshot?, error: FirebaseFirestoreException?) {
if (error != null)
{
Log.e("Firestore error", error.message.toString())
return
}
for (dc: DocumentChange in value?.documentChanges!!){
if (dc.type == DocumentChange.Type.ADDED){
projectArrayList.add(dc.document.toObject(Proyecto::class.java))
}
}
myAdapter.notifyDataSetChanged()
}
})
}
我收到以下錯誤訊息:
2022-05-29 18:30:38.363 6252-6252/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firebasecrudapplication, PID: 6252
java.lang.RuntimeException: Could not deserialize object. Class com.example.firebasecrudapplication.models.Proyecto does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(CustomClassMapper.java:568)
at com.google.firebase.firestore.util.CustomClassMapper.access$200(CustomClassMapper.java:57)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:754)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:746)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:547)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:258)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:103)
at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:183)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:116)
at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:161)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:97)
at com.example.firebasecrudapplication.ProyectosAlumnoActivity$EvenChangeListener$1.onEvent(ProyectosAlumnoActivity.kt:55)
at com.example.firebasecrudapplication.ProyectosAlumnoActivity$EvenChangeListener$1.onEvent(ProyectosAlumnoActivity.kt:45)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2$com-google-firebase-firestore-Query(Query.java:1205)
at com.google.firebase.firestore.Query$$ExternalSyntheticLambda2.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$com-google-firebase-firestore-core-AsyncEventListener(AsyncEventListener.java:42)
at com.google.firebase.firestore.core.AsyncEventListener$$ExternalSyntheticLambda0.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
讓我相信這里的問題是 hashmap 欄位沒有被正確決議到 arraylist 中。
關于我能做些什么來解決這個問題的任何想法?
uj5u.com熱心網友回復:
您需要為模型欄位提供默認值。如果檔案快照中缺少特定欄位,Firestore 會將該默認值分配給該欄位。
data class Proyecto(
val alcances: String = "",
val alumnos: HashMap<String, Any> = hashMapOf(),
val areasConoc: String = "",
val asignaturas: String = "",
val cliente: String = "",
val colaboradores: String = "",
val compDes: String = "",
val compPrev: String = "",
val coordinador: String = "",
val departamentos: String = "",
val impacto: String = "",
val institucion: String = "",
val justificacion: String = "",
val limityrest: String = "",
val materiaEje: String = "",
val periodo: HashMap<String, Any> = hashMapOf(),
val plan: String = "",
val planteamiento: String = "",
val profeResp: String = "",
val tipoejec: String = "",
val tituloproyecto: String = ""
)
此外,建議使用 Kotlin 的 immutableMap
而不是 Java 的HashMap
.
uj5u.com熱心網友回復:
我認為您缺少一個 ; 兄弟,希望能幫到你:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/482792.html
標籤:安卓 火力基地 科特林 谷歌云火库 android-recyclerview