带图像的ExpandableListAdapter出错

时间:2016-03-18 18:39:25

标签: java android xml android-layout

我在尝试编译应用程序时仍然遇到相同的错误,应用程序必须在列表组中显示一个问题,并在每个组的子项中显示5个图像。我认为错误可能在上下文中。

MainActivity.java

public class QuestionsFragment extends Fragment {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<Integer>> listDataChild;

//private ArrayAdapter<String> mQuestionAdapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.questions_fragment, container, false);

    //Get the ListView
    expListView = (ExpandableListView) rootView.findViewById(R.id.list_view);

    //Prepare ListData
    prepareListData();

    listAdapter = new ExpandableListAdapter(QuestionsFragment.this.getActivity().getApplicationContext(), listDataHeader, listDataChild);

    expListView.setAdapter((android.widget.ExpandableListAdapter) listAdapter);


    // Inflate the layout for this fragment
    return rootView;
}


/*
 * Preparing the list data
 */
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<Integer>>();

    // Adding child data
    listDataHeader.add("Question 1");
    listDataHeader.add("Question 2");
    listDataHeader.add("Question 3");

    // Adding child data
    List<Integer> emoticons = new ArrayList<Integer>();
    emoticons.add(R.drawable.bad);
    emoticons.add(R.drawable.bad2);
    emoticons.add(R.drawable.normal);
    emoticons.add(R.drawable.good);
    emoticons.add(R.drawable.excelent);

    listDataChild.put(listDataHeader.get(0), emoticons); // Header, Child data
    listDataChild.put(listDataHeader.get(1), emoticons);
    listDataChild.put(listDataHeader.get(2), emoticons);
}
}

QuestionsFragment.java

 public class ExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<Integer>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<Integer>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

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


        //final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.emoticons_question_list_item, null);
        }


        ImageView lblListChild = (ImageView) convertView
                .findViewById(R.id.image_bad);

        ImageView lblListChild2 = (ImageView) convertView
                .findViewById(R.id.image_bad2);

        ImageView lblListNormal = (ImageView) convertView
                .findViewById(R.id.image_normal);

        ImageView lblListGood = (ImageView) convertView
                .findViewById(R.id.image_good);

        ImageView lblListExcelent = (ImageView) convertView
                .findViewById(R.id.image_excelent);

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.question_list_item_textview);

        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    public int getGroupCount() {
        return this._listDataHeader.size();
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.question_list_item, null);
        }

        TextView txtListGroup = (TextView) convertView
                .findViewById(R.id.question_list_item_textview);
        txtListGroup.setText(headerTitle);

        return convertView;
    }

    public boolean hasStableIds() {
        return false;
    }

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

ExpandableListAdapter.java

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

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@drawable/list_divider"
        android:dividerHeight="1dp"
        android:id="@+id/list_view">
    </ExpandableListView>
</FrameLayout>

questions_fragment.xml

    <?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"
    android:id="@+id/lists"
    android:background="@color/light_gray"
    android:padding="4dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="10dp"
        android:gravity="center"
        android:id="@+id/question_list_item_textview"
        android:textColor="@color/black"
        android:textSize="21dp">

    </TextView>

question_list_item.xml

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

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:stretchColumns="*">
        <!-- android:padding="2dp"> -->

        <TableRow>

            <ImageView
                android:layout_height="55dp"
                android:layout_width="55dp"
                android:id="@+id/image_bad"/>

            <ImageView
                android:layout_height="55dp"
                android:layout_width="55dp"
                android:id="@+id/image_bad2"/>

            <ImageView
                android:layout_height="55dp"
                android:layout_width="55dp"
                android:id="@+id/image_normal"/>

            <ImageView
                android:layout_height="55dp"
                android:layout_width="55dp"
                android:id="@+id/image_good"/>

            <ImageView
                android:layout_height="55dp"
                android:layout_width="55dp"
                android:src="@+id/image_excelent"/>

        </TableRow>

    </TableLayout>


</LinearLayout>

emoticons_question_list_item.xml

java.lang.RuntimeException: Unable to start activity ComponentInfo{br.ind.uptech.princessfeedback/br.ind.uptech.princessfeedback.MainActivity}: java.lang.ClassCastException: br.ind.uptech.princessfeedback.ExpandableListAdapter cannot be cast to android.widget.ExpandableListAdapter
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3139)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3238)
                                                                              at android.app.ActivityThread.access$1000(ActivityThread.java:197)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:145)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6862)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                           Caused by: java.lang.ClassCastException: br.ind.uptech.princessfeedback.ExpandableListAdapter cannot be cast to android.widget.ExpandableListAdapter
                                                                              at br.ind.uptech.princessfeedback.QuestionsFragment.onCreateView(QuestionsFragment.java:44)
                                                                              at android.app.Fragment.performCreateView(Fragment.java:2114)
                                                                              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
                                                                              at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
                                                                              at android.app.BackStackRecord.run(BackStackRecord.java:834)
                                                                              at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
                                                                              at android.app.Activity.performStart(Activity.java:6573)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3238) 
                                                                              at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:145) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6862) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 

这是它返回的错误:

{{1}}

非常感谢!

1 个答案:

答案 0 :(得分:0)

修改

expListView.setAdapter((android.widget.ExpandableListAdapter) listAdapter);

expListView.setAdapter(listAdapter);