首页 > 程序开发 > 移动开发 > Android >

[Android开发学习28]界面控件之列表控件(ListView)--显示字符数组

2013-04-06

一、基础知识:ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。先说说ListView的实现:1.准备ListView要显示的数据;2.使用一维或多维动态数组保存数据;3.构...

一、基础知识:

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(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: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_10\res\layout\my_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>

三、效果展示:

\

相关文章
最新文章
热点推荐