[转]android adapter 深刻分析

转帖地址,不知源头,留个脚印。

http://www.devdiv.com/blog/space-1-do-blog-id-1397.html

 

 

listview加载adapter过程是这样的.

1 先判断adapter 有多少数据项,根据这个数据确定有多少item. 
2 确定每个item里加载哪个View. 
3 把View里加载要显示的数据.

问提一个一个来解决. 第一个问题: 因为adapter都要关联一个list .有来存储数据.list的项数就是Item的数目. 我们在重载BaseAdapter 时候,都要实现这个函数

public int getCount() {                           
        return weatherList.size();   
    }   

哎,这个函数就是确定关联条目的.

第二个问题 哪来的view 呢, 当然我们自己创建的.重载BaseAdapter时候你要实现getView()这个函数,就是这个view.

第三个问题,你自己创建的view.加载哪些数据你该知道的.呵呵.

public class CustomAdapterActivity extends ListActivity   
{   
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState)   
    {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
        ArrayList<Weather> weatherList = new ArrayList<Weather>();   
        Weather w = new Weather( "London", 17, Weather.OVERCAST );   
        weatherList.add( w );   
        w = new Weather( "Paris", 22, Weather.OVERCAST );   
        weatherList.add( w );   
        w = new Weather( "Athens", 29, Weather.SUNNY );   
        weatherList.add( w );   
        w = new Weather( "Stockholm", 12, Weather.RAIN );   
        weatherList.add( w );   
        WeatherAdapter weatherAdapter = new WeatherAdapter(    
                this,   
                weatherList );    
        setListAdapter( weatherAdapter );   
    }   
}  

哎,这个大家都很清楚,关键问题是weatherAdapter 哪来的呢? 自己创建的啊,如果创建呢?

public class WeatherAdapter extends BaseAdapter {   
  
    private Context context;   
    private List<Weather> weatherList;    这就是adapter关联的List,用来存储数据.还记的ArrayList 要往里传参数吗? 传的也是这个类型啊.呵呵
  
    public WeatherAdapter(Context context, List<Weather> weatherList ) {    
        this.context = context;   
        this.weatherList = weatherList;   
    }   
  
    public int getCount() {                           
        return weatherList.size();   
    }   
  
    public Object getItem(int position) {        
        return weatherList.get(position);   
    }   
  
    public long getItemId(int position) {     
        return position;   
    }   
  
    public View getView(int position, View convertView, ViewGroup parent) {    
        Weather weather = weatherList.get(position);   
        return new WeatherAdapterView(this.context, weather );   
    }   
  
}  

哎,这段告诉了我们,有多少个Item, 可以通过getCount()得到了。 可是View 哪来的呢?
当然是getView ()这个函数提供.

这个view 的获取就多中多样了,我们可以传个LayoutID. 通过Inflater出来,也可以自己创建个,只要出来就行.

在这里,我们自己创建个View. 这个View.是个VIewGroup.


class WeatherAdapterView extends LinearLayout {           
        public static final String LOG_TAG = "WeatherAdapterView";   
  
        public WeatherAdapterView(Context context,    
                                Weather weather ) {   
            super( context );   
  
            this.setOrientation(HORIZONTAL);           
            LinearLayout.LayoutParams cityParams =    
                new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);   
            cityParams.setMargins(1, 1, 1, 1);   
  
            TextView cityControl = new TextView( context );   
            cityControl.setText( weather.getCity() );   
            addView( cityControl, cityParams);          
  
            LinearLayout.LayoutParams temperatureParams =    
                new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);   
            temperatureParams.setMargins(1, 1, 1, 1);   
  
            TextView temperatureControl = new TextView(context);   
            temperatureControl.setText( Integer.toString( weather.temperature ) );   
            addView( temperatureControl, temperatureParams);               
  
            LinearLayout.LayoutParams skyParams =    
                new LinearLayout.LayoutParams(25, LayoutParams.WRAP_CONTENT);   
  
            ImageView skyControl = new ImageView( context );   
            Log.d( LOG_TAG, weather.getCity()+" -> "+weather.sky );   
            skyControl.setImageResource( weather.getSkyResource() );   
            addView( skyControl, skyParams );   
        }   
}  

你可能感兴趣的:(android,Adapter)