0

私は 2 つのクラスを持っています。1 つはアダプターで、2 つ目はアクティビティです。別のアイテムをクリックすると別のクラスを開くためのコードを見つけた後、アプリが停止し、アイテムをクリックすることさえできません。1 つの項目をクリックすると、強調表示されず、アプリが停止します。

これが私のアダプターです

@SuppressWarnings("unchecked")
public class NewAdapter extends BaseExpandableListAdapter {

    public ArrayList<String> groupItem, tempChild;
    public ArrayList<Object> Childtem = new ArrayList<Object>();
    public LayoutInflater minflater;
    public Activity activity;

    public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) {
        groupItem = grList;
        this.Childtem = childItem;
    }

    public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        tempChild = (ArrayList<String>) Childtem.get(groupPosition);
        TextView text = null;
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.childrow, null);
        }
        text = (TextView) convertView.findViewById(R.id.textView1);
        text.setText(tempChild.get(childPosition));
        /*convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, tempChild.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });*/
        return convertView;
    }

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

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

    @Override
    public int getGroupCount() {
        return groupItem.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.grouprow, null);
        }
        ((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
        //((CheckedTextView) convertView).setChecked(isExpanded);
        return convertView;
    }

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

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

私のアクティビティ-最初の2つだけが機能することを望んでいます。親と子が増えていますが、これらが機能する場合は、残りの部分についても同じことを行います。

public class Mecanica  extends ExpandableListActivity implements OnChildClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExpandableListView expandbleLis = getExpandableListView();
        expandbleLis.setDividerHeight(2);
        expandbleLis.setGroupIndicator(null);
        expandbleLis.setClickable(true);



        setGroupData();
        setChildGroupData();

        NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
        mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
        getExpandableListView().setAdapter(mNewAdapter);
        expandbleLis.setOnChildClickListener(this);
    }
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        // Create a switch that switches on the specific child position.
        switch(childPosition) {
        case 0:
            // Go to child #0 specific class.
            Intent child0Intent = new Intent(this, Mecanica1.class);
            startActivity(child0Intent);
            break;
        case 1:
            // Go to child #1 specific class.
            Intent child1Intent = new Intent(this, Mecanica2.class);
            startActivity(child1Intent);
            break;
        case 2:
            // Go to child #1 specific class.
            Intent child2Intent = new Intent(this, Mecanica3.class);
            startActivity(child2Intent);
            break;
        case 3:
            // Go to child #1 specific class.
            Intent child3Intent = new Intent(this, Mecanica4.class);
            startActivity(child3Intent);
            break;
        case 4:
            // Go to child #1 specific class.
            Intent child4Intent = new Intent(this, Mecanica5.class);
            startActivity(child4Intent);
            break;
        case 5:
            // Go to child #1 specific class.
            Intent child5Intent = new Intent(this, Mecanica6.class);
            startActivity(child5Intent);
            break;
        case 6:
            // Go to child #1 specific class.
            Intent child6Intent = new Intent(this, Mecanica7.class);
            startActivity(child6Intent);
            break;
        case 7:
            // Go to child #1 specific class.
            Intent child7Intent = new Intent(this, Mecanica8.class);
            startActivity(child7Intent);
            break;

        }
        return false;   
}


    public void setGroupData() {
        groupItem.add("Directia");
        groupItem.add("Franarea");
       // groupItem.add("Motorul");
        //groupItem.add("Rotile");
        //groupItem.add("Siguranta si control");
        //groupItem.add("Suspensia");
        //groupItem.add("Transmisia");
    }

    ArrayList<String> groupItem = new ArrayList<String>();
    ArrayList<Object> childItem = new ArrayList<Object>();

    public void setChildGroupData() {
        /**
         * Add Data For TecthNology
         */
        ArrayList<String> child = new ArrayList<String>();
        child.add("Java");
        child.add("Drupal");
       child.add(".Net Framework");
        child.add("PHP");
        childItem.add(child);

        /**
         * Add Data For Mobile
         */
     child = new ArrayList<String>();
       child.add("Android");
       child.add("Window Mobile");
       child.add("iPHone");
        child.add("Blackberry");
        childItem.add(child);
//        /**
//         * Add Data For Manufacture
//         */
//        child = new ArrayList<String>();
//        child.add("HTC");
//        child.add("Apple");
//        child.add("Samsung");
//        child.add("Nokia");
//        childItem.add(child);
//        /**
//         * Add Data For Extras
//         */
//        child = new ArrayList<String>();
//        child.add("Contact Us");
//        child.add("About Us");
//        child.add("Location");
//        child.add("Root Cause");
//        childItem.add(child);
    }    
}

そしてログキャット

