Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

java - How i supposed to show listView in fragment class with adapter from another class

Hello i have this problem, i am new to this.I try to show my list in fragment. My app is running, but when i go to my fragment it crashes and shows This Error:

2021-01-08 14:27:25.442 13129-13129/com.example.parkngo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.parkngo, PID: 13129
android.view.InflateException: Binary XML file line #53: For input string: "64.0dip"
Caused by: java.lang.NumberFormatException: For input string: "64.0dip"

Apperanty error is in my Adapter class 17row whitch is this:

 val view : View = layoutInflater.inflate(id , null)

I've tried to look up information about viewGroups e.t.c

I have my created Adapter class here

package com.example.parkngo

class Adapter(context: Context, var id: Int, var list: List<Spots>):
     ArrayAdapter<Spots>(context,id,list){
          override fun getView(index: Int, convertView: View?, parent: ViewGroup): View {
               val layoutInflater: LayoutInflater = LayoutInflater.from(context)
               val view : View = layoutInflater.inflate(id , null)

               val address : TextView = view.findViewById(R.id.textView1)
               val spotsLeft : TextView = view.findViewById(R.id.textView2)
               val time : TextView = view.findViewById(R.id.textView3)

               var spotsList : Spots = this.list[index]

               address.text=spotsList.adress
               spotsLeft.text=spotsList.spotsLeft.toString()
                time.setText(spotsList.time)

        return super.getView(index, convertView, parent)
  }}

My fragment

class FreeSpotFragment : Fragment() {


override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    var view: View = inflater.inflate(fragment_free_spot, container, false)
    var listview = view.findViewById<ListView>(R.id.listView)

    var list = mutableListOf<Spots>()

        list.add(Spots("Zemitāna laukums", 9, 21))
        list.add(Spots("Brīvibas iela 78", 10, 21))
        list.add(Spots("Zvaig??u iela 24", 9, 21))

    listview.adapter = Adapter(view.context, R.layout.row ,list)
    
    return view

     
}}

Here i will post my xml file for row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<LinearLayout
    android:layout_width="307dp"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:text="Parking spot adress"
        android:textColor="#000"
        android:textSize="18sp"
        android:textStyle="bold">
    </TextView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:text="Spots left"
        android:textColor="#000"
        android:textSize="15sp"
        android:textStyle="normal"
        android:id="@+id/textView2">
    </TextView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:text="Avilable parking time"
        android:textColor="#000"
        android:textSize="15sp"
        android:textStyle="normal"
        android:id="@+id/textView3">
    </TextView>

</LinearLayout>

</LinearLayout>

And here is fragment xml

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

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/freeSpotFragment"
    android:gravity="center"
    android:textSize="25sp"
    android:textStyle="bold"

    />
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    >

</ListView>


</FrameLayout>

I think my problem is in the fragment context. I probably don't have enough knowledge in this matter, but i've looked up many solutions, and try to study this..

Can someone explain my mistake here ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...