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

Android笔记(十)ListView

时间:2015-08-21 17:06:59      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:android   listview   

ListView是Android中最常用的控件之一,也是最难用的控件。
ListView的作用是展示大量的数据,不过ListView并不是直接和数据打交道,而是通过适配器作为中间桥梁。
1.首先在layout中建立ListView布局文件

<?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="match_parent" >

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

</LinearLayout>

2.然后,建立ListView的子项布局

<?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="match_parent" >

    <ImageView
        android:id="@+id/num_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/num_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dip" />

</LinearLayout>

3.接着定义一个实体类, 作为 ListView 适配器的适配类型

public class Number {
    private String name;
    private int imageId;

    public Number(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public String getName() {
        return name;
    }

    public int getImageId() {
        return imageId;
    }
}

4.建立适配器

public class NumberAdapter extends ArrayAdapter<Number> {

    private int rsId;

    public NumberAdapter(Context context, int itemid, List<Number> Numlist) {
        super(context, itemid, Numlist);
        rsId = itemid;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Number num = getItem(position);
        View view;
        ViewSave viewsave;//建立一个类保存ImageView和TextView的实例,不用每次都重新生成实例
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(rsId, null);
            viewsave = new ViewSave();
            viewsave.image = (ImageView) view.findViewById(R.id.num_image);
            viewsave.name = (TextView) view.findViewById(R.id.num_name);
            view.setTag(viewsave);
        } else {
            view = convertView;
            viewsave = (ViewSave) view.getTag();
        }
        viewsave.image.setImageResource(num.getImageId());
        viewsave.name.setText(num.getName());
        return view;
    }

    class ViewSave {
        ImageView image;
        TextView name;
    }

}

5.活动的代码

public class MainActivity extends ActionBarActivity {

    private List<Number> numlist = new ArrayList<Number>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_layout);
        initNumber();
        NumberAdapter adapter = new NumberAdapter(MainActivity.this,
                R.layout.item, numlist);
        ListView list = (ListView) findViewById(R.id.list_view);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            Number num = numlist.get(position);
            Toast.makeText(MainActivity.this, num.getName(),
            Toast.LENGTH_SHORT).show();
            }
            });
    }

    private void initNumber() {
        Number n1 = new Number("n1", R.drawable.n1);
        numlist.add(n1);
        Number n2 = new Number("n2", R.drawable.n2);
        numlist.add(n2);
        Number n3 = new Number("n3", R.drawable.n3);
        numlist.add(n3);
        Number n4 = new Number("n4", R.drawable.n4);
        numlist.add(n4);
        Number n5 = new Number("n5", R.drawable.n5);
        numlist.add(n5);
        Number n6 = new Number("n6", R.drawable.n6);
        numlist.add(n6);
        Number n7 = new Number("n7", R.drawable.n7);
        numlist.add(n7);
        Number n8 = new Number("n8", R.drawable.n8);
        numlist.add(n8);
        Number n9 = new Number("n9", R.drawable.n9);
        numlist.add(n9);
        Number n10 = new Number("n10", R.drawable.n10);
        numlist.add(n10);
             }
    }

6.最后实现的截图
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android笔记(十)ListView

标签:android   listview   

原文地址:http://blog.csdn.net/tomi_en/article/details/47837093

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