• android筆記--在ListView中顯示圖片及ListView的動態刷新

    0
    Android Java XML C/C++ Go 26593 次瀏覽

    最近在做一個天氣預報的小課題, 其中涉及到了ListView的動態刷新, 以及如何在ListView上顯示圖片.

     

    1. 在ListView上顯示圖片.

    為了實現這個功能, 首先需要定義一個布局文件, 用于顯示ListView的每個Item. 比如list_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:layout_width="fill_parent" android:layout_height="fill_parent">
    
    	<ImageView android:id="@+id/list_image"
    		android:layout_marginTop="6px" android:layout_marginBottom="6px"
    		android:layout_marginLeft="6px" android:layout_marginRight="6px"
    		android:layout_width="wrap_content" android:layout_height="wrap_content" />
    	<TextView android:id="@+id/list_text"
    		android:layout_marginLeft="6px" android:layout_marginRight="6px" android:layout_gravity="center_vertical"
    		android:layout_width="fill_parent" android:layout_height="wrap_content" />
    
    </LinearLayout>
    接下來需要選擇一個合適的adapter, 由于系統提供的adapter都無法滿足顯示圖片的需求(這點我并不確定), 因此在這里我自定義了一個BaseAdapter的子類:
    public class MyListAdapter extends BaseAdapter {
    	private Activity context;
    	private List<WeatherInfomation> list;
    
    	public MyListAdapter(Activity context, List<WeatherInfomation> list) {
    		this.context = context;
    		this.list = list;
    	}
    
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		LayoutInflater inflater = context.getLayoutInflater();
    		View itemView = inflater.inflate(R.layout.list_item, null);
    		WeatherInfomation info = list.get(position);
    		TextView textView = (TextView) itemView.findViewById(R.id.list_text);
    		ImageView imageView = (ImageView) itemView
    				.findViewById(R.id.list_image);
    		textView.setText(info.getWeatherText());
    		imageView.setImageBitmap(info.getWeatherBitmap());
    		return itemView;
    	}
    
    	@Override
    	public int getCount() {
    		return list.size();
    	}
    
    	@Override
    	public Object getItem(int position) {
    		return list.get(position);
    	}
    
    	@Override
    	public long getItemId(int position) {
    		return position;
    	}
    }
    可以看到, 定義BaseAdapter的子類需要重寫很多方法, 其他方法都不難, 關鍵就是getView()方法. getView()方法返回的itemView對象用來顯示ListView的一個Item. 使用LayoutInflater類的inflater方法可以從ist_item.xml 文件中解析出itemView對象, 但此時不可直接將itemView返回, 還需定義itemView中的ImageView和TextView的行為. 可以將itemView理解為ImageView和TextView的父控件. 源文件中涉及的WeatherInfomation是我定義的一個實體類, 代碼如下:
    public class WeatherInfomation {
    	/**
    	 * 代表天氣狀況的圖片
    	 */
    	private Bitmap weatherBitmap;
    	/**
    	 * 具體天氣的文字說明
    	 */
    	private String weatherText;
    
    	public WeatherInfomation(Bitmap weatherBitmap, String weatherText) {
    		super();
    		this.weatherBitmap = weatherBitmap;
    		this.weatherText = weatherText;
    	}
    
    	public Bitmap getWeatherBitmap() {
    		return weatherBitmap;
    	}
    
    	public void setWeatherBitmap(Bitmap weatherBitmap) {
    		this.weatherBitmap = weatherBitmap;
    	}
    
    	public String getWeatherText() {
    		return weatherText;
    	}
    
    	public void setWeatherText(String weatherText) {
    		this.weatherText = weatherText;
    	}
    
    }
    接下來只需要創建出MyListAdapter對象并調用listview的setAdapter()方法就可以:
    infomations = new ArrayList<WeatherInfomation>();
    		myListAdapter = new MyListAdapter(MainActivity.this, infomations);
    		listView.setAdapter(myListAdapter);
    這樣就可以在listview上顯示圖片了.

     

    2. 很多朋友都知道, 想要動態刷新listview中的內容, 只要調用在數據發生改變之后調用adapter的notifyDataSetChanged()方法就可以了. 這里需要注意的是所謂的"數據發生改變"到底指的是什么, 比如說如下的代碼:

    infomations = new ArrayList<WeatherInfomation>();
    infomations.add(new WeatherInfomation());
    此時是否滿足發生了數據改變的情形? 其實是沒有的!(這個問題導致我浪費了很長時間, 我怎么花了一天時間才找出無法動態刷新listview的原因, 希望可以讓看到的朋友避免這個問題). adapter在android中屬于MVC中的mode, 它是UI界面和數據之間的橋梁, 而此時的數據, 并不是只infomations, 而是infomations這個引用所指向的WeatherInfomation集合, 因此重新創建一個 ArrayList<WeatherInfomation>并將其賦值給infomations改變的只是infomations的值,

    而adapter關聯的數據并沒有發生變化. 后來我的處理代碼是:

    infomations.clear();
    infomations.add(new WeatherInfomation());
    這樣就真正改變了adapter所關聯的數據了.

    轉自:http://coolxing.iteye.com/blog/1207946

    相似問題

    相關經驗

    相關資訊

    相關文檔

  • sesese色