ListView显示信息列表

来源:互联网 发布:老电视接网络电视 编辑:程序博客网 时间:2024/04/28 03:45

在主布局文件中声明一个ListView控件

<ListView    android:id="@+id/listView1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    ></ListView>

在Main.activity中声明并获取此对象

private ListView listView;listView = (ListView) findViewById(R.id.listView1);

定义ListView中每一行的布局样式

item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/appIcon"        android:layout_width="100dp"        android:layout_height="100dp"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:scaleType="fitXY"        android:src="@drawable/ic_launcher"/>    <TextView        android:id="@+id/appName"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_toRightOf="@+id/appIcon"        android:text="AppName"        android:textSize="24sp" />    <TextView        android:id="@+id/appPackage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/appName"        android:layout_below="@+id/appName"        android:text="package"        android:textAppearance="?android:attr/textAppearanceMedium" />    <TextView        android:id="@+id/appSize"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/appPackage"        android:layout_toRightOf="@+id/appIcon"        android:text="TextView" /></RelativeLayout>

定义数据源

/**数据源的格式是一个List集合在此集合中,*每一项又是一个Map集合,*此Map泛型定义为String,Object*/private List<Map<String, Object>> data;/**此List即为上面Map中的object*此处的AppBean是获取手机中已安装的所有应用信息*/private ArrayList<AppBean> src;

定义适配器

ArrayAdapter 主要用于显示简单的字符串信息

要实现相对复杂的布局样式,需使用SimpleAdapter

 simpleAdapter = new SimpleAdapter(    this,     data,     R.layout.item,    new String[] { "appIcon", "appName", "appPackage", "appSize" },    new int[] { R.id.appIcon, R.id.appName, R.id.appPackage, R.id.appSize }    );android.widget.SimpleAdapter.SimpleAdapter(    Context context, //上下文    List<? extends Map<String, ?>> data, //数据源    int resource, //每一行的布局文件    //以下两个参数主要用于Map映射    String[] from, //Map中的每一个Key    int[] to//布局文件中对应的id,Map中的Object)

为ListView绑定适配器

listView.setAdapter(simpleAdapter);

为ListView 绑定监听器

public class MainActivity extends Activity     implements OnScrollListener, OnItemClickListenerlistView.setOnScrollListener(this);listView.setOnItemClickListener(this);@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {    // TODO Auto-generated method stub    //此监听器中的此方法可实现动态下拉刷新    if (scrollState == SCROLL_STATE_IDLE) {        if (view.getLastVisiblePosition() == view.getCount() - 1) {            Toast.makeText(this, "继续向下滑动刷新列表", Toast.LENGTH_SHORT).show();            Map<String, Object> temp = new HashMap<String, Object>();            AppBean ap = src.get(0);            temp.put("appIcon", ap.getAppIcon());            temp.put("appName", ap.getAppName());            temp.put("appPackage", ap.getAppPackageName());            temp.put("appSize", ap.getAppSizeM());            data.add(temp);            simpleAdapter.notifyDataSetChanged();            listView.setSelection(simpleAdapter.getCount()-1);        }    }}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {    // TODO Auto-generated method stub}@Overridepublic void onItemClick(    AdapterView<?> parent,View view,int position,long id) {    // TODO Auto-generated method stub    Map<String,Object> temp = (Map<String, Object>) listView.getItemAtPosition(position);    Toast.makeText(this,"已选中:"+temp.get("appName"),Toast.LENGTH_SHORT).show();}

如何自定义布局文件中对数据源中对数据的使用方式

SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {    @Override    public boolean setViewValue(View view, Object data, String textRepresentation) {        // TODO Auto-generated method stub        if (view instanceof ImageView) {            ImageView iv = (ImageView) view;            iv.setImageDrawable((Drawable) data);            return true;        }        return false;    }};simpleAdapter.setViewBinder(viewBinder);
0 0