无法显示可扩展列表视图

时间:2013-01-17 07:06:13

标签: 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>

可扩展列表视图适配器实现

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" />

childrow.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>

我想知道我在哪里错过...提前致谢

1 个答案:

答案 0 :(得分:4)

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

在ExpandableListAdapterImpl中修改您的代码: 为儿童计数

 @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();
}

这肯定会有效.......