Previous: http://www.bkjia.com/kf/201203/121466.html
As shown in the previous Activity, start is the second Activity. I think it is easy to understand the program, but I should give the code first.
First look at the list_item.xml file:
1 <? Xml version = "1.0" encoding = "UTF-8"?>
2
3 <TextView xmlns: android = "http://schemas.android.com/apk/res/android"
4 android: layout_width = "fill_parent"
5 android: layout_height = "fill_parent"
6 android: padding = "10dp"
7 android: textSize = "16sp">
8 </TextView>
The xml file is simple, just a TextView, used to display every item in the List. In eclipse, GraphLayout is dragged in and automatically generated. Note that textSize can be modified, which determines the font size. The font unit is sp.
The Java code is as follows:
1 package Workshop. english. englishwords;
2
3 import android. app. Activity;
4 import android. OS. Bundle;
5 import android. widget .*;
6 import android. view .*;
7 import android. app. ListActivity;
8 import android. content. Intent;
9 import android. widget. AdapterView;
10 import android. widget. AdapterView. OnItemClickListener;
11
12/* This list activity list all unit1 ~ 12 */
13 public class UnitListAdapter extends ListActivity {
14
15
16 @ Override
17 protected void onCreate (Bundle savedInstanceState)
18 {
19
20 super. onCreate (savedInstanceState );
21
22 setListAdapter (new ArrayAdapter <String> (this, R. layout. list_item, UNIT_NAME ));
23 ListView lv = getListView ();
24 lv. setTextFilterEnabled (true );
25
26 lv. setOnItemClickListener (new OnItemClickListener (){
27 public void onItemClick (AdapterView <?> Parent, View view,
28 int position, long id ){
29
30 Intent intent = new Intent (UnitListAdapter. this, WordActivity. class );
31 startActivity (intent );
32}
33 });
34}
35
36 static final String [] UNIT_NAME = new String [] {
37 "unit1", "unit2", "unit3", "unit4", "unit5", "unit6 "};
38}
The code is actually very simple. I have always advocated simplicity. Easy to understand. Especially for beginners, simple code allows people to grasp the key and won't get lost.
Note that the key control used here is ListView. The ListView control is widely used in the applications we see. When we find the desired app in the most common app store, we see the ListView.
Therefore, ListActivity is inherited here, rather than directly inherited from Activity. One thing that is close to ListView is listAdapter.
Here, it reflects the MVC pattern. M is a mode, but it is actually a data source. V is a view, and data is displayed. C is a control, and data is controlled. It is a link connecting M and V.
In this example, we can regard the static String Array I defined as M, ListView as V, and listAdapter as C. In this case, the adapter displays the data in the String array, that is, those strings in the ListView. Line 22 function calls reflect this process.
The next key is to define the click on each item, that is, I want to share with our experience in using app store, and click an item, the item content is displayed on the next screen.
So here we set event listening for items for ListView, and line 26 implemented this setting. The onItemClick method is implemented in listener to switch to another Activity ---- WordActivity.
This program may be too simple. In one aspect, there is only one simple ListView. In the second aspect, there is only a few simple data in the ListView. Finally, after listening to the event, it simply switches to another Activity.
Why is it so simple? It is mainly because I am working on a prototype and the prototype does not need to be so complicated. Easy to control. In the final program, the setListAdapter can use other better methods, and there are two other adapters. Most importantly, the data source will certainly not be such an array. for data storage, a large amount of data can be stored in the database, and a small amount of data can be stored as files. Therefore, a dedicated class is responsible for data acquisition.
In general, there is no problem with the prototype, and the basic elements are also available. I learned all aspects of the important control ListView.
Finally, let's share the experience accumulated during the debugging process.
When writing a program, you can find an example to simulate it.
I found an example on the official website and imitated it. Basically, I am right, but unfortunately, it's only half right. As mentioned above, Activity and xml files are closely integrated. ListView is used in my code. I found a ListView example on the official website to simulate it. This was the first time I was writing code, didn't I? Unfortunately, there are no xml files on the official website. I can only write xml files based on my own understanding. A problem occurs when my program runs. In addition, the debug display is in the Activity of ListView. But it is still unknown where it is. You can see that the Code is in the line called by setViewAdapter, but you cannot see it in depth.
After a long attack, I chose to turn to the Internet. I searched a lot of examples using ListView on the Internet and analyzed them carefully to find out the differences with my programs. The Emperor finally found a similar example. Let's take a look, there should be no problem with the Java program. The difference lies in the xml file. Experience tells me that my xml file is faulty.
Look at my xml file, and add a LinearLayout, but the TextView. Good is added in the example, and then add a TextView according to the example. The result is all right.
Think about it later. Because the String in ListView needs to be displayed in TextView, if there is no TextView, there is no place to store the String data when the Adapter is used. After analysis, the result is that the TextView cannot be found in the setViewAdapter function. Therefore, an error occurs because the object is empty.
This experience makes me further realize that xml is a very important thing. In addition, it is very important to understand the basis of function dependencies, rather than simply calling them. You must thoroughly understand what is hidden behind them.
From love Jue Roche