2012-01-19 20 views
0

私はxmlの前にボタンでexpandableListViewを表示しようとしています。しかし何とか、私はコード内でそれを動作させることができませんでした。androidの使い方ExpandableListView in xml?

私のxml(私は親のレイアウトとしてRelativeLayoutを使用しています):私は変更しようとした

java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list' 

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/mlt_file_titleFrame" 
    android:background="#FF3300" 
    android:padding="5dp" 
    android:id="@+id/mlt_file_frameL" 
    > 

    <Button 
     android:id="@+id/mlt_file_btn_edit" 
     android:text="Edit" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="left" 
     /> 

</FrameLayout> 

<ExpandableListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/mlt_file_frameL" 
    > 

</ExpandableListView> 

私のコード:

public class MLT_File extends ExpandableListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mlt_file_intent); 
    // Set up our adapter 
    ExpandableListAdapter listAdapter = new customListAdapter(); 

    ExpandableListView expLV = (ExpandableListView) findViewById(R.id.mlt_expList);//getExpandableListView(); 
    expLV.setAdapter(listAdapter); 

    //registerForContextMenu(expLV); 

}//onCreate 

/** 
* A simple adapter which maintains an ArrayList of photo resource Ids. 
* Each photo is displayed as an image. This adapter supports clearing the 
* list of photos and adding a new photo. 
* 
*/ 
public class customListAdapter extends BaseExpandableListAdapter { 
    // Sample data set. children[i] contains the children (String[]) for groups[i]. 
    private File mapFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Map"); 
    private File recordFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Record"); 
    private File scribbleFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/Scribble"); 
    private String[] groups = { "Map", "Record", "Scribble" }; 
    private String[][] children = { mapFolder.list(), recordFolder.list(), scribbleFolder.list() }; 

    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) { 

     TextView textView = new TextView(MLT_File.this); 
     textView.setBackgroundColor(Color.BLACK); 
     textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
     textView.setPadding(100, 0, 0, 0); 
     textView.setTextColor(Color.WHITE); 
     textView.setTextSize(20); 
     textView.setText(getChild(groupPosition, childPosition).toString()); 
     return textView; 
    }//getChildView 

    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) { 
     TextView textView = new TextView(MLT_File.this); 
     textView.setBackgroundColor(Color.WHITE); 
     textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
     textView.setPadding(100, 0, 0, 0); 
     textView.setTextColor(Color.BLACK); 
     textView.setTextSize(25); 
     textView.setText(getGroup(groupPosition).toString()); 

     /*Button btn = new Button(MLT_File.this); 
     btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
     btn.setText("Edit"); 
     btn.setId(100+groupPosition); 
     btn.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT); 

     TableLayout layout = new TableLayout(MLT_File.this); 
     layout.setBackgroundColor(Color.WHITE); 
     layout.setColumnStretchable(1, true); 
     layout.addView(textView); 
     //layout.addView(btn);*/ 

     return textView; 
    } 

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

    public boolean hasStableIds() { 
     return true; 
    } 

}//customListAdapter 
}//class 

私のlogcat id to id = "アンドロイド:id/list "、しかし、私はそのIDを持つExpandableListViewオブジェクトを作成する方法を知らない、それは空のことを言うエラーを与えるので。

* impt:私はonChildClickListenerをキャストできるようにExpandableListViewオブジェクトを作成したかったのです。

答えて

1

からこのコード全体でこれを試すことができます write public class MLT_Fileアクティビティをextendアクティビティで拡張します。独自の(カスタマイズされた)展開可能なリストを書くことができます。

expLV= new ExpandableAdapter(Expandablelist.this); 
setListAdapter(expLV); 

グループと子のために

+0

おかげで異なるレイアウトを使用!私が望んでいただけです。シンプルでわかりやすい... – Jovi

+0

+1の簡単な説明 – Praveenkumar

+0

詳細についてはhttp://stackoverflow.com/questions/8925424/android-expandablelistview-making-dynamic-childs/8937610#8937610 – Rajeswari

1

このコードを試してみてください -

Main.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"> 
<ExpandableListView android:id="@+id/listView" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none"></ExpandableListView> 
</LinearLayout> 

ChildLayout.xml -

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" android:layout_height="45dip" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon" 
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="45dip" android:paddingLeft="5dip" 
    android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip" 
    android:gravity="center_vertical" android:id="@+id/tvChild" 
    android:text="Children" android:textColor="#ffCCCC22"></TextView> 
</LinearLayout> 

Grouplayout.xml -

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" android:layout_height="45dip" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@drawable/title"> 
<ImageView android:id="@+id/ImageView01" android:src="@drawable/icon" 
    android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView> 
<TextView android:id="@+id/tvGroup" android:layout_width="fill_parent" 
    android:layout_height="45dip" android:text="Groups" android:gravity="center_vertical|right" 
    android:paddingLeft="5dip" android:paddingRight="5dip" 
    android:textColor="#ffffffff" android:textStyle="bold" 
    android:textSize="17dip"></TextView> 
</LinearLayout> 

これらはのためにレイアウトされていますCustomExpandableリストビュー。

あなたはMLT_FileがExpandableListActivityを拡張する代わりに、パブリッククラスの活性のカスタマイズされたID

<ExpandableListView 
    android:id="@+id/expandablelist" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/mlt_file_frameL" 
    > 

</ExpandableListView> 

にIDすなわちデフォルトのIDを変更するXMLファイルにhere.

関連する問題