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