Today, summarize the list controls in Android: ListView and Spinner.
ListView can display some information vertically and in a sliding way. The following explains how to display a series of information using ListView.
1. Create a project: BASICVIEWS5.
2. Code in the Strings.xml.
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" >hello world, basicviews5activity!</string>
<string name= "app_name" >BasicViews5</string>
< String-array name= "Presidents_array" >
<item>dwight D. eisenhower</item>
<item>john F. Kennedy</item>
<item>lyndon B. johnson</item>
<item>richard Nixon</item>
<item>gerald ford</item>
<item>jimmy carter</item>
<item>ronald Reagan</item>
<item>george H. bush</item>
<item>bill clinton</item>
<item>george W. bush</item>
<item>barack obama</item>
</string-array >
</resources>
3. Code in the Basicviews5activity.java.
Package net.learn2develop.BasicViews5;
Import android.app.ListActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.Toast;
public class Basicviews5activity extends Listactivity {string[] presidents; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
---No need to call this---//setcontentview (R.layout.main);
Presidents = Getresources (). Getstringarray (R.array.presidents_array); Setlistadapter New Arrayadapter<string> (this, Android.
R.layout.simple_list_item_checked, Presidents)); } public void Onlistitemclick (ListvieW Parent, View v, int position, long id) {Toast.maketext (this, "for you have selected
"+ presidents[position], toast.length_short). Show (); }
}
4. Press F11 to Debug.
5. Click on each line of information, there will be information pop-up.
First of all, it will be noted that the Basicviews5activity class is inherited listactivity. Listactivity is also inherited from the activity class, and ListView can display data from the bound data source. Therefore, it is not necessary to modify the Main.xml file to include a ListView, because Listactivity itself contains a listview. Then, there is no need to invoke Setcontentview () in the OnCreate () callback.
In the OnCreate () callback function, we use the Setlistadapter () method to manually let a ListView fill the entire activity.
The Arrayadapter class manages those displayed strings, and in this case we let ListView display the data using the Simple_list_item_1 method:
Setlistadapter New Arrayadapter<string> (this,
Android. R.layout.simple_list_item_checked, Presidents));
Finally, the Onlistitemclick () method is overridden, and the method is triggered when the data in the ListView is clicked:
public void Onlistitemclick (
ListView Parent, View v., int position, long id)
{
Toast.maketext (this,
" You are have selected "+ presidents[position],
toast.length_short). Show ();
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/