使用带有XML和代码的ExpandableListView

时间:2011-09-06 13:47:49

标签: android-layout expandablelistview expandablelistadapter

我只是想在一项活动中创建几个小组及其相应的孩子。到目前为止没有成功。尝试了人们在不同问题中提出的各种建议,但他们未能使我的代码工作。也许,我不清楚这个概念并做一些不能解决我的问题的事情!

我想通过XML指定expandablelistview,groupview和childview的布局。我想通过代码操纵数据。目前,我正在使用BaseExpandableListAdpater,但将来我希望它也可以使用SimpleCursorTreeAdapter。

以下是我写的代码:

1)menuitems.xml - 实际的ExpandableListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Menu Title -->
    <!-- <TextView  android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Menu Categories"
                android:textSize="20sp"/> -->

    <!-- Menu sorted according to the categories -->
    <ExpandableListView android:id="@android:id/list"
                        android:layout_height="fill_parent" 
                        android:layout_width="fill_parent"
                        >
    </ExpandableListView>

</LinearLayout>

2)group_list.xml - 用于填充组的布局,即父级

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:id="@+id/groupListId"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:textSize="15dp"
            android:gravity="center"
            android:text="group"
  />

</LinearLayout>

3)child_list.xml - 与父(group_list.xml)对应的子列表

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

  <TextView android:id="@+id/childListId"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:textSize="20dp"
            android:gravity="center"
            android:text="child"
  />

</LinearLayout>

4)FoodieActivity.java

public class FoodieActivity extends ExpandableListActivity {
    /** Called when the activity is first created. */

    ExpandableListView expandableMenuCategories = null;
    ExpandableListAdapter expandableListMenuAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menuitems);

        expandableMenuCategories = getExpandableListView();

        expandableListMenuAdapter = new MyMenuCategoriesAdapter();

        setListAdapter(expandableListMenuAdapter);
        registerForContextMenu(expandableMenuCategories);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) 
    {
        //TODO
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        return super.onContextItemSelected(item);
    }

    public class MyMenuCategoriesAdapter extends BaseExpandableListAdapter
    {

            String[] categoryGroups = {"One,Two,Three"};
        String[][] categoryItems = {{"Number","Integer"},{"Float","Double"},{"short,byte"}};

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return categoryItems[groupPosition][childPosition];
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) 
        {
//          TextView childView = null;

            convertView = ((LayoutInflater.from(FoodieActivity.this)).inflate(R.layout.child_list, null));
            convertView = (TextView) convertView.findViewById(R.id.childListId);
//          TextView childView = new TextView(FoodieActivity.this);
//          childView.setLayoutParams(expandableMenuCategories.getLayoutParams());
            ((TextView)convertView).setText(getChild(groupPosition, childPosition).toString());

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return categoryItems[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return categoryGroups[groupPosition];
        }

        @Override
        public int getGroupCount() {
            return categoryGroups.length;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return categoryGroups[groupPosition].length();
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) 
        {
//          TextView groupView = null;
            try
            {
                convertView = ((LayoutInflater.from(FoodieActivity.this)).inflate(R.layout.group_list,parent, false));
                convertView = (TextView) convertView.findViewById(R.id.groupListId);
//              TextView groupView = new TextView(FoodieActivity.this);
//              groupView.setLayoutParams(expandableMenuCategories.getLayoutParams());
                ((TextView)convertView).setText(getGroup(groupPosition).toString());
            }
            catch(ClassCastException cce)
            {
                Log.d(cce.toString(), cce.getMessage());
            }

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }
}

当我运行此特定代码时,我得到ClassCast例外:

09-06 18:29:11.145: ERROR/AndroidRuntime(1031): FATAL EXCEPTION: main
09-06 18:29:11.145: ERROR/AndroidRuntime(1031): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.ListView.setupChild(ListView.java:1761)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.ListView.makeAndAddView(ListView.java:1730)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.ListView.fillDown(ListView.java:652)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.ListView.fillFromTop(ListView.java:709)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.ListView.layoutChildren(ListView.java:1580)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.view.View.layout(View.java:7035)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.view.View.layout(View.java:7035)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.view.View.layout(View.java:7035)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.view.View.layout(View.java:7035)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.view.View.layout(View.java:7035)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.os.Looper.loop(Looper.java:123)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at java.lang.reflect.Method.invoke(Method.java:521)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-06 18:29:11.145: ERROR/AndroidRuntime(1031):     at dalvik.system.NativeStart.main(Native Method)

其次, 我不清楚convertView和parent在方法中的作用是什么:

public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent)

public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) 

任何人都可以解释这些意义吗?

第三, 我想从XML中检索expandableListView的Id,但是没有创建ID,因为我们被迫将ID设置为android:id="@android:id/list"。我的意思是在R.id类中没有创建ID。

-Adithya

1 个答案:

答案 0 :(得分:0)

首先,以下似乎是错误的。

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) 
{
    //TextView childView = null;

    convertView = ((LayoutInflater.from(FoodieActivity.this)).inflate(R.layout.child_list, null));
    **convertView = (TextView) convertView.findViewById(R.id.childListId);**

    ((TextView)convertView).setText(getChild(groupPosition, childPosition).toString());

    return convertView;
}

这里转换视图最有可能是布局。你不能把它强制转换为textView。