码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- ListView与Adapter

时间:2014-06-20 21:54:18      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:android   style   class   blog   code   http   

ListView在Android中有着很重要的作用。Android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

背景                                                                                          

建了个Person类,里面有Name,Number,id,三个属性。

private String name;
    private String number;
    private int id;

主要用来向listView中添加信息的。

布局                                                                                            

<LinearLayout 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" 
    android:orientation="vertical">

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

</LinearLayout>

直接放listview上去就OK了。

程序                                                                                           

private ListView lv;
   private List<Person> list;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<Person>();
        lv = (ListView) findViewById(R.id.lv);
        addPerson();
        lv.setAdapter(new MyAdapter());
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            //返回大小
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO 自动生成的方法存根
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO 自动生成的方法存根
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = new TextView(getApplicationContext());
            tv.setTextSize(50);
            tv.setTextColor(Color.BLUE);
            Person person = list.get(position);
            tv.setText(person.toString());
            System.out.println("返回位置"+position);
            return tv
        }

    }

    // 添加数据函数
    private void addPerson() {

        for (int i = 0; i < 20; i++) {
            Person person1 = new Person("张三" + i, "12345678912", i);
            list.add(person1);
        }
    }

要申明一个adapter,adapter里面放数据,然后listview通过setAdapter配置adapter。

bubuko.com,布布扣

----------------------------简单的分割线------------------------------------简单的---------------------------

如果需要自定义lixtview中当样式的话,可以仙剑一个布局item的布局。

item布局                                                                                  

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

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:text="id"
        android:textColor="#ff0000"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >
        
        <TextView 
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="名字"
            android:textColor="#000000"
            android:textSize="18sp"/>
        <TextView 
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:text="电话"
            android:textColor="#88000000"
            android:textSize="16sp"/>

    </LinearLayout>

</LinearLayout>

重新写一下adapter里面的getView方法:

getView                                                                                    

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Person person = list.get(position);
            View view = View.inflate(MainActivity.this, R.layout.listview_item, null);
            //找id
            TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
            tv_id.setText("id:"+person.getId());
            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_name.setText("tv_name:"+person.getName());
            TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
            tv_phone.setText("tv_phone:"+person.getNumber());
            return view;
        }

bubuko.com,布布扣

我是天王盖地虎的分割线                                                               

源代码:http://pan.baidu.com/s/1dD1Qx01

listview学习.zip

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut

Android -- ListView与Adapter,布布扣,bubuko.com

Android -- ListView与Adapter

标签:android   style   class   blog   code   http   

原文地址:http://www.cnblogs.com/yydcdut/p/3790828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!