Using the Universal adapter Base-adapter-helper

Source: Internet
Author: User

Adapter write more will have a kind of write to vomit feeling, today to experience without writing adapter wait feel, generally speaking, we write adapter is rewrite GetView method, and then use Viewholder design mode, in GetView data binding, write once fortunately, Write more will feel in constantly doing repetitive work, then there is no such a generic adapter for us to use, rather than write so many repeated code, the answer is yes, see Https://github.com/JoanZapata/base-adapter-helper, Use this adapter today.
Before that, take a look at the implementation effect.

Start writing entity classes, which involve headings, descriptions, and picture resources

 PackageCn.edu.zafu.demo; Public  class News {    PrivateString title;PrivateString description;Private intImgid; PublicStringGetTitle() {returnTitle } Public void Settitle(String title) { This. title = title; } PublicStringgetdescription() {returnDescription } Public void setdescription(String description) { This. Description = description; } Public int Getimgid() {returnImgid; } Public void Setimgid(intImgid) { This. Imgid = Imgid; }@Override     PublicStringtoString() {return "News [title=]+ title +", description="+ Description +", imgid="+ Imgid +"]"; }}

The corresponding layout

<relativelayout  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 " tools:context  =" ${relativepackage}.${activityclass} " >     <imageview  android:id
      = "@+id/img"  android:layout_width  = "40DP"  android:layout_height  =" 40DP " android:layout_centervertical  =" true " android:layout_marginleft  =         "5DP"  android:background  = "@drawable/ic_launcher"  />     <TextViewandroid:id= "@+id/title"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:layout_marginleft=" 5DP "android:layout_margintop=" 5DP "android:layout_torightof=" @id/img "  Android:text="Title title title Title Header title"android:textsize="16sp" />                                                                     <TextView        Android:id="@+id/description"        Android:layout_width="Wrap_content"        Android:layout_height="Wrap_content"        Android:layout_below="@id/title"        Android:layout_marginbottom="5DP"        Android:layout_marginleft="5DP"        Android:layout_margintop="5DP"        Android:layout_torightof="@id/img"        Android:text=" Description Description description"/></relativelayout>

It's time to witness a miracle, see how we can do this with a few lines of code, and before that, we'll forge a few pieces of data.

    PrivateList<news> news=NewArraylist<news> ();Private void InitData() {News _new=NewNews (); _new.settitle ("Nanjing abused boy's adoptive mother sentenced to detention"); _new.setdescription ("intentional injury; before the boy was);        _new.setimgid (R.DRAWABLE.QBLOG_FIG1);        News.add (_new); _new=NewNews (); _new.settitle ("Today's headline"); _new.setdescription ("Describe today, whisper.");        _new.setimgid (R.DRAWABLE.QBLOG_FIG2);        News.add (_new); _new=NewNews (); _new.settitle ("Today's headline"); _new.setdescription ("Describe today, whisper.");        _new.setimgid (R.DRAWABLE.QBLOG_FIG3);    News.add (_new); }

Start initializing our interface

    private ListView listView;    privatevoidinitView() {        listView=(ListView) findViewById(R.id.listview);        listView.setAdapter(new QuickAdapter<News>(this, R.layout.item, news) {            @Override            protectedvoidconvert(BaseAdapterHelper helper, News item) {                helper.setText(R.id.title, item.getTitle());                helper.setText(R.id.description, item.getDescription());                helper.setBackgroundRes(R.id.img, item.getImgId());            }        });    }

Called in OnCreate.

    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initData();        initView();    }

Run it, and you'll find that the magic, the data and the ListView are bound, yes, this is the role of the universal adapter, in fact, there is another adapter in this library, interested in their own to see the usage.

SOURCE download
http://download.csdn.net/detail/sbsujjbcy/8565361

Using the Universal adapter Base-adapter-helper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.