[Android] ListView Getting Started

Source: Internet
Author: User

The Listview component is very important,4 of The time is doing this, or through the previous section of the database, write a for loop, insert Bar data.

First use the stupid method to display the data, the root layout linearlayout define an ID, in the activity to get this ID .

Get the Persondao object, call the DAO object's query all the way, and get The data for a list collection.

Traversing this collection, the toString () method of invoking the person entity class object is traversed to get a recorded string.

The TextView Object is instantiated in the traversal, the SetText () method of the TextView object is called , and a piece of information is put in.

Call the AddView (Child) method of the linearlayout object , which is a sub - view Object

Stupid method Code:

        LinearLayout lv=(linearlayout) Findviewbyid (r.id.lv);        Persondao DAO=new Persondao (this);        List<Person> persons=dao.select ();          for (person person:persons) {            TextView TV=new TextView (this);            Tv.settext (Person.tostring ());            Lv.addview (TV);        }

At this time the screen height is not enough, beyond the information will be hidden directly, the system to provide a component called ScrollView can scroll up and down, wrap the linearlayout, this time will prompt the namespace where the error , place namespaces in sorollview , specify width and height are filled with parent form

When the amount of data is large, so that the direct new out of countless objects is very resource-intensive, it should be in the sliding process of dynamically new component objects, out of sight to destroy, this time introduced a new control ListView

GetListViewobject, byFindviewbyid ()method, callingListViewobject thatSetadapter (Adapter)method to set the data binding to theListView, the parameter is aListAdapterinterface type, we want to implement this interface, pass the implementation class object in, define a private inner class implementationListAdapter, this will require the implementation of dozens of methods, too many, the system provides us with an implementation classBaseadapter, most of the default implementation classes arebasexxx,simplexxx,defaultxxxStart

Baseadapter is an abstract class, to implement his abstract method, the inner class inherits Baseadapter to implement the following methods

GetCount (), returns the number of int data, defines my data collection as a member property, calls the List collection size () Method

GetItem (),

Getitemid ()

GetView (position,)   control index location object, position is the index of the current collection,

In this method, we can instantiate textview object, Need to pass in the context object, using getapplicationcontext () get context, call textview of the object settext () list get () method, incoming index position get, return that tv object. In this method log.i () The print log can see that only the view object.

Activity code:

 Packagecom.tsh.database;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportCom.tsh.database.dao.PersonDao;ImportCom.tsh.database.domain.Person; Public classMainactivityextendsActivity {PrivateList<person>persons; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); ListView LV=(ListView) Findviewbyid (r.id.mylist); Persondao DAO=NewPersondao ( This); Persons=Dao.select (); Lv.setadapter (NewMyadapter ()); }    Private classMyadapterextendsbaseadapter{@Override Public intGetCount () {//TODO auto-generated Method Stub            returnpersons.size (); } @Override PublicObject GetItem (intposition) {            //TODO auto-generated Method Stub            return NULL; } @Override Public LongGetitemid (intposition) {            //TODO auto-generated Method Stub            return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {LOG.I ("Mainactivity", "Build Object" +position); TextView TV=NewTextView (Getapplicationcontext ()); Person Person=Persons.get (position);            Tv.settext (Person.tostring ()); returnTV; }            }}

[Android] ListView Getting Started

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.