1

カスタムアダプタを実装してAndroidにExpandableListViewを実装しようとしていますが、画面に出力が表示されません。

主な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;
}


}

}

すべてが十分に単純に見えますが、アプリを実行した後、画面は空白のままです。ヘルプ/理想はありがたいです。

4

2 に答える 2

2

あなたのコードは不完全に見えます。メソッドgetGroup、 、getGroupIdおよびにプレースホルダーが追加されましたgetGroupCount。それらはgroupElements配列を参照する必要があります。

現在ゼロを返すという事実は、が何も表示しないgetGroupCountのに十分です。ExpandableListView

于 2012-05-14T14:20:15.583 に答える
0

おそらく、 の戻り値をgetGroupCount()に設定する必要がありgroupElements.lengthます。

現在返されている0は、グループがないことを示しているため、何も表示されません。

于 2012-05-14T14:20:48.873 に答える