Android Learning Note-listview implementation method of Baseadapter

Source: Internet
Author: User

The ListView is the most commonly used component in Android development, and here we learn to implement the ListView in a Baseadapter way,

The main layout activity_main.xml is this:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/ll_root "    android:layout_width=" Match_parent "    android: layout_height= "Match_parent"    android:orientation= "vertical" >    <listview android:id=        "@+id/lv"        android:layout_width= "match_parent"        android:layout_height= "match_parent"        android:orientation= " Vertical ">    </ListView></LinearLayout>

Defines a listitem.xml, which is used to display in a ListView component.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" 60dip "android:gravity=" center_vertical "a ndroid:orientation= "Horizontal" > <textview android:id= "@+id/tv_id" android:layout_width= "Wrap_cont Ent "android:layout_height=" wrap_content "android:layout_marginleft=" 5dip "android:text=" ID "a" Ndroid:textcolor= "#ff0000" android:textsize= "18sp"/> <linearlayout android:layout_marginleft= "20di         P "android:layout_width=" Fill_parent "android:layout_height=" 60dip "android:gravity=" center_vertical " android:orientation= "vertical" > <textview android:id= "@+id/tv_name" Android:la            Yout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "5dip"           android:text= "Name" Android:textcolor= "#000000" android:textsize= "18sp"/> <textview android:id= "@+id/tv_n Umber "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:l ayout_marginleft= "5dip" android:text= "number" android:textcolor= "#880000" android:textsize= "1 8sp "/> </LinearLayout></LinearLayout>

So the implementation logic in the Activiy is this, because I am in the database to take the value, so, the definition of a DAO class, and the reader, can fully define a ArrayList for the data display,

What we're doing here is baseddapter, here, this is to implement the two methods within the class, GetCount (): Get the number of data to show, GetView (): The presentation of the data, there is an important way: inflate (context context, int resource, ViewGroup root)

Context: is contextual,

Resource:resource the resource ID to inflate, which is the ID of the resource file to display in the ListView,

Root:root A View group that'll be the parent. Used to properly inflate the * layout_* parameters, this can be set bit empty.

  

public class Mainactivity extends Activity {private linearlayout ll;private ListView lv;private list<person> person    S        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        ll = (linearlayout) Findviewbyid (r.id.ll_root);        LV = (ListView) Findviewbyid (r.id.lv);        Get data Persondao dao = new Persondao (this);                       The type of persons is list<person> persons = Dao.findall ();      Lv.setadapter (New Myadapter ());    }//default implementation class Simplexxx,defaultxxx,basexxx. When the integration excuse is more, you can inherit Base,simple,defaule, these start private class Myadapter extends baseadapter{private static final String TAG = "Myadapter";//control how many entries in the ListView are in total @overridepublic int GetCount () {//TODO auto-generated method Stubreturn Persons.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;} @Overridepublic Long Getitemid (iNT position) {//TODO auto-generated method Stubreturn 0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//Gets the person object for a location. Person person = persons.get (position);//load the XML in view view = View.inflate (mainactivity.this, r.layout.listitem, NULL); /Be sure to look for the child's Idtextview tv_id = (TextView) View.findviewbyid (r.id.tv_id) in the View object; TextView tv_name = (TextView) View.findviewbyid (r.id.tv_name); TextView Tv_number = (TextView) View.findviewbyid (R.id.tv_number),//person.getid (), int, + "", int must be converted to string type. Tv_id.settext (Person.getid ()); Tv_id.settext ("ID:" +person.getid ()); Tv_name.settext ("Name:" +person.getname ()); Tv_number.settext ("Phone:" +        Person.getnumber ()); return view;} }}

Android Learning Note-listview implementation method of Baseadapter

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.