这是我的问题:我花了好几个小时试图让ExpandableListView工作。最后我做到了。然后我尝试将此代码填充到需要此ListView的现有应用程序中。现在它不再起作用了。 ListView没有真正展开(children = items)没有显示。你知道出了什么问题吗? 我试图清理代码并删除与此ExpandableListView无关的活动部分。这就是它的样子: (非常感谢您的任何帮助)
public class ExpandableAdapter extends BaseExpandableListAdapter {
private List<GroupCat> catList;
private Context ctx;
NewGraveActivity app;
public ExpandableAdapter(List<GroupCat> catList, Context ctx, NewGraveActivity app) {
this.catList = catList;
this.ctx = ctx;
this.app = app;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return catList.get(groupPosition).getItemList().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return catList.get(groupPosition).getItemList().get(childPosition).hashCode();
}
@Override
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.item_layout, parent, false);
}
CheckBox itemCheck = (CheckBox) v.findViewById(R.id.itemCheck);
TextView itemName = (TextView) v.findViewById(R.id.itemName);
if (app.selected.containsKey((groupPosition))==true)
itemCheck.setChecked(true);
else
itemCheck.setChecked(false);
Category child = catList.get(groupPosition).getItemList().get(childPosition);
itemName.setText(child.getName());
itemCheck.setOnCheckedChangeListener(new CheckListener(childPosition, app));
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
int size = catList.get(groupPosition).getItemList().size();
System.out.println("Child for group ["+groupPosition+"] is ["+size+"]");
return size;
}
@Override
public Object getGroup(int groupPosition) {
return catList.get(groupPosition);
}
@Override
public int getGroupCount() {
return catList.size();
}
@Override
public long getGroupId(int groupPosition) {
return catList.get(groupPosition).hashCode();
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.group_layout, parent, false);
}
TextView groupName = (TextView) v.findViewById(R.id.groupName);
GroupCat cat = catList.get(groupPosition);
groupName.setText(cat.getGroupName());
return v;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class NewGraveActivity extends Activity {
ExpandableListView categoryList;
private List<GroupCat> catList;
public HashMap<Integer, Boolean> selected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
setContentView(R.layout.activity_newgrave);
///////////////////////////// --- CATEGORIES LISTVIEW --- ///////////////////
selected = new HashMap<Integer, Boolean>();
ExpandableListView exList = (ExpandableListView) findViewById(R.id.expandableListView1);
exList.setIndicatorBounds(5, 5);
ExpandableAdapter exAdpt = new ExpandableAdapter(catList, this, this);
exList.setIndicatorBounds(0, 20);
exList.setAdapter(exAdpt);
}
private void initData() {
catList = new ArrayList<GroupCat>();
GroupCat cat1 = createCategory("Categories", 1);
cat1.setItemList(createItems());
catList.add(cat1);
Log.d("NewGrave initData", "Liste von Kategroien erstellt");
}
private GroupCat createCategory(String name, long id) {
Log.d("NewGrave CreateCategory", "Kategorie erzeugen");
return new GroupCat(id, name);
}
private List<Category> createItems() {
List<Category> result = new ArrayList<Category>();
String[] array = getResources().getStringArray(R.array.categoryNames);
for (int i=0; i < array.length; i++) {
Category item = new Category(i, array[i]);
result.add(item);
Log.d("createItems", item.getName());
}
Log.d("NewGrave createItems", "Items in Liste erledigt");
for (int i = 0; i < result.size(); i++) {
Log.d("ItemDetail enthalten:", result.get(i).getName());
}
return result;
}
}
这些是我的xml布局文件:
一般活动的布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/labelInCategories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/labelInCategories" />
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
</ExpandableListView>
<Button
android:id="@+id/buttonNewGrave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonNewGrave" />
</LinearLayout>
</ScrollView>
ExpandableListView中的组布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/groupName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dip" />
<TextView
android:id="@+id/groupDescr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="8dip" />
</LinearLayout>
ExpandableListView中项目的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/itemCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/itemName"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>