2011-05-09 207 views
0

主要活動:擴展列表視圖

public class ListActivityEx extends Activity { 
/** Called when the activity is first created. */ 
LayoutInflater inflate; 
ExpandableListAdapter adapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    adapter = new MyCustomExpand(this); 
    ExpandableListView expand = (ExpandableListView) findViewById(R.id.expandableListView_profile); 
    expand.setAdapter(adapter); 

} 

} 

適配器類:

public class MyCustomExpand extends BaseExpandableListAdapter { 
LayoutInflater infalInflater; 
private String[] groups = { "Test1", "Test2", "Test3" }; 

private String[][] children = { 

{ "neelinfo", "MobileApp", "HYD" }, 

{ "rajesh", "palepu", "narasimha" }, 

{ "Android", "iphone", "J2ME" } }; 

// Add these lines to your code 
private Context context; 

public MyCustomExpand(Context context) { 
    this.context = context; 
} 


public Object getChild(int groupPosition, int childPosition) { 
    return children[groupPosition][childPosition]; 
} 

public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

public int getChildrenCount(int groupPosition) { 
    return children[groupPosition].length; 
} 

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
     View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.child_row, null); 
    } 

    TextView textView = (TextView) convertView.findViewById(R.id.childname3); 
    textView.setText(getChild(groupPosition, childPosition).toString()); 
    return convertView; 
} 
public Object getGroup(int groupPosition) { 
    return groups[groupPosition]; 
} 

public int getGroupCount() { 
    return groups.length; 
} 

public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    if (convertView == null) { 
     switch (groupPosition) { 
     case 0: 

      holder = new ViewHolder(); 
      infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.group_row, null, 
        false); 
      holder.textView = (TextView) convertView 
        .findViewById(R.id.childname); 
      Log.d("heading", getGroup(groupPosition).toString()); 
      holder.textView.setText(getGroup(groupPosition).toString()); 

      break; 
     case 1: 
      infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.group_row2, null, 
        false); 
      holder = new ViewHolder(); 
      holder.textView = (TextView) convertView 
        .findViewById(R.id.childname11); 
      // holder.textView.setText(getGroup(groupPosition).toString()); 
      Log.d("heading", getGroup(groupPosition).toString()); 

      holder.textView.setText(getGroup(groupPosition).toString()); 
     case 2: 
      infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.group_row2, null, 
        false); 
      holder = new ViewHolder(); 
      holder.textView = (TextView) convertView 
        .findViewById(R.id.childname11); 
      // holder.textView.setText(getGroup(groupPosition).toString()); 
      Log.d("heading", getGroup(groupPosition).toString()); 
      holder.textView.setText(getGroup(groupPosition).toString()); 
     } 
    } 
    return convertView; 
} 

public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

public boolean hasStableIds() { 
    return true; 
} 

static class ViewHolder { 
    TextView textView; 
} 

} 
+0

我得到verity輸出ifr我點擊組的位置1 ..我得到第二組的職位....我不知道爲什麼?誰能幫幫我嗎???我需要custon Android Expandablelist – rajesh 2011-05-09 06:48:29

回答

0

那是因爲你沒有breakcase 1:做。和'情況2'