Android ListView 使用

前台代码
<ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
ListView 中较简单但又非常方便的ArrayAdapter
        private ListView list1;
        private ArrayAdapter<people> adapter;                
        adapter = new ArrayAdapter<people>(this,
                android.R.layout.simple_list_item_1);
        list1 = (ListView) findViewById(R.id.list1);
        adapter.add(new people("野原新之助", "5"));
        adapter.add(new people("野原美伢", "26"));
        adapter.add(new people("野原广智", "28"));
        adapter.add(new people("野原葵", "2"));
        list1.setAdapter(adapter);
单击ListView事件

继承implements OnItemClickListener
list1.setOnItemClickListener(this);  
@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        System.out.println("listview");
        people p = adapter.getItem(position);
        Toast.makeText(this,
                String.format("名字:%s,年龄:%s岁", p.getName(), p.getAge()),
                Toast.LENGTH_SHORT).show();
    }

 

posted @ 2015-10-23 10:44  注定likeyou  阅读(332)  评论(0编辑  收藏  举报