2017-08-08 46 views
1

我试图在我的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) 

回答

3

试试这个:

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; 
     } 
}); 
0

基本上我所做的就是打电话给我Mainactivty的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; 
      } 
     });