一、基礎知識:
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