Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ListView in Android

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The specific use of ListView in Android, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Simple usage of ListView

It's easy to add a ListView control to the layout. First, specify an id for ListView, and then set the width and height to match_parent, so that ListView takes up the space of the whole layout.

Next, modify the code in MainActivity

Class MainActivity: AppCompatActivity () {privateval data = listOf ("Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry" "Mango") override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) val adapter = ArrayAdapter (this, android.R.layout.simple_list_item_1, data) listView.adapter = adapter}

Prepare the data first, and then pass the data to ListView with the help of the adapter. ArrayAdapter is an implementation class of an adapter provided by Android. You can specify the data type to be adapted through generics, and then pass in the data to be adapted in the constructor. Enter the instance of Activity, the id of ListView sub-item layout, and the data source in the constructor of ArrayAdapter. Here, we use android.R.layout.simple_list_item_1 as the id of ListView sub-item layout, which is an Android built-in layout file with only one TextView, which can be used to simply explicit a piece of text. Finally, the setAdapter () method of ListView is called to pass in the built adapter object, so that the association between ListView and data is established.

Customize the interface of ListView

ListView, which can only display one piece of text, is so monotonous that we now want to customize the interface of ListView so that it can display text and pictures

Specify a custom layout in the subkey that requires ListView, and create a new fruit_item.xml under the layout directory

Define an entity class as the adaptation type of the ListView adapter

Class FruitAdapter (activity: Activity, val resourceId: Int, data: List): ArrayAdapter (activity, resourceId, data) {@ SuppressLint ("ViewHolder") override fun getView (position: Int, convertView: View?, parent: ViewGroup): View {val view = LayoutInflater.from (context) .innovate (resourceId, parent) False) val fruitImage: ImageView = view.findViewById (R.id.fruitImage) val fruitName: TextView = view.findViewById (R.id.fruitName) val fruit = getItem (position) if (fruit! = null) {fruitImage.setImageResource (fruit.imageId) fruitName.text = fruit.name} return view}}

The FruitAdapter class inherits from ArrayAdapter, and the generic type is specified as the Fruit class, overriding the getView () method. In the getView () method, first use LayoutInflater to load the layout we passed in for this subitem, then call the findViewById () method of View to get ImageView and TextView respectively, then get the Fruit instance of the current item through the getItem () method, set the displayed picture and text, and finally return the layout

Finally, modify the code in MainActivity

Class MainActivity: AppCompatActivity () {privateval fruitList = ArrayList () privateval data = listOf ("Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry" "Mango") override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) initFruits () val adapter = FruitAdapter (this, R.layout.fruit_item, fruitList) listView.adapter = adapter} private fun initFruits () {repeat (2) {fruitList.add ("Apple") R.drawable.apple_pic)) fruitList.add (Fruit ("Banana", R.drawable.banana_pic)) fruitList.add (Fruit ("Orange", R.drawable.orange_pic)) fruitList.add (Fruit ("Watermelon", R.drawable.watermelon_pic)) fruitList.add (Fruit ("Pear", R.drawable.pear_pic)) fruitList.add (Fruit ("Grape") R.drawable.grape_pic)) fruitList.add (Fruit ("Pineapple", R.drawable.pineapple_pic)) fruitList.add (Fruit ("Strawberry", R.drawable.strawberry_pic)) fruitList.add (Fruit ("Cherry", R.drawable.cherry_pic)) fruitList.add (Fruit ("Mango", R.drawable.mango_pic))}

Improve the running efficiency of ListView

There is also a convertView parameter in the getView () method, which is used to cache the previously loaded layout for later reuse, which we can use for performance optimization.

Class FruitAdapter (activity: Activity, val resourceId: Int, data: List): ArrayAdapter (activity, resourceId, data) {@ SuppressLint ("ViewHolder") override fun getView (position: Int, convertView: View?, parent: ViewGroup): View {val view: View if (convertView = = null) {view = LayoutInflater.from (context) .expiate (resourceId, parent) False)} else {view = convertView} val fruitImage: ImageView = view.findViewById (R.id.fruitImage) val fruitName: TextView = view.findViewById (R.id.fruitName) val fruit = getItem (position) if (fruit! = null) {fruitImage.setImageResource (fruit.imageId) fruitName.text = fruit.name} return view}}

We judged in the getView () method: if convertView is null, LayoutInflater is used to load the layout; if not null, convertView is directly reused

At present, the code can continue to be optimized. Every time in the getView () method, the findViewById method of View is still called to get an instance of the control. We can optimize this part of the performance with the help of a ViewHolder and modify the code in FruitAdapter.

Class FruitAdapter (activity: Activity, val resourceId: Int, data: List): ArrayAdapter (activity, resourceId, data) {inner class ViewHolder (val fruitImage: ImageView, val fruitName: TextView) @ SuppressLint ("ViewHolder") override fun getView (position: Int, convertView: View?, parent: ViewGroup): View {val view: View val viewHolder: ViewHolder if (convertView = = null) {view = LayoutInflater.from (context) .expiate (resourceId, parent) False) val fruitImage: ImageView = view.findViewById (R.id.fruitImage) val fruitName: TextView = view.findViewById (R.id.fruitName) viewHolder = ViewHolder (fruitImage) FruitName) view.tag = viewHolder} else {view = convertView viewHolder = view.tag as ViewHolder} val fruit = getItem (position) if (fruit! = null) {viewHolder.fruitImage.setImageResource (fruit.imageId) viewHolder.fruitName.text = fruit.name} return view}}

We added an inner class ViewHolder to cache control instances of ImageView and TextView. When convertView is null, create a ViewHolder object, store the instance of the control in ViewHolder, and then call the setTag () method of View to store the ViewHolder object in View

Click event of ListView

After all, the scrolling of ListView only satisfies our visual effect, so this section learns how ListView can respond to user clicks.

Class MainActivity: AppCompatActivity () {privateval fruitList = ArrayList ()... Override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) initFruits () val adapter = FruitAdapter (this, R.layout.fruit_item, fruitList) listView.adapter = adapter / * val adapter = ArrayAdapter (this, android.R.layout.simple_list_item_1, data) * / / * listView.adapter = adapter*/ listView.setOnItemClickListener {parent, view, position Id-> val fruit = fruitList [position] Toast.makeText (this, fruit.name, Toast.LENGTH_SHORT). Show ()} questions about the specific use of ListView in Android are shared here. I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report