【Android】Android適配器的應用

一、適配器簡介

  • 適配器就是管理數據, 控件就是展示數據
  • 數據的來源和子視圖均由適配器控制
  • 代碼展示
1. SimpleCursorAdapter(Context context, int childLayout, Cursor c, String[] from, int[] to)
2. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	android.R.layout.simple_list_item_1, new String[]{"Dave", "Satya", "Dylan"});
3. ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
	.array.planets, android.R.layout.simple_spinner_item);

二、適配器ArrayAdapter和微調框Spinner示例


1.  代碼展示

視圖xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"  android:layout_height="fill_parent"
    >
<!--  android:prompt列表選擇的提示 -->
<Spinner android:id="@+id/spinner"  android:prompt="@string/spinnerprompt"
    android:layout_width="wrap_content"  android:layout_height="wrap_content" />
</LinearLayout>
 
數據:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="planets">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
  </string-array>
</resources>

public class SpinnerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner);
 
        Spinner spinner = (Spinner)findViewById(R.id.spinner);
 
	<!-- 第一個佈局 -->
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
                R.array.planets, android.R.layout.simple_spinner_item);
 
	<!-- 選擇之後,彈出的列表的佈局 -->
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 
        spinner.setAdapter(adapter);
    }
}

2. 效果圖




二、自定義適配器和Gallery示例


1. 代碼展示

主視圖
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/gallery.xml -->
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
 
子視圖
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#555"
    android:scaleType="centerInside"
    android:padding="5dip"
    android:maxHeight="50dip"
    android:maxWidth="50dip"
  />

public class GalleryActivity extends Activity {
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.gallery);
 
	        Gallery gallery = (Gallery)findViewById(R.id.gallery);
 
	        ManateeAdapter manateeAdapter = new ManateeAdapter(this);
 
	        gallery.setAdapter(manateeAdapter);
	    }
 
	    public static class ManateeAdapter extends BaseAdapter {
	        private static final String TAG = "ManateeAdapter";
	        private static int convertViewCounter = 0;
			private Context mContext;
	        private LayoutInflater mInflater;
 
		// 管理數據
	        static class ViewHolder {
	            ImageView image;
	        }
 
	        private int[] manatees = {
	                R.drawable.manatee00, R.drawable.manatee01, R.drawable.manatee02,
	                R.drawable.manatee03, R.drawable.manatee04, R.drawable.manatee05,
	                R.drawable.manatee06, R.drawable.manatee07, R.drawable.manatee08,
	                R.drawable.manatee09, R.drawable.manatee10, R.drawable.manatee11,
	                R.drawable.manatee12, R.drawable.manatee13, R.drawable.manatee14,
	                R.drawable.manatee15, R.drawable.manatee16, R.drawable.manatee17,
	                R.drawable.manatee18, R.drawable.manatee19, R.drawable.manatee20,
	                R.drawable.manatee21, R.drawable.manatee22, R.drawable.manatee23,
	                R.drawable.manatee24, R.drawable.manatee25, R.drawable.manatee26,
	                R.drawable.manatee27, R.drawable.manatee28, R.drawable.manatee29,
	                R.drawable.manatee30, R.drawable.manatee31, R.drawable.manatee32,
	                R.drawable.manatee33 };
 
	        private Bitmap[] manateeImages = new Bitmap[manatees.length];
	        private Bitmap[] manateeThumbs = new Bitmap[manatees.length];
 
	        public ManateeAdapter(Context context) {
	        	Log.v(TAG, "Constructing ManateeAdapter");
	        	this.mContext = context;
	        	mInflater = LayoutInflater.from(context);
 
	        	for(int i=0; i<manatees.length; i++) {
	        		manateeImages[i] = BitmapFactory.decodeResource(
	        				context.getResources(), manatees[i]);
	        		// 創建數據的一個較小版本用於顯示		
	        		manateeThumbs[i] = Bitmap.createScaledBitmap(manateeImages[i],
	        				100, 100, false);
	        	}
	        }
 
	        // 獲取對象數量
	    	public int getCount() {
	    		Log.v(TAG, "in getCount()");
	    		return manatees.length;
	    	}
 
		// 獲取視圖類型數量
	    	public int getViewTypeCount() {
	    		Log.v(TAG, "in getViewTypeCount()");
	    		return 1;
	    	}
 
	    	// 獲取視圖類型
	    	public int getItemViewType(int position) {
	    		Log.v(TAG, "in getItemViewType() for position " + position);
	    		return 0;
	    	}
 
		// 獲取視圖
                // 參數convertView,用於緩存最近一個失去可視狀態的列表項控件對象
 
	    	public View getView(int position, View convertView, ViewGroup parent) {
	            ViewHolder holder;
 
	            Log.v(TAG, "in getView for position " + position + 
	            		", convertView is " +
	            		((convertView == null)?"null":"being recycled"));
 
	            if (convertView == null) {
	            	// 1、對於一個沒有被載入或者想要動態載入的界面,都需要使用LayoutInflater.inflate()來載入;
			// 2、對於一個已經載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素
	                convertView = mInflater.inflate(R.layout.gridimage, null);
	                convertViewCounter++;
	                Log.v(TAG, convertViewCounter + " convertViews have been created");
 
	                holder = new ViewHolder();
	                holder.image = (ImageView) convertView.findViewById(R.id.gridImageView);
                        // 對列表控件中的子控件進行緩存
	                convertView.setTag(holder);
	            } else {
	                holder = (ViewHolder) convertView.getTag();
	            }
 
	            holder.image.setImageBitmap(manateeImages[position]);
 
	            return convertView;
	        }
 
	    	public Object getItem(int position) {
	    		Log.v(TAG, "in getItem() for position " + position);
	    		return manateeImages[position];
	    	}
 
	    	public long getItemId(int position) {
	    		Log.v(TAG, "in getItemId() for position " + position);
	    		return position;
	    	}
	    }
}

2. 效果圖




發佈了79 篇原創文章 · 獲贊 5 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章