Easy to use adapter — detailed explanation of arrayadapter

The most terrible thing about procrastination is that even if you don't procrastinate on your side, the other side will procrastinate, leading to procrastination on your side! Now the project has been completed on my side, but the other party has not finished it, resulting in the failure to submit the whole project.

This is the horror of procrastination: we fight not only our own procrastination, but the Procrastination of all relevant people. It is the slowest person who decides whether the project can be submitted.

Since the decision is no longer in my hands, I can also do other things, such as blogging now.

This time, let's introduce the relatively simple but very convenient arrayadapter in listview.

Arrayadapter is a derived class of baseadapter. Based on baseadapter, it adds an important function: you can directly use generic construction.

Let's start with a simple example:

An arrayadapter is customized here. The use of the adapter has been involved in the previous simpleadapter, so the customized arrayadapter is taken as an example.

We need to list the information of students here. We need three textviews:

When customizing the arrayadapter, the most magical thing is that we can specify the data type bound by the arrayadapter, which can be either a basic data type or a custom object type, such as the user type this time. There are many forms for the construction method of custom arrayadapter. This time, the resource ID of a view is passed in, but we can also specify the bound data type.

The magic of arrayadapter is that we can operate arrayadapter like array! Like the add operation in the example, other adapters need to be passed into a container. Why can an arrayadapter handle object type data? In fact, the arrayadapter uses the toString () method of the object in the array to fill the specified textview, so we can customize the display of the listview by overriding the toString () method of the object.

In this way, we can display all the data in only one row.

The biggest question about using the arrayadapter is whether we need to transfer an existing container into the arrayadapter? Originally, the arrayadapter itself used the basic operations of a general container, such as adding new elements, but it itself can not be used as a container. More often, we want to give the elements in a container to the arrayadapter, which determines its display form.

If we define the data type bound by arrayadapter as object, we can freely pass in any type of container without any type conversion operation!

Arrayadapter can not only display textview, but also display any other non textview components like other adapters:

If we need other components in our layout, we must specify the ID of the textview used to display the data in the arrayadapter in the layout.

If it's just convenient to bind data, it's not necessary to create an arrayadapter independently. Just override getview(). Just as the container is used to facilitate the processing of a large amount of data, the arrayadapter is also used to deal with large data. For example, when there are more than 100 pieces of data or frequent dynamic additions and deletions, the arrayadapter can be used, Moreover, in order to facilitate us to refresh the UI, the arrayadapter also provides the setnotifyonchange() method, which can reduce the processing capacity of the UI and make the UI refresh faster, mainly by stopping the operations of add, insert, remove and clear.

summary

The above is all about the detailed explanation of the easy-to-use adapter - arrayadapter in this article. I hope it will be helpful to you. Interested friends can continue to refer to this site: Python fun project - pornographic picture identification code sharing, python implementation of a simple verification code program, python generated digital picture code sharing, etc. if you have any questions, you can leave a message at any time. Company is the longest advertisement. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>