In the previous article, we have implemented the display of database content in the ListView. But in our ListView, typesetting is not very good-looking, so this article, we come to beautify the listveiw. Haha, plainly, is to add a layout file to the ListView, and then convert the layout file to the View object display.
Here the code we do not post all, paste the new added layout files and have to make changes to the method.
Let's look at the picture first.
Then look at the ListView layout file, in fact from this example, you can know that the ListView item can display very complex things, as long as the view object is can display the amount, as long as your layout file is written well. All can.
List_layout.xml
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TextViewandroid:textsize= "20SP"Android:id= "@+id/name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "First Name" /> <LinearLayoutAndroid:layout_alignparentright= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/age"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Age" /> <TextViewAndroid:id= "@+id/phone"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "mobile" /> </LinearLayout></Relativelayout>
Mainactivity.java here only to paste the changes in the place, in fact, is to modify the GetView of several statements. The content of the GetView method is written here
PublicView GetView (intposition, View Convertview, ViewGroup parent) {Girl Girl=Girlist.get (position); /*TextView TV = new TextView (mainactivity.this); Tv.settextsize (18); Gets the element Tv.settext (girl.tostring ()) in the collection;*/View v; /** Judgment is for the performance optimization of the ListView, Converview represents a buffer domain. * After seeing the items, leaving the screen, actually will be buffered, so we do not need to * every time to fill, you can first determine whether there is a buffer **/ if(Convertview = =NULL){ //fills the ListView layout file as a View objectv = view.inflate (mainactivity. This, R.layout.list_layout,NULL); }Else{v=Convertview; } /*V.findviewbyid (r.id.name); the front plus V is because the layout file element you are looking for is a ListView object's * and the V-loaded layout file is the ListView layout file*/TextView name=(TextView) V.findviewbyid (r.id.name); Name.settext (Girl.getname ()); TextView Age=(TextView) V.findviewbyid (r.id.age); Age.settext (Girl.getage ()+""); TextView Phone=(TextView) V.findviewbyid (R.id.phone); Phone.settext (Girl.getphone ()); returnv; }
ListView Displays the data beautification version of SQLite and performance optimization