這就像他們根本就沒有。它編譯,但每個組中都沒有可見或不可選的內容。這裏是截圖:可擴展的ListView兒童不會顯示
這是我的Activity類。爲孩子們充氣的xml幾乎沒有在那裏。
package com.anthonyce.mcathomie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlayoptionsActivity extends Activity {
ExpandableListView Mtopics;
ExpandableAdapter MtopicsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playoptions);
//set up adapter
MtopicsAdapter = new ExpandableAdapter();
Mtopics = (ExpandableListView) findViewById(R.id.mtopicsListView);
Mtopics.setAdapter(MtopicsAdapter);
//Mtopics.setGroupIndicator(null);
}
public class ExpandableAdapter extends BaseExpandableListAdapter {
private String[] groups = { "People Names", "Dog Names", "Cat Names",
"Fish Names" };
private String[][] children = { { "Arnold" }, { "Ace" }, { "Fluffy" },
{ "Goldy" } };
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final class ExpandChildRow extends LinearLayout {
public ExpandChildRow(Context context) {
super(context);
LayoutInflater childInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout childView = (LinearLayout) childInflate.inflate(R.layout.mtopics_childrow, this, true);
TextView childtxt = (TextView)childView.findViewById(R.id.mtopicchildtext);
childtxt.setText(getChild(groupPosition, childPosition).toString());
}
}
ExpandChildRow ChildRow = new ExpandChildRow(getBaseContext());
return ChildRow;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final class ExpandGroupRow extends LinearLayout {
public ExpandGroupRow(Context context) {
super(context);
LayoutInflater groupInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout groupView = (LinearLayout) groupInflate.inflate(R.layout.mtopics_grouprow, this, true);
TextView grouptxt = (TextView)groupView.findViewById(R.id.mtopicgrouptext);
grouptxt.setText(getGroup(groupPosition).toString());
}
}
ExpandGroupRow GroupRow = new ExpandGroupRow(getBaseContext());
return GroupRow;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
感謝您的協助。說實話,我也喜歡這樣做,但如果我這樣做,我會得到上下文的錯誤。我不確定我是否可以正確設置構造函數。我確實是一個初學者 – mango
哦,只需在上面的代碼中用'getBaseContext()'代替'context'就行。 – Sameer
對不起,我想通了。我真笨。 – mango