一、基礎知識:
ListView是一個經常用到的控制項,ListView裡面的每個子項Item可以使一個字串,也可以是一個群組控制項。先說說ListView的實現:
1.準備ListView要顯示的資料;
2.使用一維或多維動態數組儲存資料;
3.構建適配器,簡單地來說,適配器就是Item數組,動態數組有多少元素就產生多少個Item;
4.把適配器添加到ListView,並顯示出來。
二、代碼展示:
1."Activity_10srcyanactivity_10MainActivity.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);
// 產生動態數組,並且傳入資料
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);
}
//產生適配器,數組-->>ListItem
SimpleAdapter mSchedule = new SimpleAdapter(
this,
mylistArray, // 資料來源
R.layout.my_listview, // ListItem的XML實現
new String[] {raw_user_name,raw_user_id,raw_user_ip}, // 動態數組與ListItem對應的子項
new int[] {R.id.user_name,R.id.user_id,R.id.user_ip} // ListItem的XML檔案裡面的兩個TextView ID
);
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);
// 產生動態數組,並且傳入資料
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);
}
//產生適配器,數組-->>ListItem
SimpleAdapter mSchedule = new SimpleAdapter(
this,
mylistArray, // 資料來源
R.layout.my_listview, // ListItem的XML實現
new String[] {raw_user_name,raw_user_id,raw_user_ip}, // 動態數組與ListItem對應的子項
new int[] {R.id.user_name,R.id.user_id,R.id.user_ip} // ListItem的XML檔案裡面的兩個TextView ID
);
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_10reslayoutmain.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:drawSelectorOnTop="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:drawSelectorOnTop="false"
android:scrollbars="vertical"
/>
</LinearLayout>
3."Activity_10reslayoutmy_listview.xml" my_listview.xml用於設計ListView的Item:
[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>
三、效果展示: