当前位置: 代码迷 >> Android >> Android札记(十)ListView
  详细解决方案

Android札记(十)ListView

热度:54   发布时间:2016-04-27 23:20:43.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.最后实现的截图
这里写图片描述

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

1楼chenjianhui0915昨天 17:36
很好,适合初学者。就是命名突然变成ViewSave有点违和感啊!
  相关解决方案