2012年8月17日 星期五

ListActivity & ExpandableListView



ListActivity (Multiple Choice + Single Choice + Header/Footer)

Multiple Choice:




public class Cq_MyListActivityMultipleChoice extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        
        String [] values1 = new String []{"蘋果", "香吉士", "奇異果", "西洋梨", "西瓜", "哈密瓜", "葡萄", "杏子", "琵琶", "甘蔗", "龍眼", "荔枝", "橘子", "榴槤", "火龍果", "柚子", "小紅苺", "紅毛丹", "柿子", "楊桃"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, values1); 
        setListAdapter(adapter);
        
        ListView listview = getListView();
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
    }
}


********************************************
Single-Choice:


public class Cq_MyListActivitySingleChoice extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        
        String [] values1 = new String []{"蘋果""香吉士""奇異果""西洋梨""西瓜""哈密瓜""葡萄""杏子""琵琶""甘蔗""龍眼""荔枝""橘子""榴槤""火龍果""柚子""小紅苺""紅毛丹""柿子""楊桃"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, values1); 
        setListAdapter(adapter);
        
        ListView listview = getListView();
        listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        
    }
}


*******************************************
FOOTER & HEADER:




//Footer and Header A
        ListView listview = getListView();
        View header = getLayoutInflater().inflate(R.layout.header, null);
        View footer = getLayoutInflater().inflate(R.layout.footer, null);



TextView textview1 = (TextView)header.findViewById(R.id.headerTextView01);
textview1.setText(getEvent + " :   ");
TextView textview2 = (TextView)header.findViewById(R.id.headerTextView02);
textview2.setText(Integer.toString(str1.length) + " events");


        listview.addHeaderView(header);
        listview.addFooterView(footer);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, android.R.id.text1, values1);
        listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setListAdapter(adapter);



************************************************
//Footer and Header B
如果用的是 ExpandableListActivity




//Set Header
        ListView listview = getExpandableListView();
        View header = getLayoutInflater().inflate(R.layout.mainheader, null);
        ImageView iconimg = (ImageView)header.findViewById(R.id.iconImage);
        TextView headertv1 = (TextView)header.findViewById(R.id.headerTextView01);
        TextView headertv2 = (TextView)header.findViewById(R.id.headerTextView02);
        iconimg.setImageResource(R.drawable.airplane1);
       
        headertv1.setText("旅遊英語實用會話");
        headertv2.setText("English Phrasal Books for Tourists");
        listview.addHeaderView(header);
        
        //Set Adapter
        mAdapter = new MyExpandableListAdapter(this);
        setListAdapter(mAdapter);
        

***********************************************

//example 2: Create a customized Header in a ListView under ListActivity      

ListView listview = getListView();
View header = getLayoutInflater().inflate(R.layout.header, null);

//取得 headerlayout.xml 裡的物件
TextView textview1 = (TextView)header.findViewById(R.id.headerTextView01);
TextView textview2 = (TextView)header.findViewById(R.id.headerTextView02);
textview1.setText(getEvent + " :   ");
textview2.setText(Integer.toString(str1.length) + " events");

listview.addHeaderView(header);

ArrayAdapter<String> adapter = new EventAdapter(this, str1, str2);
setListAdapter(adapter);

*************
附上:Customized    headerlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     >

          <TextView
            android:id="@+id/headerTextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5px"
            android:layout_marginRight="10px"
            android:layout_marginTop="1px"
            android:text="運動" 
            android:textSize = "20dp"
            android:textColor="@color/lime"/>

        <TextView
            android:id="@+id/headerTextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="1px"
            android:layout_marginRight="2px"
            android:layout_marginTop="1px"
            android:layout_toRightOf="@+id/headerTextView01"
            android:text="項目"
            android:textSize = "20dp"
            android:textColor="@color/lime"/>


</RelativeLayout>


沒有留言:

張貼留言