2013年10月8日 星期二

BaseAdapter 範例



使用ListView 搭配 BaseAdapter 類別,創造出來的功能

習慣性先作介面的部分。Layout 中設計一個list.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"        
    tools:context=".myList" >

<ListView
   android:id="@android:id/list"    
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >
   
</ListView>
    
</LinearLayout>

adapter.xml 中設計了一個ImageView,一個TextView與一個CheckBox

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10px"
/>
   
<TextView 
android:id="@+id/tv" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_marginLeft="15px"
android:layout_toRightOf="@id/iv"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:minHeight="?android:attr/listPreferredItemHeight"
  />
 
  <CheckBox android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:layout_marginRight="15px"
android:layout_alignParentRight="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:focusable="false"
android:clickable="false"
/>
</RelativeLayout>


在主程式listView中繼承一個ListActivity,在onCreate事件中,先使用
getResources().getStringArray(R.array.books)來讀取string.xml中的books陣列。使用setListAdapter函數來為listView  提供資料。MyAdapter為一個繼承BaseAdapter的自訂類別。



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
CharSequence[] list=getResources().getStringArray(R.array.books);
setListAdapter(new MyAdapter(this,list));
}

MyAdapter為一個繼承BaseAdapter的自訂類別。 可自訂一個建構子 MyAdapter(Context ctxt,CharSequence[] list),
並需實作getCount()、getItem(int position)、getItemId(int position)與 
getView(int position, View convertView, ViewGroup parent)。


其中 MyAdapter可透過LayoutInflater 取得一個View

public MyAdapter(Context ctxt,CharSequence[] list)
{
// Obtains the LayoutInflater from the given context.
// myInflater=LayoutInflater.from(ctxt);
myInflater=LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list=list;

}
public int getCount() { return list.length; }

public Object getItem(int position) {return list[position];}


public long getItemId(int position) {return position;}

getView 可透過 ViewTag 取得與adapter.xml的連結

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

ViewTag viewTag;
if(convertView==null)
{
//Inflate a new view hierarchy from the specified xml resource.
//Throws InflateException if there is an error.
convertView=myInflater.inflate(R.layout.adapter,null);
viewTag = new ViewTag(
(ImageView)convertView.findViewById(R.id.iv),
(TextView) convertView.findViewById(R.id.tv),
(CheckBox) convertView.findViewById(R.id.cb)
);
convertView.setTag(viewTag);
}
else{
viewTag = (ViewTag) convertView.getTag();
}
switch(position)
{
case Jobs: 
viewTag.iv.setBackgroundResource(R.drawable.jobs);
break;
case Swan: viewTag.iv.setBackgroundResource(R.drawable.blackswan);
break;
case Thinking: viewTag.iv.setBackgroundResource(R.drawable.thinking);
break;
}

viewTag.tv.setText(list[position]);
return convertView;

}


public class ViewTag {
ImageView iv;
TextView tv;
CheckBox cb;
public ViewTag(ImageView iv, TextView tv, CheckBox cb) {
// TODO Auto-generated constructor stub
this.iv=iv;
this.tv=tv;
this.cb=cb;

}


}
程式下載


沒有留言:

張貼留言