自定义Adapter要实现 BaseAdapter的四个方法,其中,为了是listView不卡死,用了两个新的知识1,convertview,2,ViewHolder
package com.example.listviewshili;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class jiazaiActivity extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.jiazai);
lv=(ListView) findViewById(R.id.listView1);
Myadapter adapter=new Myadapter(this);
lv.setAdapter(adapter);
}
使用自定义的adapter
class Myadapter extends BaseAdapter{
//填充图编标题--------------------------listView中的关于文字
private String[]titles={"title1","title2","title3","title4","title5"};
//填充图编标题--------------------------listView中的关于图片
android.R.drawable.alert_dark_frame,
android.R.drawable.arrow_up_float,
android.R.drawable.btn_default_small,
android.R.drawable.btn_dialog,
android.R.drawable.btn_radio};
//构造方法
Context context;
ViewHolder holder;
public Myadapter(Context context)
{
this.context=context;
// TODO Auto-generated constructor stub
}
@Override
//个数
public int getCount() {
// TODO Auto-generated method stub
return titles.length;
}
//返回的目标
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return titles[position];
}
//返回位置
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//得到控件的视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
holder=new ViewHolder();
//convertview和ViewHolder的目的是加快速度,不卡机
if( convertView==null)
{//得到一个inflater对象,为获得布局做准备
LayoutInflater inflater=LayoutInflater.from(context);
holder=new ViewHolder();
//获得布局
convertView=inflater.inflate(R.layout.init2, null);
//取得控件
holder.tv_titiles=(TextView) convertView.findViewById(R.id.textView1);holder.iv_ipc=(ImageView) convertView.findViewById(R.id.imageView1);
//做标记,记录第一搜到控件
convertView.setTag(holder);
}else
{
holder=(ViewHolder) convertView.getTag();
}
holder.tv_titiles.setText(titles[position]);
holder.iv_ipc.setImageResource(ipc[position]);
return convertView;
}
}
static class ViewHolder{
ImageView iv_ipc;
TextView tv_titiles;
}
}