0

I allready created some custom array adapters and they all work. I created this one like the other ones but I keep getting this error:

"android.view.InflateException: can be used only with a valid ViewGroup root and attachToRoot=true"

My Array Adapter:

class CustomArrayAdapter(private val context: Activity, private val customList : ArrayList<Component>)
: ArrayAdapter<Component>(context, R.layout.customcardcomponent, customList) {

@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

    val view = LayoutInflater.from(context).inflate(R.layout.customcardcomponent, parent, false)

    val description: TextView = view.findViewById(R.id.Description)
    val progressText : TextView = view.findViewById(R.id.ProgressBarText)
    val progressBar : LinearProgressIndicator = view.findViewById(R.id.ProgressBar)

    description.text = customList[position].description
    progressText.text = "${customList[position].current}/${customList[position].max}"
    progressBar.max = customList[position].max
    progressBar.progress = customList[position].current

    return view
    }
}

My ListViewPage.kt File

class ListViewPage : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_listview, container, false)
    val customListView = view.findViewById<ListView>(R.id.customListView)

    val listDescriptions = arrayOf(
        "description 1",
        "description 2",
        "description 3",
        "description 4",
    )
    val listMax = arrayOf(5, 10, 25, 50)
    val listCurrent = arrayOf(current, current, current, current)

    val customList : ArrayList<Component> = ArrayList()
    for (i in listDescriptions.indices){
        val component = Component(listMax[i], listCurrent[i] ,listDescriptions[i])
        customList.add(component)
        if (customList.size == 4){
            customListView.adapter = CustomArrayAdapter(context as Activity, customList)
        }
    }

    return view
    }
}

My ListViewPage.xml File

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/customListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"/>

</FrameLayout>

In the ArrayAdapter I tried to:

change {LayoutInflater.from(context).inflate(R.layout.customcardcomponent, parent, false)}

to {LayoutInflater.from(context).inflate(R.layout.customcardcomponent, parent, true)}

But the I get the follwoing error: "java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView"

I also tried to create a whole custom class for this list view, but I still got the same error.

I would like to know what Iam doing wrong with this ArrayAdapter

1 Answer 1

0

Here is the line form your fragment you should look into it :

  customListView.adapter = CustomArrayAdapter(context as Activity, customList)

you can't treat your fragment as an activity

if you want to refer to your fragment context you may want to use requireContext() or requireActivity() instead if (context as Activity )

and you also you may want to change your adapter's class first parameter from activity to Fragment or yourFragmentName specifically

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.