无法将 OnLongClickListener 设置为 ExpandableListView 组

Unable to setOnLongClickListener to a ExpandableListView group

我正在尝试在我的 expandableListView 组 header 上设置一个 setOnLongClickListener 以便能够在用户按住按钮时删除它们,为此,我已经实现了以下是我适配器中 getGroupView 方法中的代码。它在方法的最底部:

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View view, final ViewGroup viewGroup) {
    View vi = view;
    if (vi == null) {

        LayoutInflater groupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = groupInflater.inflate(R.layout.parent_row, viewGroup, false);
    }

    final Semester semester = getGroup(groupPosition);

    TextView textView = (TextView) vi.findViewById(R.id.semester_view);

    textView.setText(semester.getName());
    textView.setPadding(100, 0, 0, 0);
    textView.setTextColor(Color.BLACK);
    textView.setTextSize(20);

    final Button button = (Button) vi.findViewById(R.id.add_course_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            View dialogView = LayoutInflater.from(context).inflate(R.layout.child_dialog_layout, null);
            final EditText et_course = (EditText)dialogView.findViewById(R.id.et_course);

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Create A Course");
            builder.setView(dialogView);
            builder.setCancelable(false);

            builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Course course = new Course(et_course.getText().toString());
                    getAllData().get(groupPosition).getCourses().add(course);
                    notifyDataSetChanged();
                }
            });
            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();
        }
    });

    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            getAllData().remove(groupPosition);
            return false;
        }
    });
    return vi;
}

但是现在,我什至无法创建组 header,因为它给了我一个空的 object 引用。我不明白为什么,因为我的组 header 在我命名时已经初始化了。 Morever,我不知道为什么它不会让我仅仅因为这行代码就创建一个组,它只添加了一个功能而没有覆盖其他任何东西。这是堆栈跟踪:

FATAL EXCEPTION: main
  Process: myapp.onur.expandablelistviewexample, PID: 26569
  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnLongClickListener(android.view.View$OnLongClickListener)' on a null object reference
      at myapp.onur.expandablelistviewexample.ExpandableListViewAdapter.getGroupView(ExpandableListViewAdapter.java:127)
      at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)

EDIT 我听从了 KeLiuyue 的建议,现在我可以生成组了,但是 onLongClick 给了我这个堆栈跟踪:

FATAL EXCEPTION: main
    Process: myapp.onur.expandablelistviewexample, PID: 31734
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
      at java.util.ArrayList.remove(ArrayList.java:477)
      at myapp.onur.expandablelistviewexample.ExpandableListViewAdapter.onLongClick(ExpandableListViewAdapter.java:130)

试试这个:

final int position = groupPosition;

    vi.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                long packedPosition = position;
                int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
                int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
                // childPosition = -1 ,Group was clicked.
                if (childPosition != -1) {
                    // group was clicked ,you can do something here.
                } else {
                    // children was clicked,you can do something here.
                }

                // edited ,getAllData() didn't have data.So you need to judge.
                if (getAllData().size() != 0 && getAllData() != null) {
                    getAllData().remove(groupPosition);
                    notifyDataSetChanged();
                }
                return true;
            }
    });

只需将view更改为vi

你也可以在外面做。

mExpandableListView = (ExpandableListView) findViewById(R.id.list_view);
    mExpandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

            long packedPosition = mExpandableListView.getExpandableListPosition(i);
            int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
            int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
            // childPosition = -1 ,Group was clicked.
            if (childPosition != -1) {
                // group wa clicked ,you can do something here.
            } else {
                // children was clicked,you can do something here.
            }
            return true;
        }
});

基本上我所做的是在我的 Mainactivity 中调用 expandableListView.setOnItemLongClickListener 并删除额外的 if 语句。像这样:

expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                long packedPosition = expandableListView.getExpandableListPosition(position);
                int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);

                if (adapter.getAllData().size() != 0 && adapter.getAllData() != null) {
                    adapter.getAllData().remove(groupPosition);
                    adapter.notifyDataSetChanged();
                    adapter.notifyDataSetChanged();

                }
                return false;
            }
        });