kotlin Android主屏幕仅显示最后一个列表文本

szqfcxe2  于 2022-12-13  发布在  Kotlin
关注(0)|答案(2)|浏览(112)

我想知道为什么家庭小部件只显示最后一个数组的文本?如何使内容作为列表?谢谢帮助
这是我XML布局

<LinearLayout 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"
        android:layout_margin="8dp"
        android:orientation="vertical"
        android:background="@drawable/widget_background"
        android:padding="8dp"
        android:id="@+id/widget_container">
    
        <TextView
            android:id="@+id/widget_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:textStyle="bold"
            tools:text="Title" />
    
        <TextView
            android:id="@+id/widget_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            tools:text="Message" />
</LinearLayout>

这是Kotlin提供程序

val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
        for (i in cars){
        val message = widgetData.getString("message", null)
        setTextViewText(R.id.widget_message, message
                ?: i)

        val pendingIntentWithData = HomeWidgetLaunchIntent.getActivity(
                context,
                MainActivity::class.java,
                Uri.parse("homeWidgetExample://message?message=$message"))
        setOnClickPendingIntent(R.id.widget_message, pendingIntentWithData)
        }
    }
    appWidgetManager.updateAppWidget(widgetId, views)
}

该屏幕截图

wswtfjt7

wswtfjt71#

不幸的是,您不能循环布局视图并期望它重复。当您循环数组并将文本设置为TextView时,它将依次设置所有这些值,并以显示最后一个元素结束。如果您希望显示数组中的所有值,您应该使用RecyclerView而不是TextView,并将数组传递给RecyclerView的适配器。

3z6pesqy

3z6pesqy2#

在活动XML布局中插入recyclerView

<LinearLayout 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"
    android:layout_margin="8dp"
    android:orientation="vertical"
    android:background="@drawable/widget_background"
    android:padding="8dp"
    android:id="@+id/widget_container">

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

然后创建另一个XML布局并将其命名为home_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/widget_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textStyle="bold"
        tools:text="Title" />

    <TextView
        android:id="@+id/widget_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        tools:text="Message" />
</LinearLayout>

下一步是为RecyclerView创建一个适配器。

class CustomAdapter(private val mList: List<String>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
  
    // create new views
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // inflates the home_item view 
        // that is used to hold list item
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.home_item, parent, false)
  
        return ViewHolder(view)
    }
  
    // binds the list items to a view
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  
        val messageVal= mList[position]
  
        // sets the text to the textview from our itemHolder class
        holder.messageView.text = messageVal
  
    }
  
    // return the number of the items in the list
    override fun getItemCount(): Int {
        return mList.size
    }
  
    // Holds the views for adding it to image and text
    class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
        val messageView: TextView = itemView.findViewById(R.id.widget_message)
    }
}

在活动类上OnCreate()

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
        // getting the recyclerview by its id
        val recyclerview = findViewById<RecyclerView>(R.id.recyclerview)
  
        // this creates a vertical layout Manager
        recyclerview.layoutManager = LinearLayoutManager(this)
  
        // ArrayList of class ItemsViewModel
        
  
        // This will pass the ArrayList to our Adapter
        val adapter = CustomAdapter(cars)
  
        // Setting the Adapter with the recyclerview
        recyclerview.adapter = adapter
  
    }

请确定您已先实作recyclerView

implementation 'androidx.recyclerview:recyclerview:1.2.0'

相关问题