Tell me please .. made my markup for group headers in ExpandableListView . In the adapter wrote the following:

 public class ExpListAdapter extends SimpleCursorTreeAdapter { DBHeler dbHeler; private int layout; public ExpListAdapter(Context context, Cursor cursor, int groupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo) { super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo); this.layout = childLayout; } @Override public View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layout, parent, false); ViewHolder viewHolder = new ViewHolder(view); view.setTag(viewHolder); return view; } @Override protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) { super.bindGroupView(view, context, cursor, isExpanded); ViewHolder holder = (ViewHolder) view.getTag(); String category = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_NAME)); holder.txtCategory.setText(category); } } 

I get the error:

Process: test.myapp, PID: 6572 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence) (ExpListAdapter.java:82)

    1 answer 1

    The error is caused by the fact that you specify the wrong markup for a custom type of group:

     public class ExpListAdapter extends SimpleCursorTreeAdapter { DBHeler dbHeler; private int layoutChild; ptivate int layoutGroup; public ExpListAdapter(Context context, Cursor cursor, int groupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo) { super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo); dbHeler = new DBHeler(context); layoutChild = childLayout; layoutGroup = groupLayout; } @Override public View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layoutGroup, parent, false); // разметка групп ViewHolder viewHolder = new ViewHolder(view); view.setTag(viewHolder); return view; } @Override public View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layoutChild, parent, false); // разметка вложенных ViewHolder viewHolder = new ViewHolder(view); view.setTag(viewHolder); return view; } } 

    To change the type of the group disclosure indicator, you need to draw two types of this indicator: closed and open, then write a selector, let's call its indicator.xml:

     <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_expanded="true" android:drawable="@drawable/group_indicator_expanded" /> <item android:drawable="@drawable/group_indicator" /> </selector> 

    file is placed in the folder res / drawable

    Now in the markup of your ExpandableListView indicate this indicator:

     <ExpandableListView android:id="@+id/expandableListView1" android:layout_width="match_parent" android:layout_height="match_parent" android:groupIndicator="@drawable/indicator" > </ExpandableListView 

    >

    • Happened. Thank. The last little question. I have this indicator stretched to the full height of the group, is it possible to make it not stretch, but be the same size as it was? - Veronica
    • @ Veronica is doing a 9-patch format indicator. Specify the areas of stretching - the edges of your indicator, where there is no image, so that empty space is stretched. - pavlofff
    • @ Veronica to expand the first group expdListView.expandGroup(0); - pavlofff
    • created a question. - Veronica