標籤:
三個關鍵點
XML布局
- 主Activity布局
- ListView條目的XML布局
主Activity布局,只需要加入一個ListView控制項,特別要注意各個控制項的layout_width和layout_height的設定
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <LinearLayout 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content"10 android:orientation="horizontal">11 <TextView 12 android:text="@string/name"13 android:layout_width="1dp"14 android:layout_height="wrap_content"15 android:layout_weight="3"/>16 <TextView 17 android:text="@string/phone"18 android:layout_width="1dp"19 android:layout_height="wrap_content"20 android:layout_weight="5"/>21 <TextView 22 android:text="@string/account"23 android:layout_width="1dp"24 android:layout_height="wrap_content"25 android:layout_weight="2"/>26 </LinearLayout>27 28 <ListView 29 android:id="@+id/listView"30 android:layout_width="fill_parent"31 android:layout_height="fill_parent">32 33 </ListView>34 35 </LinearLayout>
條目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="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="1dp" android:layout_height="wrap_content" android:layout_weight="3"/> <TextView android:id="@+id/phone" android:layout_width="1dp" android:layout_height="wrap_content" android:layout_weight="5"/> <TextView android:id="@+id/account" android:layout_width="1dp" android:layout_height="wrap_content" android:layout_weight="2"/> </LinearLayout>
資料和簡單資料配接器
- 資料形式為Cursor 結果集形式及相應的SimpleCursorAdapter
- 資料形式為List< ?extendsMap< String,? > >及相應的SimpleAdapter
Cursor和SimpleCursorAdapter
private void showListView(Uri uri) { ContentResolver resolver = MainActivity.this.getContentResolver(); cursor = resolver.query(uri, null, null, null, "_id desc limit 0,20"); //給listView綁定適配器 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"name", "phone", "account"}, new int[]{R.id.name, R.id.phone, R.id.account}, 1); listView.setAdapter(adapter); }List< ?extendsMap< String,? > >及SimpleAdapter
private void otherShowListView(Uri uri) { ContentResolver resolver = MainActivity.this.getContentResolver(); cursor = resolver.query(uri, null, null, null, "_id desc limit 0,50"); //資料存放區形式為List< ?extendsMap< String,? > > data; List< HashMap<String,Object> > data = new ArrayList<HashMap<String,Object> >(); while(cursor.moveToNext()){ HashMap<String, Object> item = new HashMap<String, Object>(); item.put("name", cursor.getString(cursor.getColumnIndex("name"))); item.put("phone", cursor.getString(cursor.getColumnIndex("phone"))); item.put("account", cursor.getString(cursor.getColumnIndex("account"))); data.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"name", "phone", "account"}, new int[]{R.id.name, R.id.phone, R.id.account}); listView.setAdapter(adapter); }
自訂配接器
- 首先調用適配器的getCount得到共有n個條目
- 然後根據每個條目的高度計算出頁面可以容納perPage個條目
- 然後為這perPage個條目通過調用getView對象建立出perPage個View控制項
- ListView是具有緩衝功能的,即只會建立perPage個View,當上下移動時,新出現的View使用暫時看不到的View控制項(改變其中的資料)
1 private void showListView3(){ 2 final List<Person> dataList = pService.getScrollDate1(0, 30); 3 listView.setAdapter(new BaseAdapter() { 4 @Override 5 public View getView(int position, View convertView, ViewGroup parent) { 6 ViewCache vCache; 7 //如果該View控制項為空白,初始化 8 if(convertView == null){ 9 convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null);10 vCache = new ViewCache();11 /**12 * 每次findView會降低效能,所以希望只在第一次建立View對象時執行find操作13 */14 vCache.name = (TextView) convertView.findViewById(R.id.name);15 vCache.phone = (TextView) convertView.findViewById(R.id.phone);16 vCache.account = (TextView) convertView.findViewById(R.id.account);17 convertView.setTag(vCache);18 }19 vCache = (ViewCache) convertView.getTag();20 vCache.name.setText(dataList.get(position).getName());21 vCache.phone.setText(dataList.get(position).getPhone());22 vCache.account.setText(dataList.get(position).getAccount()+"");23 24 return convertView;25 }26 @Override27 public long getItemId(int position) {28 return position;29 }30 @Override31 public Object getItem(int position) {32 return dataList.get(position);33 }34 35 @Override36 public int getCount() {37 return dataList.size();38 }39 });40 }
熟悉AndroidAPI系列15——ListView