解读ListView
android开发中listview的运用非常之多,下面介绍全面介绍下ListView。
列表的显示需要三个元素:
1.ListVeiw 用来展示列表的View。
2.适配器
用来把数据映射到ListView上的中介。
3.数据
具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter,这三种适配器使用起来都比较简单,这里就不做详细的介绍,主要介绍一下这三种适配器的适配。
- ArrayAdapter(Context context, int textViewResourceId,
List<T> objects)来装配数据 - SimpleCursorAdapter
构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:一个包含数据库的列的String型数组,一个包含布局文件中对应组件id的int型数组 - SimpleAdapter(Context context,List<Map<String,Object> mData,int
textViewResourceId, new String[]{}, new int[]{});
下面重点介绍自定义Adapter来适配ListView
- package com.cn.listview;
- import java.util.ArrayList;
import java.util.HashMap;
import
java.util.List;
import java.util.Map; - import android.app.AlertDialog;
import
android.app.ListActivity;
import android.content.Context;
import
android.content.DialogInterface;
import android.os.Bundle;
import
android.view.LayoutInflater;
import android.view.View;
import
android.view.View.OnClickListener;
import android.view.ViewGroup;
import
android.widget.*; - public class ListView04 extends ListActivity {
private
List<Map<String,Object>> mData;
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData=getData();
MyAdapter mAdapter=new MyAdapter(this);
setListAdapter(mAdapter);
}
private
List<Map<String,Object>> getData(){
List<Map<String,Object>> list=new
ArrayList<Map<String,Object>>();
Map<String,Object>
map=new HashMap<String,Object>();
map.put(“title”,
“title01″);
map.put(“info”, “info01″);
map.put(“img”,
R.drawable.icon);
list.add(map);
map=new
HashMap<String,Object>();
map.put(“title”, “title02″);
map.put(“info”, “info02″);
map.put(“img”, R.drawable.icon);
list.add(map);
return list;
}
public void
showInfo(){
new AlertDialog.Builder(this)
.setTitle(“myListView”)
.setMessage(“instruction”)
.setPositiveButton(“OK”, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int
arg1) {
}
}).show();
}
public final
class ViewHolder{
public ImageView img;
public TextView
title;
public TextView info;
public Button viewBtn;
}
public class MyAdapter extends BaseAdapter{
private
LayoutInflater mflater;
public MyAdapter(Context mContext){
this.mflater=LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated
method stub
return mData.size();
} - @Override
public Object getItem(int arg0) {
return null;
} - @Override
public long getItemId(int arg0) {
return 0;
} - @Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
ViewHolder holder=null;
if (arg1==null) {
holder=new
ViewHolder();
arg1=mflater.inflate(R.layout.main,
null);
holder.img=(ImageView)arg1.findViewById(R.id.img);
holder.title=(TextView)arg1.findViewById(R.id.title);
holder.info=(TextView)arg1.findViewById(R.id.info);
holder.viewBtn=(Button)arg1.findViewById(R.id.btn);
arg1.setTag(holder);
}else{
holder=(ViewHolder)arg1.getTag();
}
holder.img.setBackgroundResource((Integer)mData.get(arg0).get(“img”));
holder.title.setText((String)mData.get(arg0).get(“title”));
holder.info.setText((String)mData.get(arg0).get(“info”));
holder.viewBtn.setOnClickListener(new
btn_click());
return arg1;
}
class btn_click implements
OnClickListener{ - @Override
public void onClick(View arg0) {
// TODO
Auto-generated method stub
showInfo();
}
}
}
}
XML代码
- <?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<ImageView
android:id=”@+id/img”
android:layout_width=”45px”
android:layout_height=”45px”
/>
<TableLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:stretchColumns=”0″>
<TableRow>
<LinearLayout
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:gravity=”left”>
<TextView
android:textSize=”16px”
android:textColor=”#ffffff”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/title”
/>
<TextView android:textSize=”13px”
android:textColor=”#ffffff”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/info”
/>
</LinearLayout> - <Button android:id=”@+id/btn”
android:text=”@string/viewBtn”
android:layout_width=”65px”
android:layout_height=”wrap_content”
/>
</TableRow>
</TableLayout>
</LinearLayout>


浙公网安备 33010602011771号