简体   繁体   中英

How to populate an expandable listview in Android

I'm working with Expandable ListView for the first time and I found an example here:

http://android-adda.blogspot.ro/2011/06/custom-expandable-listview.html

I went through it, adapted it to my needs but the app would crash when applying the adapter. I copy-pasted the entire example, using even the given String arrays thinking I might be doing something wrong on that side.

Still, the error is the same.

Below you have the code for my activity + the custom adapter class included there. I've taken out non-related things like onClickListeners to other elements for obvious reasons.

///////////////////////////////////////////////
    public String[] parentArray = {
           "India",
           "Australia",
           "England",
           "South Africa"
         };
    public String[][] childArray = {
               {
                   "Sachin Tendulkar",
                   "Raina",
                   "Dhoni",
                   "Yuvi"
                    },
                    {
                   "Ponting",
                   "Adam Gilchrist",
                   "Michael Clarke"
                    },
                    {
                   "Andrew Strauss",
                   "kevin Peterson",
                   "Nasser Hussain"
                    },
                    {
                   "Graeme Smith",
                   "AB de villiers",
                   "Jacques Kallis"
                    }
                     };
    ///////////////////////////////////////////////

    public void onCreate(Bundle savedInstanceState) {   
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewschedule);

        getViews();
        setSpinnerAdapter();
        listview.setAdapter(new ExpAdapter(this));

    }

    public class ExpAdapter extends BaseExpandableListAdapter {

      private Context myContext;
      public ExpAdapter(Context context) {
       myContext = context;
      }
      public Object getChild(int groupPosition, int childPosition) {
       return null;
      }

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

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

       if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.childrow, null);
       }

       TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
       tvPlayerName.setText(childArray[groupPosition][childPosition]);

       return convertView;
      }

      public int getChildrenCount(int groupPosition) {
       return childArray[groupPosition].length;
      }

      public Object getGroup(int groupPosition) {
       return null;
      }

      public int getGroupCount() {
       return parentArray.length;
      }

      public long getGroupId(int groupPosition) {
       return 0;
      }

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

       if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.parentrow, null);
       }

       TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
       tvGroupName.setText(parentArray[groupPosition]);

       return convertView;
      }

      public boolean hasStableIds() {
       return false;
      }

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

Below this you have my log saying "class not found" mostly.

07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.View.layout(View.java:11180)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.ViewGroup.layout(ViewGroup.java:4203)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.View.layout(View.java:11180)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.ViewGroup.layout(ViewGroup.java:4203)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.os.Looper.loop(Looper.java:137)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.app.ActivityThread.main(ActivityThread.java:4340)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at java.lang.reflect.Method.invoke(Method.java:511)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at dalvik.system.NativeStart.main(Native Method)
07-19 12:34:09.760: E/AndroidRuntime(2889): Caused by: java.lang.ClassNotFoundException: android.view.linearlayout
07-19 12:34:09.760: E/AndroidRuntime(2889):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
07-19 12:34:09.760: E/AndroidRuntime(2889):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)

My getViews() method:

private void getViews(){
        font = Typeface.createFromAsset(getAssets(), "arialnb.ttf");

        groupInput = (EditText) findViewById(R.id.editText1);
        groupInput.setTextColor(getResources().getColor(R.color.lightGrey));
        groupInput.setText("Grupa: (ex: 10201b)");

        titleText = (TextView) findViewById(R.id.textView1);
        titleText.setTypeface(font);

        editDefault = (Button) findViewById(R.id.button2);

        homeImage = (ImageView) findViewById(R.id.imageView2);

        daySpinner = (Spinner) findViewById(R.id.spinner1);

        listview = (ExpandableListView) findViewById(R.id.expandableListView1);
        listview.setVerticalFadingEdgeEnabled(false);
    }

The entire ViewSchedule.xml:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:background="@color/aceDarkBlue" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/orarbuton" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dp"
                android:layout_toRightOf="@+id/imageView1"
                android:text="@string/aceOrar"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/homebuton" />
        </RelativeLayout>


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:text="@string/editDefault" />

        <RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/relativeLayout1"
            android:layout_marginTop="14dp" >

            <LinearLayout
                android:layout_width="0px"
                android:layout_height="0px"
                android:focusable="true"
                android:focusableInTouchMode="true" >
            </LinearLayout>

            <Spinner
                android:id="@+id/spinner1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" />


            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/spinner1"
                android:ems="10" />

        </RelativeLayout>



        <ExpandableListView
            android:id="@+id/expandableListView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/relativeLayout2" >

        </ExpandableListView>

    </RelativeLayout>

</LinearLayout>

Any sort of help with whatever I'm currently missing would be greatly appreciated...

I solved this, thanks to Tim who had the right idea that I clean up the project.

I have 2 separate layout folders for this app, a landscape and a portrait folder. For whatever reason, the TextView path in the portrait folder, in the parentrow.xml was broken.

All I had to do was click on "Change to TextView" in the GUI editor and it worked fine.

Worth mentioning that this error was not signaled by Eclipse and I was only aware of it once I started going through the xml files by hand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM