只有最后一个元素添加到Android的RecyclerView(Kotlin)

我将7个物品的清单传递给recyclerview,它只是绑定最后一个。 通过使用日志我cal看到getItemCount()返回7,但onBindViewHolder只被调用一次。 我知道(再次通过日志记录),我传递给适配器的列表是完整的列表。

你能帮我找出为什么只显示最后一个元素,而不是整个列表。 谢谢。

活动代码:

class HabitActivity : AppCompatActivity() { var adapter: ListHabitsAdapter? = null private var habitList: ArrayList? = null private var habitListItems: ArrayList? = null private var layoutManager: RecyclerView.LayoutManager? = null var dbHandler:HabitDataAdapter? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_habit) // Initialize the Class global variables //val mContext = applicationContext dbHandler = HabitDataAdapter(this) habitList = ArrayList() habitListItems = ArrayList() layoutManager = LinearLayoutManager(this) adapter = ListHabitsAdapter(habitListItems!!,this) // Set up the RecyclerView habitListRecyclerViewID.layoutManager = layoutManager habitListRecyclerViewID.adapter = adapter // Load the chores habitList = dbHandler!!.readHabits() habitList!!.reverse() for (h in habitList!!.iterator()){ var habit = Habit() habit.habitName = h.habitName habit.habitActive = h.habitActive habit.habitID = h.habitID habitListItems!!.add(habit) ///adapter!!.notifyDataSetChanged() } adapter!!.notifyDataSetChanged() printList(habitListItems!!) } //TODO - Remove debug before production fun printList(list:ArrayList){ for (i in 0..list.size - 1){ Log.d(DEBUG, "${list[i].habitName} has ID ${list[i].habitID}") } } fun addHabit(view: View) { var popUpResult:String?=null var db = HabitDataAdapter(applicationContext) // pop up a dialog var promptView:View = layoutInflater.inflate(R.layout.habit_pop_up, null) var builder:AlertDialog.Builder =AlertDialog.Builder(this) builder.setView(promptView) builder.setTitle("Create a Habit") builder.setNeutralButton("CANCEL", DialogInterface.OnClickListener({ dialog, which -> Toast.makeText(this,"Boo",Toast.LENGTH_LONG).show() })) builder.setPositiveButton("OK", DialogInterface.OnClickListener(){ dialog, which -> var habitField = promptView.editText.text.toString() db.createHabit(habitField) Toast.makeText(this,habitField,Toast.LENGTH_LONG).show() }) builder.show() } fun deleteHabit(view: View) { } fun loadMain(view: View){ var intent = Intent(this, MainActivity::class.java) startActivity(intent) } 

这里是ListHabitAdapter:

 class ListHabitsAdapter(val list:ArrayList, context: Context): RecyclerView.Adapter() { val mContext = context override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { val view = LayoutInflater.from(mContext) .inflate(R.layout.habit_list_file, parent, false) return ViewHolder(view, mContext, list) } override fun onBindViewHolder(holder: ViewHolder?, position: Int) { Log.d(DEBUG,"Starting to Bind ${list[position]}") holder?.bindViews(list[position]) } override fun getItemCount(): Int { Log.d(DEBUG,"getItemCount = ${list.size}") return list.size } inner class ViewHolder(itemView:View, context: Context, list: ArrayList): RecyclerView.ViewHolder(itemView), View.OnClickListener { var mContext = context var mList = list var habitName = itemView.findViewById(R.id.habitNameID) var deleteButton = itemView.findViewById(R.id.habitDeleteButton) var doneButton = itemView.findViewById(R.id.habitActiveButton) fun bindViews(habit: Habit){ habitName.text = habit.habitName deleteButton.setOnClickListener(this) doneButton.setOnClickListener(this) } override fun onClick(v: View?) { var mPosition:Int = adapterPosition var habit = mList[mPosition] Log.d(DEBUG, habitName.text.toString()) when(v!!.id){ deleteButton.id -> Log.d(DEBUG,"delete button clicked") doneButton!!.id -> Log.d(DEBUG,"done button clicked") } } } } 

布局的XML:

         

根据我上面的评论只是让你的ConstraintLayout高度android:layout_height="wrap_content"