Spinner not showing options in kotlin fragment - android

I'm trying to add options to spinner while using databinding in fragment but it's not working, I can't understand why
class AddTransaction: Fragment() {
private var _binding: TransactionAddFragmentBinding? = null
private val binding get() = _binding!!
lateinit var spinner: Spinner
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = TransactionAddFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
fun spinner() {
spinner = binding.spinnerCategory
spinner.adapter = ArrayAdapter.createFromResource(
requireContext(), R.array.categories, R.layout.transaction_add_fragment)
}
}

Based on what you have posted, you are most likely never calling your spinner() function, which means you never set the adapter so it remains empty.
You were also passing R.layout.transaction_add_fragment to the spinner adapter, which is probably not what you wanted. The layout resource you pass in there should be for the spinner row.
Instead of putting it in a separate function, just add it to onViewCreated, like this:
class AddTransaction: Fragment() {
private var _binding: TransactionAddFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = TransactionAddFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// set the adapter here, and there is no need to make a separate "spinner"
// class member since "binding" is already a class member that contains
// the spinner
binding.spinnerCategory.adapter = ArrayAdapter.createFromResource(
requireContext(), R.array.categories, android.R.layout.simple_spinner_item)
}
}

Related

ListView Array Adapter error"None of the following functions can be called with the arguments supplied."

I'm trying to create a fragment with a ListView with the id=lv and the ArrayAdapter doesn't seem to work
this is the fragment code
class Istoric : Fragment() {
var array = arrayOf("item1","item2")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val adapter = ArrayAdapter(this,R.layout.listview_item,array)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_istoric, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val lv1: ListView = view.findViewById(R.id.lv)
lv1.setAdapter(adapter)
}
}
You need to pass context instead of this
val adapter = ArrayAdapter(requireContext(), R.layout.listview_item, array)

View and paramater is returning null in android kotlin

I am having a fragment and I'm sending data(Params i.e url as string from activity) to it from an activity, but in onCreateView it's(Params) showing null and also view as null I tried the bundle method also it's also not working and the code is like this.
private const val ARG_PARAM1 = "url"
private var Arg2 = ""
var mView: View? = null
class FragmentDisplayImage : Fragment() {
private var param1: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString("url")
Arg2 = param1.toString()
// Toast.makeText(context, param1.toString() , Toast.LENGTH_SHORT).show()
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState);
var a =this.getView()
var image= view?.findViewById<ImageView>(R.id.Fragimage)
Picasso.get().load(Arg2).resize(700, 700).centerCrop().into(image);
return inflater.inflate(R.layout.fragment_display_image, container, false)
}
how to get param1 value in oncreateView and how to solve this image/view which is giving null
The onCreateView method is supposed to return the view. You are getting null there because using getView before the view is created cant give you the view. If you change that for requireView it wont be null but it will throw an exception.
Since you are using findViewById and not bindings what you have to do is to override onViewCreated that method includes an argument view.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_display_image, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
var image= view.findViewById<ImageView>(R.id.Fragimage)
Picasso.get().load(Arg2).resize(700, 700).centerCrop().into(image);
}

Why it throw error like "did not return a View from onCreateView() or this was called before onCreateView()."?

I have a problem in my project, there is not any error in my codes, but when I build project, it is throw an error for viewPager2 = requireView().findViewById(R.id.vpager), I do not know why?, any idea?
class MyFragment : Fragment(R.layout.fragment_my) {
lateinit var viewPager2: ViewPager2
lateinit var adapter: VideoAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
/**set find id*/
viewPager2 = requireView().findViewById(R.id.vpager)
/**set database*/
val mDataBase = Firebase.database.getReference("videos")
val options = FirebaseRecyclerOptions.Builder<VideoModel>()
.setQuery(mDataBase,VideoModel::class.java)
.build()
/**set adapter*/
adapter = VideoAdapter(options)
viewPager2.adapter = adapter
return view
}
override fun onStart() {
super.onStart()
adapter.startListening()
}
override fun onStop() {
super.onStop()
adapter.stopListening()
}
}
Try This
class MyFragment : Fragment() {
lateinit var viewPager2: ViewPager2
lateinit var adapter: VideoAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_my, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
/**set find id*/
viewPager2 = view.findViewById(R.id.vpager)
/**set database*/
val mDataBase = Firebase.database.getReference("videos")
val options = FirebaseRecyclerOptions.Builder<VideoModel>()
.setQuery(mDataBase,VideoModel::class.java)
.build()
/**set adapter*/
adapter = VideoAdapter(options)
viewPager2.adapter = adapter
}
override fun onStart() {
super.onStart()
adapter.startListening()
}
override fun onStop() {
super.onStop()
adapter.stopListening()
}
}
At onCreateView fragment lifecycle step, the view hasn't been created yet.
Best practice would be to move that instantiation logic (including the VideoAdapter assignment) inside onViewCreated, where it's guaranteed that your view is built and ready to be referenced.

Trying to make a spinner in a fragment and getting an error could youse a suggestion

The thing is when i try to set up a simple spinner into the second fragment of my application i get an error which i believe is appering because it has to do something with the main activity but im not sure what.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_second).setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
setupspinner()
}
}
private fun setupspinner() {
val adapter =ArrayAdapter.createFromResource(
this,
R.array.zemlje1,
android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
}
}
The this, and the spinner are in the red sorry if i haven`t formated the question as i sould have had.

Kotlin spinners in fragments using

I'm new to Kotlin and I'm just trying to figure out how to use spinners in a fragment.
So I guess the code below is how I should initialize the spinner but how do I point this to my premade strings in R.array and how do I capture the selections on button click. Thanks.
class paychart : Fragment() {
private lateinit var paychartview: View
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
paychartview = inflater.inflate(R.layout.fragment_paychart, container, false)
}
val yearspinner = paychartview.findViewById<Spinner>(R.id.spinneryear)
}
Use ArrayAdapter.createFromResource
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val paychartview = inflater.inflate(R.layout.fragment_paychart, container, false)
val spinner = paychartview.findViewById<Spinner>(R.id.spinneryear)
spinner?.adapter = ArrayAdapter.createFromResource(activity, R.array.string_array, android.R.layout.simple_spinner_item) as SpinnerAdapter
spinner?.onItemSelectedListener = object :AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
//..
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val item = parent?.getItemAtPosition(position).toString()
//..
}
}
return paychartview
}

Categories

Resources