我有一个可扩展的列表,它在init函数中填充(1个父项,10个孩子)。当我的应用程序启动(扩展列表)时,我可以看到条目,但getChildCount() = 0
。
我需要折叠并展开列表才能使用它。命令getCount()
返回正确的值,但这对我没有帮助。
我想在开头标记一个条目(更改背景),但我不能,因为列表似乎是空的。
祝你好运
(Android 2.3.3)
修改
抱歉延误。我使用的可扩展列表视图的示例如下所示。
Init函数(名为onCreate())
private void init_List() {
Log.i("Function", "Init_List()");
mExpandableList = (ExpandableListView) findViewById(R.id.expandableList);
ArrayList<ListParent> arrayParents = new ArrayList<ListParent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
// here we set the parents and the children
ListParent parent = new ListParent();
parent.setTitle(this.getString(R.string.parent_title));
arrayChildren = new ArrayList<String>();
for (int i=0; i<10; i++){
arrayChildren.add(i+1+". "+names[i]);
}
parent.setArrayChildren(arrayChildren);
// In this array we add the Parent object. We will use the arrayParents
// at the setAdapter
arrayParents.add(parent);
// Sets the adapter that provides data to the list.
mExpandableList.setAdapter(new ListAdapter(this,
arrayParents));
mExpandableList.setOnChildClickListener(this);
mExpandableList.setOnGroupClickListener(this);
}
ListAdapter
公共类ListAdapter扩展了BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<ListParent> mParent;
public ListAdapter(Context context, ArrayList<ListParent> parent){
mParent = parent;
inflater = LayoutInflater.from(context);
}
@Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
@Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
return mParent.get(i).getArrayChildren().size();
}
@Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
@Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder.groupPosition = i; //Group position
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(i).toString());
view.setTag(holder);
//return the entire view
return view;
}
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder.childPosition = i1; //child position
holder.groupPosition = i; // group position
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
textView.setText(mParent.get(i).getArrayChildren().get(i1));
view.setTag(holder);
//return the entire view
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
}
protected class ViewHolder {
protected int childPosition;
protected int groupPosition;
protected Button button;
} }
列出父母
public class ListParent {
private String mTitle;
private ArrayList<String> mArrayChildren;
public String getTitle() {
return mTitle;
}
public void setTitle(String mTitle) {
this.mTitle = mTitle;
}
public ArrayList<String> getArrayChildren() {
return mArrayChildren;
}
public void setArrayChildren(ArrayList<String> mArrayChildren) {
this.mArrayChildren = mArrayChildren;
}
}