Android Learning Note _listview (List view) (i)

Source: Internet
Author: User

First, the concept.

The ListView displays all list items as a vertical list.

Second, use

Assume that there are 50 data messages in the database (persons, including name, number, and money) that are displayed through the ListView.

1, to the database of 50 person information. Specific additions can be found in the database section, with the Dao2 Add method in the database example combined with the For loop increase.

Add the data code as follows:

1  Public voidTestadd ()throwsException {2PersonDao2 DAO =NewPersonDao2 (GetContext ());3         LongNumber = 885900000l;4Random random =NewRandom ();5          for(inti=0;i<50;i++){6Dao.add ("Wangwu" +i, Long.tostring (number+i), Random.nextint (5000));7         }    8}
View Code

2. Display the database information to the interface.

(1) implemented with the ListView component. The ListView is a typical MVC design pattern. M:mode data type, which is the list collection; V:view view, which is Listview;c:controller controller, adapter data adapter.

①. Use a linear layout (linearlayout) in the layout file, in the vertical direction.

②. Adding a ListView control, setting the ID and width height (populating the form)

③. Create in the main code and find the ListView control through Findviewbyid;

④. Use the SETADAPT (adapter (ListAdapter Interface)) method of the ListView. Because the parameter adaper is an interface type, you need to create an implementation class for the interface.

⑤. Create a Class (MYADAPT) to implement the ListAdapter interface and implement the methods implemented in the interface. Because the ListAdapter interface is implemented in many ways, the new Class (Myadapter) simply inherits the simple implementation class (Baseadapter) of the ListAdapter interface, because Baseadapter is an abstract class, So you need to implement the abstract method. (rule: If an interface implementation class is very much, generally there will be basexxx, simplexxx, defaultxxx and other simple implementation classes, and then simply inherit these simple implementation classes can be).

⑥.baseadapter abstract methods in abstract classes. GetCount (): Controls how many entries are in the ListView, that is, the number of entries in the list collection returned from the database, that is, the number of entries = Size;getview of the collection (int position, View Convertview, ViewGroup parent), gets a View object that represents the data in one location of the data collection, and the method parameter position the position of the current data in the collection.

⑦. in GetView (int position, View Convertview, ViewGroup Parent) method, New sets a TextView object and places it in the context through the Getapplicationcontext () method, and then gets the person object that corresponds to the position location through the get (position) method of the person. The string object is obtained by the ToString () method of the position object and added to the TextView object through the SetText method of the TextView object, and the method returns the TextView object.

The code shown through the Listvie component is as follows:

Main code:

1  Public classMainactivityextendsActivity {2     PrivateListView LV;3     PrivateList<person>persons;4 5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9LinearLayout Ll_root =(LinearLayout) Findviewbyid (r.id.ll_root);TenPersondao DAO =NewPersondao ( This); OnePersons =Dao.findall (); A          -LV =(ListView) Findviewbyid (r.id.lv); -Lv.setadapter (NewMyadapter ());  the     } -      -      Public classMyadapterextendsbaseadapter{ -         Private Static FinalString TAG = "Myaadapter"; +          - @Override +          Public intGetCount () { A             returnpersons.size (); at         } -  - @Override -          PublicObject GetItem (intposition) { -             //TODO auto-generated Method Stub -             return NULL; in         } -  to @Override +          Public LongGetitemid (intposition) { -             //TODO auto-generated Method Stub the             return0; *         } $ Panax Notoginseng @Override -          PublicView GetView (intposition, View Convertview, ViewGroup parent) { theLOG.I (TAG, "Return to view object, Position:" +position); +TextView TV =NewTextView (Getapplicationcontext ()); ATv.settextsize (20); the Tv.settextcolor (color.black); +Person person =Persons.get (position); - Tv.settext (person.tostring ()); $             returnTV; $         }         -     } -}
View Code

Layout code:

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:id= "@+id/ll_root"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6android:orientation= "Vertical"7Tools:context= ". Mainactivity ">8     9<ListViewTenAndroid:id= "@+id/lv" Oneandroid:layout_height= "Match_parent" AAndroid:layout_width= "Match_parent" ></ListView> -  -</LinearLayout>
View Code

   

(2) implemented with non-ListView components

①. The interface is defined as a linear layout linearlayout, which defines the ID;

②. The linear layout is found in the main code through Findviewbyid;

③.new out a Persondao object that gets a collection of all the person objects through the FindAll () method inside the Persondao object;

④. Traversing the person collection with a for loop;

⑤. In for to convert each person object through the ToString () method to a string string, and new to a TextView () object, set the TextView () object's font size, color, and other related data, Each person information is then added to the corresponding TextView () using the SetText () method of the TextView () object.

⑥. Because LinearLayout is a container for a View object, you can dynamically add each chind () object to TextView using the LinearLayout addview (View linearlayout) method.

⑦. Use ScrollView object to wrap LinearLayout object, realize the multi-data scroll display effect.

The main program code is as follows:

1  Public classMainactivityextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7LinearLayout Ll_root =(LinearLayout) Findviewbyid (r.id.ll_root);8Persondao DAO =NewPersondao ( This);9list<person> persons =Dao.findall ();Ten          for(person person:persons) { OneString info =person.tostring (); ATextView TV =NewTextView ( This); -Tv.settextsize (20); - Tv.settextcolor (color.black); the Tv.settext (info); - Ll_root.addview (TV); -         } -     } +}
View Code

The layout code is as follows:

1<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent" >5 6<LinearLayout7Android:id= "@+id/ll_root"8Android:layout_width= "Match_parent"9android:layout_height= "Match_parent"Tenandroid:orientation= "Vertical" OneTools:context= ". Mainactivity "> A</LinearLayout> -  -</ScrollView>
View Code

Android Learning Note _listview (List view) (i)

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.