简体   繁体   中英

android home screen only showing last list text

i wonder why home widget only show last array of text? how to make the content as list? thanks for the help

this is my XML layout

<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>

this is the kotlin provider

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

;'pl0l

the screenshot

Unfortunately you can't loop a layout view and expect it to repeat. when you loop through the array and set the text to the TextView it's setting them all on after the other and end with the last element being displayed. If you wish to display all the values inside the array you should use RecyclerView intead of TextView and pass the array to the adapter of the RecyclerView.

In your activity XML layout insert a 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>

Then create another XML layout and call it 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>

The next step is creating an adapter for your RecyclerView. So Create a new kotlin class and name it CustomAdapter.kt

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

On your Activity Class 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
  
    }

Make sure you implemented recyclerView first

implementation 'androidx.recyclerview:recyclerview:1.2.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM