Android listview universal adapter instance code

Listview is the most commonly used control in development, but it will always write duplicate code, which is a waste of time and meaningless.

Recently, referring to some materials, I found a universal listview adapter, which has less code and saves time. I'll summarize and share it with you.

First, a custom Adapter is inherited from BaseAdapter, the following is custom Adapter, the essence is in getView () method.

The above abstract method convert (viewholder, t); It is equivalent to that in the previous general code

viewHolder. mTextView = (TextView) convertView . findViewById(R.id.id_tv_title);

viewHolder. mTextView. setText(Bean.getName());

Find the ID of the control, and then go to the parameter viewholder holder in the repeated code methods such as settext. T t holder is equivalent to the viewholder in the previous general code, and t is equivalent to a self-defined entity class bean.

In the above code, one viewholder in the getview () method needs to be declared by itself. The following is the code and detailed comments:

Write another adapter to inherit from the universal adapter commonadapter, or write your own adapter, because a project may have multiple listviews, but the layout of each item element will be different. This is used to distinguish between different listviews and their corresponding items This code is small and can be written as an internal class in activity Java

After optimization, it is much easier to use the adapter. The following are entity classes, item and mainactivity Code in Java:

Entity class:

MainActivity. java:

Key codes:

The parameter has a context, a set, and its corresponding item. Adapter = new myadapter (this, MDATA, r.layout. Item);

Item layout:

design sketch:

Use this to fit a different layout:

Like the new layout adapter, it inherits its own universal adapter: commonadapter

Activity. Java code:

item:

design sketch:

The above is the data sorting of Android listview. I hope it can help you!

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
分享
二维码
< <上一篇
下一篇>>