Android 可扩展列表返回0作为childCount()
我有一个可扩展的列表,它填充在init函数中(1个父函数,10个子函数)。当我的应用程序启动(扩展列表)时,我可以看到条目,但Android 可扩展列表返回0作为childCount(),android,android-custom-view,Android,Android Custom View,我有一个可扩展的列表,它填充在init函数中(1个父函数,10个子函数)。当我的应用程序启动(扩展列表)时,我可以看到条目,但getChildCount()=0 我需要折叠并展开列表才能使用它。命令getCount()返回正确的值,但这对我没有帮助 我想在开始时标记一个条目(更改背景),但我不能,因为列表似乎是空的 致意 (安卓2.3.3) 编辑: 抱歉耽搁了。下面是我使用的可扩展列表视图的示例 Init函数(称为onCreate()) private void init_List(){ Log
getChildCount()=0
我需要折叠并展开列表才能使用它。命令getCount()
返回正确的值,但这对我没有帮助
我想在开始时标记一个条目(更改背景),但我不能,因为列表似乎是空的
致意
(安卓2.3.3)
编辑:
抱歉耽搁了。下面是我使用的可扩展列表视图的示例
private void init_List(){
Log.i(“函数”,“Init_List()”);
mExpandableList=(ExpandableListView)findViewById(R.id.expandableList);
ArrayList arrayParents=新的ArrayList();
ArrayList arrayChildren=新的ArrayList();
//我们把父母和孩子放在这里
ListParent=新的ListParent();
setTitle(this.getString(R.string.parent_title));
arrayChildren=新的ArrayList();
对于(int i=0;i可能会显示一些代码。到目前为止,我所能想到的是,您正在将子项添加到错误的布局中。如果没有代码片段,则无法入侵。我用代码更新了帖子。您知道问题可能是什么吗?作为替代方案,有人知道如何模拟可扩展列表上的触摸吗?只要我折叠并展开,代码就会工作列表(不是在使用expList.expandgroup(0)时,必须有其他命令)。
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);
}
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;
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;
}