我想用xml中的按鈕之前顯示一個expandableListView。但不知何故,我無法讓它在代碼中工作。如何在xml中使用android ExpandableListView?
我的XML(我使用的RelativeLayout父佈局):
<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:
java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
我試圖改變id到id =「android:id /列表「,但我不知道如何創建與該ID的ExpandableListView對象,因爲它給出了一個錯誤,說它是空的。
* impt:我想創建一個ExpandableListView對象,以便我可以投出onChildClickListener。
謝謝!它正是我想要的。簡單易懂... – Jovi
+1簡單說明 – Praveenkumar
更多解釋http://stackoverflow.com/questions/8925424/android-expandablelistview-making-dynamic-childs/8937610#8937610 – Rajeswari