如何解决问题:“java.lang.RuntimeException:对于ExpandableListView,使用setAdapter(ExpandableListAdapter)而不是setAdapter(ListAdapter)”

时间:2015-11-27 09:59:45

标签: java android

我在我的ExpandableListView课程中使用fragment,在运行时我遇到错误,

 `java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)` on below lines

在下面的代码useroccasion是我的适配器,occasionview是我的可扩展列表视图

 userOccasions = new UserOccasions(getActivity(), R.layout.invitation, eventMOs);

 occasionView.setAdapter(userOccasions);

我的可展开列表视图的布局代码如下,

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

    <ExpandableListView
        android:id="@+id/invitation_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbarStyle="outsideOverlay" />

</LinearLayout>

我的片段代码如下,

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
        savedInstanceState) {
    this.inflater = inflater;
    this.container = container;
    this.savedInstanceState = savedInstanceState;
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    final View view = inflater.inflate(R.layout.invitation_tab, container, false);
    context = getActivity().getApplicationContext();
    prgDialog = new ProgressDialog(getActivity());
    eventId.clear();
    eventName.clear();
    eventPlace.clear();
    prgDialog.setMessage("Please wait...");
    prgDialog.setCancelable(false);

    dbHelper = new DatabaseHelper(context);
    userMO = dbHelper.getUserData(1);
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... arg0) {
            return eventDelegates.getAllInvites(userMO, context);
        }

        @Override
        protected void onPostExecute(String eventLists) {
            if (eventLists != "null") {
                eventMOs = gson.fromJson(eventLists, new TypeToken<List<EventMO>>() {
                }.getType());
                Toast.makeText(context, "total items of eventMo" + eventMOs.size(), Toast.LENGTH_LONG).show();

                for (EventMO eventMO : eventMOs) {
                    eventName.add(eventMO.getText());
                    eventId.add(eventMO.getEventId());
                    eventPlace.add(eventMO.getPlace());
                }

                prgDialog.dismiss();
                occasionView = (ExpandableListView) view.findViewById(R.id.invitation_list_view);
                occasionView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                    @Override
                    public void onGroupExpand(int groupPosition) {

                        if (groupPosition != lastExpandedGroupPosition) {
                            occasionView.collapseGroup(lastExpandedGroupPosition);

                        }
                        lastExpandedGroupPosition = groupPosition;

                    }
                });
                userOccasions = new UserOccasions(getActivity(), R.layout.invitation, eventMOs);
                occasionView.setAdapter(userOccasions);
                occasionView.setItemsCanFocus(false);
                occasionView.setTextFilterEnabled(true);
                occasionView.setOnItemClickListener(occasionView.getOnItemClickListener());
            }
        }
    }.execute(null, null, null);

    return view;
}
private class UserOccasions extends ArrayAdapter<EventMO> {
        private ArrayList<EventMO> eventMOs;
        LayoutInflater mInflater;

        UserOccasions(Context context, int textViewResourceId,
                      ArrayList<EventMO> eventMOs) {
            super(context, textViewResourceId, eventMOs);
            this.eventMOs = new ArrayList<EventMO>();
            this.eventMOs.addAll(eventMOs);
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        private class ViewHolder {
            TextView eventNameTxtV;
            TextView eventPlaceTxtV;
            CheckBox cb;
        }

        @Override
        public int getCount() {
            return eventMOs.size();
        }
       @Override
        public long getItemId(int position) {
            return 0;
        }
 @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (null == convertView) {
                convertView = mInflater.inflate(R.layout.invitation, null);
                holder = new ViewHolder();
                holder.eventNameTxtV = (TextView) convertView.findViewById(R.id.invitationTitle);
                holder.eventPlaceTxtV = (TextView) convertView.findViewById(R.id.invitationPlace);
                holder.cb = (CheckBox) convertView.findViewById(R.id.invitationCheckBox);

                holder.cb.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        CheckBox checkBox = (CheckBox) view;
                        EventMO eventMO = (EventMO) checkBox.getTag();
                        eventMO.setSelected(checkBox.isChecked());

                    }
                });
                convertView.setTag(holder);
                holder.cb.setTag(eventMOs.get(position));
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.eventNameTxtV.setText(eventMOs.get(position).getText());
            holder.eventPlaceTxtV.setText(eventMOs.get(position).getPlace());
            holder.cb.setChecked(eventMOs.get(position).isSelected());

            View v = convertView.findViewById(R.id.invitation_single);

            final LinearLayout eventLayout = (LinearLayout) convertView.findViewById(R.id.invitation_single);
            //invitationCheckBox = (CheckBox) convertView.findViewById(R.id.invitationCheckBox);

            final Button yesBtn = (Button) convertView.findViewById(R.id.yesButton);
            final Button noBtn = (Button) convertView.findViewById(R.id.noButton);
            final Button maybeBtn = (Button) convertView.findViewById(R.id.buttonMayBe);

            final LinearLayout responseLayout = (LinearLayout) convertView.findViewById(R.id.hidden);
            eventLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.invitation_single:
                            responseLayout.setVisibility(View.VISIBLE);
                            break;
                    }
                }


            });

            return convertView;

        }

1 个答案:

答案 0 :(得分:1)

使用BaseExpandableListAdapter代替ArrayAdapter

更改:private class UserOccasions extends ArrayAdapter<EventMO>

收件人:private class UserOccasions extends BaseExpandableListAdapter ,并实现方法

教程:

http://examples.javacodegeeks.com/android/core/ui/expandablelistview/android-expandablelistview-example/

http://www.vogella.com/tutorials/AndroidListView/article.html

也许您想使用通常的RecyclerView?