This section briefly describes how to use the listview control:
1. Create an empty Android Application Project:
After the project is created, eclipse builds a basic helloworld application by default.ProgramFramework. In layout on the main interface, you can see a textview control in the center of the screen. The text content is helloworld;
2. The newly created Project is based on the activity. However, this article describes how to use the listview control. Therefore, you need to replace the activity with the listactivity. The operation is as follows:
A) Open Com. xxx. xxx folder, which stores the project's source program file "*. java, such as mainactivity. java, replace the field "extends activity" in the file with "extends listactitivy".
B) Add "Import Android. App. listactivity;" in the mainactivity. Java file header.
C) Open the layout folder under the res folder in the work path of the project, where the project interface file "*. XML ", such as" mainactivity. XML ", add the listview control to the interface file, and change the new control ID to" @ Android: ID/List "
note (from the android development website):
listactivity has a default layout that consists of a single, full-screen list in the center of the screen. however, if you desire, you can customize the screen layout by setting your own view layout with setcontentview () in oncreate (). to
do this, your own view must contain a listview object with the ID "@ Android: ID/List "(or List
If
it's in code ).
3. now, the basic framework of the project has been built. You can compile and use this project to see an empty interface from the simulation environment of the virtual machine. This is because you have not added an item to the listview. If the program crashes during running, the operation is incorrect. You can modify and improve the project according to the instructions in step 1.
4. Next, add a new item to listview as needed:
A) Open the mainactivity. Java source file in the com. ***. *** folder in the SCR directory, and add the following at the bottom of the oncreate function or other appropriate positions:Code:
// Simulation database. string [] STR = {"AAA", "BBB", "CCC", "DDD", "eee", "fff "}; setlistadapter (New arrayadapter <string> (this, android. r. layout. simple_list_item_1, STR); // android. r. layout. simple_list_item_1-> @ Android: ID/LIST/** official reference: ** listactivity has a default layout that consists of a single, * full-screen list in the center of the screen. * However, if you desire, you can customize the screen Layout * by setting your own view layout with setcontentview () in oncreate (). * To do this, your own view must contain a listview object * with the ID "@ Android: ID/List" (or list if it's in code) */getlistview (). settextfilterenabled (true );
B) Add the following code to the header of the mainactivity. Java source file:
Import Android. widget. arrayadapter; import Android. widget. baseadapter; import Android. widget. listadapter;
C) compile the project. In the listview control on the simulation interface, seven items, including "AAA", "BBB", and "CCC", will appear. So far, the creation of the listview control is successful.
(End)