`
wang_peng1
  • 浏览: 3905319 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

listView中继承BaseAdapter并且扩展LinearLayout

阅读更多

定义一个对象:

public class Weather {
  public static final int NA = -1;
  public static final int SUNNY = 0;
  public static final int OVERCAST = 1;
  public static final int RAIN = 2;

  public String city = null;
  public int temperature = 0;
  public int sky = NA;

  public Weather( String city, int temperature, int sky ) {
    this.city = city;
    this.temperature = temperature;
    this.sky = sky;
  }

  public String getCity() {
	return city;
  }

  public int getTemperature() {
	return temperature;
  }

  public int getSkyResource() {
	switch( sky ) {
	  case SUNNY:
			return R.drawable.weather_sunny;

	  case OVERCAST:
			return R.drawable.weather_overcast;

	  case RAIN:
			return R.drawable.weather_rain;
	}
	return R.drawable.unknown;
  }
}

 

主activity

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

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.setTextAppearance( context, R.style.SpecialText );
			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 );
        }
}

public class WeatherAdapter extends BaseAdapter {

    private Context context;
    private List<Weather> weatherList;

    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 );
    }

}

 

通过定义一个对象 在这个对象中包含要显示的东西,这样就不用使用simpleAdapater了,不同的方法自己选择吧。

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <style name="MyTheme" parent="android:Theme.Light">  
        <item name="android:listViewStyle">@style/MyListView</item>  
    </style>  

    <style name="SpecialText" parent="@android:style/TextAppearance">
        <item name="android:textSize">18sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#008</item>
    </style>

    <style name="MyListView" parent="@android:style/Widget.ListView">  
        <item name="android:background">@color/opaque_red</item>
        <item name="android:listSelector">@drawable/z_selector_background</item>
    </style>
</resources>

 

别忘了在主xml中加入上面的主题。

源文件上传了 这里面也使用到了selector

分享到:
评论

相关推荐

    BaseAdapter的使用与优化

    此文件为Eclipse下的android工程,里面介绍了listView的三种适配方式,从实现到优化。

    Android代码-all-base-adapter

    现在也加入了DataBinding用的BaseAdapter。 Related posts: 相关博文: ViewGroup 篇 封装博文 DataBinding 篇 封装博文 If you like, point a star .Thank you very much! 喜欢随手点个star 多谢 The ultimate

    Android ListView填充数据的方法

    Android ListView填充数据的方法 因为多人开发,为了是自己开发的...为了给ListView提供数据,我们需要为其设置一个适配,我们可以从BaseAdapter继承,然后重写它的getView方法,这个方法中有一个参数convertView,我们

    Android通过LIstView显示文件列表的两种方法介绍

    在Android中通过ListView显示SD卡中的文件列表一共有两种方法,一是:通过继承ListActivity显示;二是:利用BaseAdapter显示。BaseAdapter是一个公共基类适配器,用于对ListView和Spinner等 一些控件提供显示数据。...

    一个物联网(IoT)开发的入门教程 涉及单片机、上位机、移动应用、服务器后台开发的知识 以及蓝牙4.0、以太网模块的使用实例

    一个物联网(IoT)开发的入门教程。涉及单片机、上位机、移动应用、服务器后台开发的知识。...- 3.5 ListView与BaseAdapter - 3.6 Service学习 - 3.7 Android权限机制 - 3.8 BroadCastReciever学习 第

    Android ListView自定义Adapter实现仿QQ界面

    listview中有一些简单使用的适配器,如:SimpleAdapter:构造方法SimpleAdapter(Context context,List&lt;Map&gt; data,reString [] from,int [] to),但这种适配器过于单调,往往不能达到用户想要的效果,想要随心所欲,就...

    Android 腾讯微博客户端源码.zip

    4:布局方面的当然是最常用的ReleativeLayout,LinearLayout,FrameLayout,include,merge的应用 5:Menu菜单的使用 6:弹出菜单的使用,简单的弹出框,包含list的弹出框. 7:webview嵌入腾讯第三方授权页面的使用 8:最...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 ...

    Google Android SDK开发范例大全的目录

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

    Google+Android+SDK开发范例大全

    4.18 动态文字排版——GridView与ArrayAdapter设计 4.19 在Activity里显示列表列表——ListView的布局 4.20 以动态列表配置选项——ListActivity与Menu整合技巧 4.21 查找程序根目录下所有文件——JavaI/O与...

    Google Android sdk 开发范例大全 部分章节代码

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

    Google Android SDK 开发范例大全01

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

    Google Android SDK 开发范例大全02

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

    Google Android SDK开发范例大全(完整版)

    8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过...

Global site tag (gtag.js) - Google Analytics