BaseAdapter

TimeTrackerBaseAdapter
package com.example.timerecord;

import java.util.ArrayList;
import java.util.zip.Inflater;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TimeTrackerAdapter extends BaseAdapter {
    private ArrayList<TimeRecord> times=new ArrayList<TimeRecord>();
    private LayoutInflater inflater;
    private Context myContext;
    public TimeTrackerAdapter() {
        
        times.add(new TimeRecord("1:35", "Go to School"));
        times.add(new TimeRecord("2:35", "Go to School"));
        times.add(new TimeRecord("3:35", "Go to School"));
        times.add(new TimeRecord("4:35", "Go to School"));
        
    }

    public int getCount() {
        System.out.println("getCount"+times.size());
        return times.size();
    }

    public Object getItem(int position) {
        System.out.println("Object getItem"+position);
        return position;
    }

    public long getItemId(int position) {
        System.out.println("getItemId"+position);
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        //way1  it's a viewholder way to 
//        ViewHolder viewHolder=new ViewHolder();
//        if(convertView==null){
//            inflater=LayoutInflater.from(myContext);
//            convertView=inflater.inflate(R.layout.time_list_item, null);
//            viewHolder.times=(TextView)convertView.findViewById(R.id.time_view);
//            viewHolder.notes=(TextView)convertView.findViewById(R.id.notes_view);
//            convertView.setTag(viewHolder);
//        }else{
//            viewHolder=(ViewHolder)convertView.getTag();
//        }
//        viewHolder.notes.setText(times.get(position).getNotes());
//        viewHolder.times.setText(times.get(position).getTime());
        if(convertView==null){
            inflater=LayoutInflater.from(parent.getContext());
            convertView=inflater.inflate(R.layout.time_list_item,null);
            
        }
        TextView timeView=(TextView) convertView.findViewById(R.id.time_view);
        timeView.setText(times.get(position).getTime());
        TextView notesView=(TextView) convertView.findViewById(R.id.notes_view);
        notesView.setText(times.get(position).getNotes());
        return convertView;
    }
    private class ViewHolder{
        TextView times;
        TextView notes;
    }

}

先说一下baseadapter的工作原理:

1、主要是一次一次的获取getView然后显示到listView中的列表中,同时getView中有一个inflater.inflater(Resource id,null)的方法,可以根据XML文件膨胀出新的样子,然后你在下面获取相应的数据。就OK了

 

然后说一下改进的方法,具体效率我不大懂,但是据说效果不错,先标记下就是上面的ViewHolder方法~

posted @ 2012-07-20 14:18  Epirus  阅读(326)  评论(0编辑  收藏  举报