ListView

首先推荐一个博客点击打开链接里面讲的很是清楚,按上面写的 一步一步来很简单的,(构造SimpleAdapter对象这一块比较重要,我感觉)贴一下我写的代码:

public class MainActivity extends ActionBarActivity {
    private List<HashMap<String, Object>> list= new ArrayList<>();
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<20;i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemTitle", "订单"+i);
            map.put("ItemText", "用户姓名");
            map.put("ItemText1", "用户手机号");
            list.add(map);
        }
        lv=(ListView)findViewById(R.id.lv);
        SimpleAdapter mSimpleAdapter = new SimpleAdapter(
                this, list,//需要绑定的数据
                R.layout.item,//每一行的布局
                //动态数组中的数据源的键对应到定义布局的View中
                new String[] {"ItemTitle", "ItemText","ItemText1"},
                new int[] {R.id.ItemTitle,R.id.ItemText,R.id.ItemText1}
    );
        lv.setAdapter(mSimpleAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                setTitle("你点击了第" + arg2 + "行");//设置标题栏显示点击的行
                Intent intent=new Intent();
               intent.putExtra("num",String.valueOf(arg2));
                intent.setClass(MainActivity.this,DetailActivity.class);
                startActivity(intent);
            }
        });
    }

要建两个布局文件,一个是关于ListView控件的,另外一个是列表每行的布局文件,要自己在layout下新建一个布局文件,命名随意,只要对应构造适配器就行,我的这个布局文件叫item.xml

下面这个是ListView布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</RelativeLayout>

下面这个是新建的item.xml布局文件,其中的TextView用来显示内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/ItemTitle"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="haha"
        android:textSize="20sp"/>

    <TextView android:id="@+id/ItemText"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="hehe"
        android:layout_below="@+id/ItemTitle"/>
    <TextView android:id="@+id/ItemText1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="hehe"
        android:layout_below="@+id/ItemTitle"
        android:layout_toRightOf="@+id/ItemText"/>
</RelativeLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值