繪圖2  

 


 

 

list.xml  定義 主畫面Layout

<?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"
   android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

 


 

 

item.xml 定義 單一項目顯示格式Layout

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:id="@+id/txtID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID" />

<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />

<ImageView android:id="@+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="5dp"
android:src="@drawable/ic_launcher"
android:layout_alignParentRight="true"
/>

 </RelativeLayout>

 


 

 

item.java 定義項目內容格式

public class item {

   private String id ;

   private String name;

   public String getId () {
       return id;
   }

   public void setId ( String value )
       id = value;
   }

   public String getName () {
       return name;
   }

   public void setName ( String value ) {
       name = value;
   }

}


 

 

list.java

public class list extends Activity {

    private item[] it = new item[15];

    private ArrayList<HashMap<String,Object>> listobj = new ArrayList<HashMap<String,Object>>();

    private SimpleAdapter ad;

    public void onCreate ( Bundle bundle ) {

        super.onCreate ( bundle );
        setContentView ( R.layout.list ) ;

       建立測試資料
        for ( int i = 0 ; i < 15 ; i++)

        {
            String t = String.valueOf(i);
            it [i] = new item();
            it [i].setID( t );
            it [i] .setName("Address " + t);
        }

 

        將測試資料,放入HasMap物件中 
        因為有使用到非字串的物件,所以 原本的HasMap < String , String >
        就改成 HasMap < String , Object >
        for ( int i = 0 ; i < felist.length ; i++ )
        {
            HashMap < String , Object > obj = new HashMap < String , Object > ( ) ;
            obj .put ( "id" , it [i] .getId ( ) );           
            obj .put ( "name" , it [i] .getName ( ) ) ;
            obj .put ( "img" , R.drawable.ic_launcher ) ;            
            listobj .add ( obj ) ;       
        }

 

       定義Adapter內容,並且Bind資料至物件
       注意紅色字體的文字,需和上方HasMap定義的相同
        ad = new SimpleAdapter ( this , listobj , R.layout.item
             , new String [ ] { "id" , "name" , "img" }
             , new int [ ] { R.id.txtID , R.id.txtName , R.id.img }
        );


 
     ad = new SimpleAdapter ( 目前Layout , 資料來源 , 目標Layout
             , 資料來源識別
             , 目標Layout識別
        );
        
        因為我們使用的格式layout,是item.xml
        所以在目標Layout這一欄位,我們就要輸入R.layout.item
        將資料要填入的顯示物件給Bind起來
        然後才能放入ListView中使用。



        ListView listview = (ListView)findViewById(R.id.listView1);
        listview.setAdapter ( ad ) ;  設定要使用的Adapter
        listview.setTextFilterEnabled(true);

 

       定義 Click 事件
        listview.setOnItemClickListener(item_Click); 

    }

}

 

private OnItemClickListener item_Click = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
                Toast.makeText(mapClass.this, arg2, Toast.LENGTH_LONG).show();
        }
};

 


 

 

完成畫面如下:

0129

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Jiang Ying-Fu 的頭像
    Jiang Ying-Fu

    Jiang Ying-Fu的部落格

    Jiang Ying-Fu 發表在 痞客邦 留言(0) 人氣()