N
N
Newbie Ivanovich2020-06-09 15:11:58
Android
Newbie Ivanovich, 2020-06-09 15:11:58

How to add data to ListFragment kotlin?

How to add data to ListFragment kotlin?

Code with an error in the setAdapter function

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?)
    }

}


error in setAdapter()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-06-11
@zagayevskiy

Don't use ListFragment. Do not fill in footcloths of code, but only what is needed. Explain what the error is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question