ListView显示信息列表

在主布局文件中声明一个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, OnItemClickListener

listView.setOnScrollListener(this);
listView.setOnItemClickListener(this);

@Override
public 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);
        }
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub

}

@Override
public 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值