04-13 19:44:54.753: E/AndroidRuntime(1724): FATAL EXCEPTION: main
04-13 19:44:54.753: E/AndroidRuntime(1724): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-13 19:44:54.753: E/AndroidRuntime(1724):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at java.util.ArrayList.get(ArrayList.java:304)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at com.example.vreaucarnet.NewAdapter.getChildrenCount(NewAdapter.java:65)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:562)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.os.Handler.handleCallback(Handler.java:725)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.os.Looper.loop(Looper.java:137)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-13 19:44:54.753: E/AndroidRuntime(1724):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0
        Mecanica.java

    package com.example.expademo;

    import java.util.ArrayList;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ExpandableListActivity;
    import android.content.Context;
    import android.content.Intent;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ExpandableListView;
    import android.widget.ExpandableListView.OnChildClickListener;
    import android.widget.ListView;

    public class Mecanica extends ExpandableListActivity implements
            OnChildClickListener {
        ArrayList<String> groupItem = new ArrayList<String>();
        ArrayList<Object> childItem = new ArrayList<Object>();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            ExpandableListView expandbleLis = getExpandableListView();
            expandbleLis.setDividerHeight(2);
            expandbleLis.setGroupIndicator(null);
            expandbleLis.setClickable(true);

            setGroupData();
            setChildGroupData();

            NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
            mNewAdapter
                    .setInflater(
                            (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                            this);
            getExpandableListView().setAdapter(mNewAdapter);
            expandbleLis.setOnChildClickListener(this);
        }

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // Create a switch that switches on the specific child position.
            switch (childPosition) {
            case 0:
                // Go to child #0 specific class.
                Intent child0Intent = new Intent(this, MainActivity.class);
                startActivity(child0Intent);
                break;
            case 1:
                // Go to child #1 specific class.
                Intent child1Intent = new Intent(this, MainActivity.class);
                startActivity(child1Intent);
                break;
            case 2:
                // Go to child #1 specific class.
                Intent child2Intent = new Intent(this, MainActivity.class);
                startActivity(child2Intent);
                break;
            case 3:
                // Go to child #1 specific class.
                Intent child3Intent = new Intent(this, MainActivity.class);
                startActivity(child3Intent);
                break;
            case 4:
                // Go to child #1 specific class.
                Intent child4Intent = new Intent(this, MainActivity.class);
                startActivity(child4Intent);
                break;
            case 5:
                // Go to child #1 specific class.
                Intent child5Intent = new Intent(this, MainActivity.class);
                startActivity(child5Intent);
                break;
            case 6:
                // Go to child #1 specific class.
                Intent child6Intent = new Intent(this, MainActivity.class);
                startActivity(child6Intent);
                break;
            case 7:
                // Go to child #1 specific class.
                Intent child7Intent = new Intent(this, MainActivity.class);
                startActivity(child7Intent);
                break;

            }
            return false;
        }

        public void setGroupData() {
            groupItem.add("Directia");
            groupItem.add("Franarea");
            groupItem.add("Motorul");
            groupItem.add("Rotile");
            groupItem.add("Siguranta si control");
            groupItem.add("Suspensia");
            groupItem.add("Transmisia");
        }

        public void setChildGroupData() {
            /**
             * Add Data For TecthNology
             */
            ArrayList<String> child = new ArrayList<String>();
            child.add("Java");
            child.add("Drupal");
            child.add(".Net Framework");
            child.add("PHP");
            childItem.add(child);

            /**
             * Add Data For Mobile
             */
            child = new ArrayList<String>();
            child.add("Android");
            child.add("Window Mobile");
            child.add("iPHone");
            child.add("Blackberry");
            childItem.add(child);
            // /**
            // * Add Data For Manufacture
            // */
            child = new ArrayList<String>();
            child.add("HTC");
            child.add("Apple");
            child.add("Samsung");
            child.add("Nokia");
            childItem.add(child);
            // /**
            // * Add Data For Extras
            // */
            child = new ArrayList<String>();
            child.add("Contact Us");
            child.add("About Us");
            child.add("Location");
            child.add("Root Cause");
            childItem.add(child);

            // /**
            // * Add Data For Extras
            // */
            child = new ArrayList<String>();
            child.add("Contact Us");
            child.add("About Us");
            child.add("Location");
            child.add("Root Cause");
            childItem.add(child);
            // /**
            // * Add Data For Extras
            // */
            child = new ArrayList<String>();
            child.add("Contact Us");
            child.add("About Us");
            child.add("Location");
            child.add("Root Cause");
            childItem.add(child);
            // /**
            // * Add Data For Extras
            // */
            child = new ArrayList<String>();
            child.add("Contact Us");
            child.add("About Us");
            child.add("Location");
            child.add("Root Cause");
            childItem.add(child);
        }
    }


    NewAdapter.java

    package com.example.expademo;

    import java.util.ArrayList;

    import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.CheckedTextView;
    import android.widget.TextView;

    @SuppressWarnings("unchecked")
    public class NewAdapter extends BaseExpandableListAdapter {

        public ArrayList<String> groupItem, tempChild;
        public ArrayList<Object> Childtem = new ArrayList<Object>();
        public LayoutInflater minflater;
        public Activity activity;

        public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) {
            groupItem = grList;
            this.Childtem = childItem;
        }

        public void setInflater(LayoutInflater mInflater, Activity act) {
            this.minflater = mInflater;
            activity = act;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

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

        @Override
        public View getChildView(int groupPosition, final int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            tempChild = (ArrayList<String>) Childtem.get(groupPosition);
            TextView text = null;
            if (convertView == null) {
                convertView = minflater.inflate(R.layout.childrow, null);
            }
            text = (TextView) convertView.findViewById(R.id.textView1);
            text.setText(tempChild.get(childPosition));
            /*
             * convertView.setOnClickListener(new OnClickListener() {
             * 
             * @Override public void onClick(View v) { Toast.makeText(activity,
             * tempChild.get(childPosition), Toast.LENGTH_SHORT).show(); } });
             */
            return convertView;
        }

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

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

        @Override
        public int getGroupCount() {
            return groupItem.size();
        }

        @Override
        public void onGroupCollapsed(int groupPosition) {
            super.onGroupCollapsed(groupPosition);
        }

        @Override
        public void onGroupExpanded(int groupPosition) {
            super.onGroupExpanded(groupPosition);
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = minflater.inflate(R.layout.grouprow, null);
            }
            TextView text = (TextView) convertView.findViewById(R.id.textView);
            text.setText(groupItem.get(groupPosition));
            // ((CheckedTextView) convertView).setChecked(isExpanded);
            return convertView;
        }

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

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

    grouprow.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:orientation="vertical" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

activity_main.xml

<LinearLayout 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:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</LinearLayout>
于 2013-04-14T12:38:35.067 に答える