2012-05-14 21 views
1

我想通过实现一个自定义适配器在Android中实现ExpandableListView,但我没有得到任何输出在屏幕上。可扩展ListView与自定义适配器

主要XML布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="This is an expandable listview" 
    /> 
<ExpandableListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 


</LinearLayout> 

集团布局文件是:

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

<TextView 
    android:id="@+id/tvPlayerName" 
    android:textSize="14px" 
    android:textStyle="normal" 
    android:layout_width="150px" 
    android:layout_height="wrap_content" 
    /> 

子布局文件是:

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

<TextView 
    android:id="@+id/tvPlayerName" 
    android:textSize="14px" 
    android:textStyle="normal" 
    android:layout_width="150px" 
    android:layout_height="wrap_content" 
    /> 

</LinearLayout> 

,最后,活动类文件是:

public class ExpandableListViewTest extends ExpandableListActivity { 
String groupElements[] = {"India","Austrailia","England","South Africa"}; 
String childElements[][] = { 
    {"Sachin Tendulkar","Raina","Dhoni","Yuvraj"}, 
    {"Ponting","Adam Gilchrist","Michael Clarke"}, 
    {"Andrew Strauss","Kevin Peterson","Nasir Hussain"}, 
    {"Grame Smith","AB de Villiers","Jacques Kallis"} 
}; 

int width; 
ExpandableListView expList; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Setup our adapter 
    MyExpandableAdapter mAdapter = new MyExpandableAdapter(this); 
    setListAdapter(mAdapter); 
} 
public class MyExpandableAdapter extends BaseExpandableListAdapter 
{ 
    private Context myContext; 

    public MyExpandableAdapter(Context context) 
    { 
     this.myContext= context; 
    } 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return childElements[groupPosition][childPosition]; 
} 

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

@Override 
public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    if(convertView == null) 
    { 
     LayoutInflater inflater = getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.child, parent,false); 
    } 
    TextView tvPlayerName = 
     (TextView)convertView.findViewById(R.id.tvPlayerName); 
    tvPlayerName.setText(childElements[groupPosition][childPosition]); 

    return convertView; 

} 

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

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

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

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    if(convertView == null) 
    { 
     LayoutInflater inflater = getLayoutInflater(); 
     convertView = inflater.inflate(R.layout.group,parent,false); 
    } 
    TextView tvGroupName = (TextView)convertView.findViewById(R.id.groupName); 
    //tvGroupName.setText(groupElements[groupPosition]); 
    tvGroupName.setText("Group Row"); 

    return convertView; 

} 

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

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


} 

} 

一切似乎straighhtforward不够好,但在运行的应用程序后,屏幕一直blank.Any帮助/理想表示赞赏。

回答

2

您的代码看起来不完整。您刚刚在方法getGroupgetGroupIdgetGroupCount中占位符。他们应该参考您的groupElements阵列。

getGroupCount目前返回零的事实足以让ExpandableListView不显示任何内容。

0

您可能应该将getGroupCount()的返回值设置为groupElements.length

当前返回的表示您没有任何组,因此没有任何可显示的内容。

+0

这工作,谢谢。 – user1107888

+1

很高兴听到!祝你好运:) – rekaszeru

相关问题