I. Basic Knowledge:
ListView is a commonly used control. Each Item in the ListView can be a string or a combination control. First, let's talk about the implementation of ListView:
1. Prepare the data to be displayed in ListView;
2. Use a one-dimensional or multi-dimensional dynamic array to save data;
3. Build an adapter. In simple terms, the adapter is an Item array, and the number of items is generated when the dynamic array has many elements;
4. Add the adapter to the ListView and display it.
Ii. Code display:
1. "Activity_10 \ src \ yan \ activity_10 \ MainActivity. java"
[Java]
Package yan. activity_10;
Import java. util. ArrayList;
Import java. util. HashMap;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. ListView;
Import android. widget. SimpleAdapter;
Import android. app. ListActivity;
Public class MainActivity extends ListActivity {
Private final String raw_user_name = "user_name ";
Private final String raw_user_id = "user_id ";
Private final String raw_user_ip = "user_ip ";
Private String user_name_array [] = {"zhangsan", "lisi", "wangwu "};
Private String user_ip_array [] = {
"192.168.1.115 ",
"192.168.1.116 ",
"192.168.1.117 "};
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Generate a dynamic array and input data
ArrayList <HashMap <String, String> mylistArray = new ArrayList <HashMap <String, String> ();
For (int I = 0; I <30; ++ I)
{
HashMap <String, String> map = new HashMap <String, String> ();
System. out. println ("HAH:" + I % 3 );
Map. put (raw_user_name, user_name_array [I % 3]);
Map. put (raw_user_id, I + 1 + "");
Map. put (raw_user_ip, user_ip_array [I % 3]);
MylistArray. add (map );
}
// Generate the adapter, array --> ListItem
SimpleAdapter mSchedule = new SimpleAdapter (
This,
MylistArray, // Data Source
R. layout. my_listview, // XML Implementation of ListItem
New String [] {raw_user_name, raw_user_id, raw_user_ip}, // subitem corresponding to the dynamic array and ListItem
New int [] {R. id. user_name, R. id. user_id, R. id. user_ip} // two TextView IDs in the XML file of ListItem
);
SetListAdapter (mSchedule );
}
@ Override
Protected void onListItemClick (ListView l, View v, int position, long id ){
// TODO Auto-generated method stub
Super. onListItemClick (l, v, position, id );
System. out. println ("id ----------------" + id );
System. out. println ("position ----------------" + position );
}
}
Package yan. activity_10;
Import java. util. ArrayList;
Import java. util. HashMap;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. ListView;
Import android. widget. SimpleAdapter;
Import android. app. ListActivity;
Public class MainActivity extends ListActivity {
Private final String raw_user_name = "user_name ";
Private final String raw_user_id = "user_id ";
Private final String raw_user_ip = "user_ip ";
Private String user_name_array [] = {"zhangsan", "lisi", "wangwu "};
Private String user_ip_array [] = {
"192.168.1.115 ",
"192.168.1.116 ",
"192.168.1.117 "};
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Generate a dynamic array and input data
ArrayList <HashMap <String, String> mylistArray = new ArrayList <HashMap <String, String> ();
For (int I = 0; I <30; ++ I)
{
HashMap <String, String> map = new HashMap <String, String> ();
System. out. println ("HAH:" + I % 3 );
Map. put (raw_user_name, user_name_array [I % 3]);
Map. put (raw_user_id, I + 1 + "");
Map. put (raw_user_ip, user_ip_array [I % 3]);
MylistArray. add (map );
}
// Generate the adapter, array --> ListItem
SimpleAdapter mSchedule = new SimpleAdapter (
This,
MylistArray, // Data Source
R. layout. my_listview, // XML Implementation of ListItem
New String [] {raw_user_name, raw_user_id, raw_user_ip}, // subitem corresponding to the dynamic array and ListItem
New int [] {R. id. user_name, R. id. user_id, R. id. user_ip} // two TextView IDs in the XML file of ListItem
);
SetListAdapter (mSchedule );
}
@ Override
Protected void onListItemClick (ListView l, View v, int position, long id ){
// TODO Auto-generated method stub
Super. onListItemClick (l, v, position, id );
System. out. println ("id ----------------" + id );
System. out. println ("position ----------------" + position );
}
}
2. "Activity_10 \ res \ layout \ main. xml"
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
<ListView
Android: id = "@ + id/android: list"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: drawselectid Top = "false"
Android: scrollbars = "vertical"
/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
<ListView
Android: id = "@ + id/android: list"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: drawselectid Top = "false"
Android: scrollbars = "vertical"
/>
</LinearLayout>
3. "Activity_10 \ res \ layout \ my_listview.xml" my_listview.xml is used to design the items of ListView:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal"
Android: paddingBottom = "3dip"
Android: paddingTop = "1dip"
Android: paddingLeft = "10dip">
<TextView
Android: id = "@ + id/user_name"
Android: layout_width = "100dp"
Android: layout_height = "30dp"
Android: textSize = "10pt"
Android: singleLine = "true"/>
<TextView
Android: id = "@ + id/user_id"
Android: layout_width = "100dp"
Android: layout_height = "30dp"
Android: textSize = "10pt"
Android: singleLine = "true"/>
<TextView
Android: id = "@ + id/user_ip"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: gravity = "right"
Android: textSize = "10pt"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "horizontal"
Android: paddingBottom = "3dip"
Android: paddingTop = "1dip"
Android: paddingLeft = "10dip">
<TextView
Android: id = "@ + id/user_name"
Android: layout_width = "100dp"
Android: layout_height = "30dp"
Android: textSize = "10pt"
Android: singleLine = "true"/>
<TextView
Android: id = "@ + id/user_id"
Android: layout_width = "100dp"
Android: layout_height = "30dp"
Android: textSize = "10pt"
Android: singleLine = "true"/>
<TextView
Android: id = "@ + id/user_ip"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: gravity = "right"
Android: textSize = "10pt"/>
</LinearLayout>
Iii. effect display: