Android笔记(十)ListView

时间:2024-08-14 14:04:02

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;
ViewHolder viewHolder;//建立一个类保存ImageView和TextView的实例,不用每次都又一次生成实例
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(rsId, null);
viewHolder = new ViewHolder();
viewHolder.image = (ImageView) view.findViewById(R.id.num_image);
viewHolder.name = (TextView) view.findViewById(R.id.num_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.image.setImageResource(num.getImageId());
viewHolder.name.setText(num.getName());
return view;
} class ViewHolder {
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