ListView

来源:互联网 发布:联赢激光怎么样 知乎 编辑:程序博客网 时间:2024/05/20 14:28

首先推荐一个博客点击打开链接里面讲的很是清楚,按上面写的 一步一步来很简单的,(构造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
原创粉丝点击