ListView 控件可使用四种不同视图显示项目。通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本。 可使用 ListView 控件将称作 ListItem 对象的列表条目组织成下列四种不同的视图之一:1.大(标准)图标2.小图标3.列表4.报表 View 属性决定在列表中控件使用何种视图显示项目。还可用 LabelWrap 属性控制列表中与项目关联的标签是否可换行显示。另外,还可管理列表中项目的排序方法和选定项目的外观。
下面我主要是对报表样式的视图进行示例了。
JAVA code
package com.example.module;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListViewTest extends ListActivity {
ArrayList<HashMap<String, String>> list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
map1.put("user_name", "zhangsan");
map1.put("user_ip", "192.168.1.1");
map2.put("user_name", "zhanger");
map2.put("user_ip", "192.168.1.2");
map3.put("user_name", "zhangsi");
map3.put("user_ip", "192.168.1.3");
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[] { "user_name", "user_ip" },
new int[] { R.id.user_name, R.id.user_ip });
setListAdapter(listAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {// 点击list触发的方法
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
StringBuffer test = new StringBuffer("user_name=");
test.append(list.get(position).get("user_name") + "\nuser_ip=");
test.append(list.get(position).get("user_ip") + "");
Toast toast = Toast.makeText(v.getContext(), "你点击了第" + position
+ "项,内容:\n" + test, Toast.LENGTH_SHORT);
toast.show();
Log.i("ListView",
"ListView=" + l.getId() + " || View=" + v.getContext());
Log.i("ListView", "position=" + position + " || id=" + id);
}
}
user.xml
<?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="1dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="14dip" >
<TextView
android:id="@+id/user_name"
android:layout_width="120dip"
android:layout_height="30dip"
android:singleLine="true"
android:textSize="10pt" >
</TextView>
<TextView
android:id="@+id/user_ip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:textSize="10pt" >
</TextView>
</LinearLayout>
listview.xml
<?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="vertical" >
<LinearLayout
android:id="@+id/listViewL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical">
</ListView>
</LinearLayout>
</LinearLayout>
Comments (暂无评论)