我试图在我的expandableListView组标题上设置setOnLongClickListener
,以便在用户按住按钮时可以将其删除,为此,我在我的getGroupView
方法在我的适配器。它在方法的最底部:无法将OnOnLongClickListener设置为ExpandableListView组
@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;
}
但现在,我甚至不能创建组头,因为它给了我一个空对象引用。我不明白为什么,因为我的组头已经初始化了,当我命名它。另外,我不知道为什么它不会让我创建一个组,只是因为这一行代码,它只增加了一个功能,并没有重写其他任何东西。这里是堆栈跟踪:
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)
编辑我跟着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$2.onLongClick(ExpandableListViewAdapter.java:130)