@NovichokIvanovich
начинающий программист

Как добавить данные в ListFragment kotlin?

Как добавить данные в ListFragment kotlin?
Код с ошибкой в функции setAdapter

class WorkoutListFragment : ListFragment() {
    private var columnCount = 1
    private var listener: OnListFragmentInteractionListener? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_workout_list, container, false)

        // Set the adapter
        if (view is RecyclerView) {
            with(view) {
                layoutManager = when {
                    columnCount <= 1 -> LinearLayoutManager(context)
                    else -> GridLayoutManager(context, columnCount)
                }
                adapter = MyworkoutRecyclerViewAdapter(DummyContent.ITEMS, listener)
            }
        }
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        var workouts:Array<Workout> = arrayOf(
            Workout("The Limb Loosener", "5 Handstand push-ups\n10 1-legend squarts\n15 Pull-ups"),
            Workout("Care agony", "100 pull-ups\n100 Push-ups\n100 Sit-ups\n100 Squarts"),
            Workout("The wimp Special", "5 Pull-ups\n10 Push-ups\n15 Squarts"),
            Workout("Strength and Length", "500 meter run\n21 x 1.5 pood kettleball swing\n21 x pull-ups")
        )
        val names = arrayOfNulls<String>(4)
        for(i in workouts.indices) {
            names[i] = workouts[i].getName()
        }
        var adapter: ArrayAdapter<String?> = ArrayAdapter(
            layoutInflater.context,
            android.R.layout.simple_list_item_1,
            names
        )
        setAdapter(adapter)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnListFragmentInteractionListener) {
            listener = context
        } else {
            throw RuntimeException(context.toString() + " must implement OnListFragmentInteractionListener")
        }
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     *
     *
     * See the Android Training lesson
     * [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html)
     * for more information.
     */
    interface OnListFragmentInteractionListener {
        // TODO: Update argument type and name
        fun onListFragmentInteraction(item: DummyItem?)
    }

}


ошибка в setAdapter()
  • Вопрос задан
  • 158 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы