Android -- ListView与Adapter

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。

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

如果需要自定义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;
        }

我是天王盖地虎的分割线                                                               

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

listview学习.zip

 

 

 

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

Android -- ListView与Adapter,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。