android - 可扩展 ListView 未显示

标签 android expandablelistview

代码没有任何错误或警告,但未显示可扩展列表,这是代码

public class MainActivity extends Activity implements  ExpandableListView.OnChildClickListener{
    ArrayList<List> childitems;
    ArrayList groupitems;
    ExpandableListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        list=(ExpandableListView)findViewById(R.id.list);
        list.setOnChildClickListener(this);
        addGroupItems();
        addChildItems();
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ExpandableListAdapterImpl adapter=new ExpandableListAdapterImpl(inflater, this, groupitems, childitems);
        list.setAdapter(adapter);
        super.onCreate(savedInstanceState);
    }

    private void addChildItems() {
        ArrayList childlist;
        try {
            childitems=new ArrayList<List>();
            childlist=new ArrayList();
            childlist.add("Pragnani");
            childlist.add("Kinnera");
            childlist.add("Good");
            childitems.add(childlist);

            childlist=new ArrayList();
            childlist.add("Bannu");
            childlist.add("Kinnera");
            childlist.add("Good");
            childitems.add(childlist);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void addGroupItems() {
        try {
            groupitems=new ArrayList();
            groupitems.add("My Details 1");
            groupitems.add("My Details 2");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        // TODO Auto-generated method stub
        return false;
    }

}

这里是activity_main.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <ExpandableListView
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/list"
             >

        </ExpandableListView>

    </RelativeLayout>

可扩展 ListView 适配器实现

public class ExpandableListAdapterImpl extends BaseExpandableListAdapter
{
    LayoutInflater inflatter;
    Context context;
    ArrayList groupitems;
    ArrayList<List> childitems;

    ExpandableListAdapterImpl(LayoutInflater inflater,Activity activity,ArrayList groupitems,ArrayList<List> childitems)
    {
        super();
        this.inflatter=inflater;
        context=activity;
        this.groupitems=groupitems;
        this.childitems=childitems;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ArrayList childtemp=(ArrayList) childitems.get(groupPosition);
        try {
            if(convertView==null)
            {
                convertView=inflatter.inflate(R.layout.childrow, null);
            }
            Button button=(Button)convertView.findViewById(R.id.button1);
            button.setText(childtemp.get(groupPosition).toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    try {
        if(convertView==null)
        {
            convertView=inflatter.inflate(R.layout.grouprow, null);
        }
        CheckedTextView view=(CheckedTextView) convertView.findViewById(R.id.textView1);
        view.setText(groupitems.get(groupPosition).toString());
        view.setChecked(isExpanded);

    } catch (Exception e) {
        e.printStackTrace();
    }
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}

这里是 grouprow.xml

<CheckedTextView xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_marginLeft="5dp"
    android:drawableRight="@drawable/plusminus"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:text="@string/hello_world"
    android:textColor="#FFFFFF"
    android:textSelectHandleLeft="@string/hello_world"
    android:textSize="14sp"
    android:textStyle="bold" />

子行.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:background="@drawable/playersdetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

我想知道我哪里遗漏了...提前致谢

最佳答案

是的.....Pragnani 我做到了.....

修改 ExpandableListAdapterImpl 中的代码: child 计数

 @Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<String>) childitems.get(groupPosition)).size();
}

对于组计数:

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return groupitems.size();
}

那肯定有用......

关于android - 可扩展 ListView 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14373811/

相关文章:

android - ExpandableListView 扩展 View 未显示

android - 使用android espresso访问自定义可扩展列表中的 child

android - I/O 错误连接被拒绝 android studio

android - 如何在android中包含公共(public)代码

Android ExpandableListView 变量范围

java - 可扩展 ListView 刷新

java - Android:获取 ExpandableListView 中的组数?

android - 如何在 LibGDX 中检查互联网连接

android - 垂直滚动时为图像从小到大创建动画

java - 扩展 MapActivity 时 onPause() 函数不起作用