@Overrideprotected void onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);                                                                                                                                  final ArrayList
title = new ArrayList
(); for (int i = 0; i < 20; i++) { title.add("标题" + i); } ListView listView = (ListView) findViewById(R.id.listView1); listView.setAdapter(new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent)//执行数等于当前页面出现的行数,convertView指滚动消失的layout { Log.e("getView", "调用getView"+ i++ + "convertView:" + convertView); LayoutInflater inf = getLayoutInflater();//通过LayoutInflater将布局id转换到对象 RelativeLayout relativeLayout = (RelativeLayout) inf.inflate(R.layout.relat, null); TextView titleview = (TextView) relativeLayout.findViewById(R.id.textView1);//通过布局找到里面的控件 titleview.setText(title.get(position));//得到容器内的内容 return relativeLayout; } @Override public long getItemId(int position) { return 0; } @Override public Object getItem(int position) { return null; } @Override //先执行getCount再执行getView,出现新行数时再次执行getCount //listview清单里要设置高度match_parent //每次点击屏幕一次都会先执行getCount public int getCount() { Log.e("getCount", "调用getCount"+ j++); return title.size(); } });}

复用converview:

ListView listView = (ListView) findViewById(R.id.listView1);listView.setAdapter(new BaseAdapter(){    @Override    public View getView(int position, View convertView, ViewGroup parent)//执行数等于当前页面出现的行数,convertView指滚动消失的layout    {                 //复用converview        RelativeLayout relativeLayout = null;        if(convertView == null)        {            LayoutInflater inf = getLayoutInflater();            relativeLayout = (RelativeLayout) inf.inflate(R.layout.relat, null);        }        else        {            relativeLayout = (RelativeLayout) convertView;        }                                                                                                                          TextView titleView = (TextView) relativeLayout.findViewById(R.id.textView1);        titleView.setText(title.get(position));        return relativeLayout;    }

继续优化:

public class MainActivity extends Activity{    int imID = 0;    class Msg//内容    {        String mTitle;        String mSubtitle;        String mTime;        int mImagID;        public Msg(String mTitle, String mSubtitle, String mTime, int mImagID)        {            super();            this.mTitle = mTitle;            this.mSubtitle = mSubtitle;            this.mTime = mTime;            this.mImagID = mImagID;        }                                                                                                               }                                                                                                           class ViewHolder//控件    {        TextView viewTitle;        TextView viewSubtitle;        TextView viewTime;        ImageView viewImage;    }    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                                                                                                                   Date timeNow = new Date();        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");        String time = timeFormat.format(timeNow);                                                                                                                   final ArrayList
msgList = new ArrayList
(); for (int i = 0; i < 30; i++)//选择图片 { if(i % 4 == 0) { imID = R.drawable.ic_launcher; } if(i % 4 == 1) { imID = R.drawable.ic_1; } if(i % 4 == 2) { imID = R.drawable.ic_2; } if(i % 4 == 3) { imID = R.drawable.ic_3; } Msg msg = new Msg("标题" + i, "内容" + i, time ,imID); msgList.add(msg); } ListView listView = (ListView) findViewById(R.id.listView1); listView.setAdapter(new BaseAdapter() { ViewHolder holder = null; @Override public View getView(int position, View convertView, ViewGroup parent) { RelativeLayout relout = null; if (convertView == null)//创建首页时才find控件 { LayoutInflater layoutInflater =getLayoutInflater(); relout = (RelativeLayout) layoutInflater.inflate(R.layout.relaout, null); holder = new ViewHolder(); holder.viewTitle= (TextView) relout.findViewById(R.id.textView_title); holder.viewSubtitle = (TextView) relout.findViewById(R.id.textView_subtitle); holder.viewTime = (TextView) relout.findViewById(R.id.textView_time); holder.viewImage = (ImageView) relout.findViewById(R.id.p_w_picpathView1); relout.setTag(holder); } else//否则复用layout和view { relout = (RelativeLayout) convertView; holder = (ViewHolder) relout.getTag();//记得得到holder } Msg msg = msgList.get(position); holder.viewTitle.setText(msg.mTitle); holder.viewSubtitle.setText(msg.mSubtitle); holder.viewTime.setText(msg.mTime); holder.viewImage.setImageResource(msg.mImagID);//设置图片的方法 return relout; } @Override public long getItemId(int position) { return 0; } @Override public Object getItem(int position) { return null; } @Override public int getCount() { return msgList.size(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }

点击按钮更新listview:Adapter使用notifyDataSetChanged()通知listview更新

3
private void btn1Click()//增加一行信息{    i++;    data.add("内容" + i );    arrAdapter.notifyDataSetChanged();//Adapter通知listview做更新}private void btn2Click()//删除第一行信息{    if(data.size() < 1)    {       Toast.makeText(this, "信息已删除完毕!", Toast.LENGTH_LONG).show();    }    else    {        data.remove(0);        arrAdapter.notifyDataSetChanged();//Adapter通知listview做更新    }}private void btnsortClick()//对列表进行排序{    Collections.sort(data);    arrAdapter.notifyDataSetChanged();}

注:

1.LayoutInflater inflater = getLayoutInflater();

 layout = (RelativeLayout) inflater.inflate(R.layout.relayout, null);

 layout要通过LayoutInflater拿到。

2.getView返回layout,getCount()返回int。

3.layout.getTag()要转换为holder = (ViewHolder)。