[Android开发学习28]界面控件之列表控件(ListView)--显示字符数组
一、基础知识:
ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。先说说ListView的实现:
1.准备ListView要显示的数据;
2.使用一维或易做图动态数组保存数据;
3.构建适配器,简单地来说,适配器就是Item数组,动态数组有多少元素就生成多少个Item;
4.把适配器添加到ListView,并显示出来。
二、代码展示:
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);
// 生成动态数组,并且传入数据
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(List
补充:移动开发 , Android ,