2012年2月18日 星期六

ExpandableListActivity_02


Main.java:


public class Cq_ExpandableListActivityActivity extends ExpandableListActivity {
    /** Called when the activity is first created. */
public String [] tourism = {"ARRIVAL", "CITY", "SOCIABLE", "ENTERTAINMENT", "HOTEL", "MEDICAL TREATMENT"};
public String [] tourism1 = {"抵達", "城市", "社交", "娛樂", "旅館", "醫療"};
public String [] arrival = {"customs", "passport"};
public String [] city = {"general", "transport", "taxi"};
public String [] sociable = {"personal", "profession", "meeting", "language"};
public String [] entertainment = {"leisure", "theaters", "exhibition", "sports"};
public String [] hotel = {"general", "check-in", "staying", "check-out"};
public String [] medical = {"general", "surgeons", "dentist", "pharmacy"};
 
 
ExpandableListAdapter mAdapter;
Context mContext;
private String [] groups = tourism1;
private String [][] children = {arrival, city, sociable, entertainment, hotel, medical};
 
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        //Set up our adapter
        mAdapter = new MyExpandableListAdapter (this);
        setListAdapter(mAdapter);
        registerForContextMenu(getExpandableListView());
    }

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
//super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("LittleTa");
menu.add(0,0,0,R.string.hello);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo)item.getMenuInfo();
String title = ((TextView)info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD){
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ":Child" + childPos + " clicked in group " + groupPos, Toast.LENGTH_SHORT).show();
return true;
}else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP){
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title+ ":Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
    
public class MyExpandableListAdapter extends BaseExpandableListAdapter{

public MyExpandableListAdapter(Context context){
mContext = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return children[groupPosition][childPosition];
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
//Create a Linear Layout for the GroupView
public LinearLayout getGenericView (String string){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);
LinearLayout linearlayout = new LinearLayout (mContext);
ImageView icon = new ImageView (mContext);
    icon.setImageResource(R.drawable.basketball);
    icon.setPadding(45, 0, 0, 0);
    linearlayout.addView(icon);
   
    TextView textview = new TextView (mContext);
    //Center the text vertically
    textview.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    //Set the text starting point
    textview.setPadding(36, 0, 0, 0);
    linearlayout.setLayoutParams(lp);
    linearlayout.addView(textview);
    textview.setText(string);
   
    return linearlayout;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// String myText = this.getChild(groupPosition,childPosition).toString();
// LinearLayout linearlayout = getGenericView(myText + "QQTATA");
// textview.setText(getChild(groupPosition, childPosition)).toString());
// return linearlayout;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.album_row, null);
TextView textview = (TextView)v.findViewById(R.id.text1);
String myText = this.getChild(groupPosition, childPosition).toString();
textview.setText(myText);
CheckBox cBox = (CheckBox)v.findViewById(R.id.checkbox);
cBox.setVisibility(View.VISIBLE);
ImageView image = (ImageView)v.findViewById(R.id.rowicon);
image.setImageResource(R.drawable.monkey32);
textview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Cq_ExpandableListActivityActivity.this, IntentShowMsg.class);
startActivity(intent);
}
});
return v;
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return children[groupPosition].length;
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groups[groupPosition];
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groups.length;
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String myText = this.getGroup(groupPosition).toString();
return getGenericView(myText);
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
    
    
}

****************************************************
res/layout/album_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/scrollbarSize"
    android:layout_weight="1" >
    
    <ImageView android:id="@+id/rowicon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip" />
    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <CheckBox android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dip"
        android:focusable="true"
        android:clickable="true"
        android:gravity="center_vertical"
        android:duplicateParentState="true"
        android:visibility="visible"
        android:text="myText"/>
</LinearLayout>

********************************************
public class IntentShowMsg extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("QQ LOVES TATA.");
tv.setTextColor(Color.YELLOW);
tv.setTextSize(50);
setContentView(tv);
}

}


沒有留言:

張貼留言