-1

I'm hoping you guys can help. I tried using ExpandableListView in Fragment Issue to create my own sliding with expandablelistview and have been unsuccessful. I'm new to Java and Android and trying to figure it out. Thanks in advance.

LineupFragment.java

package com.example.expandabletest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class LineupFragment extends Fragment {

    View rootView;
    ExpandableListView lv;
    private String[] groups;
    private String[][] children;

    public LineupFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        groups = new String[] { "A318", "A319", "A320", "A321" };

        children = new String[][] {
                { "Seating Chart" },
                { "Seating Chart" },
                { "Seating Chart" },
                { "Seating Chart" }
        };
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_main,  container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        lv = (ExpandableListView) view.findViewById(R.id.expListView);
        lv.setAdapter(new ExpandableListAdapter(groups, children));
        lv.setGroupIndicator(null);
    }

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private final LayoutInflater inf;
        private String[] groups;
        private String[][] children;

        public ExpandableListAdapter(String[] groups, String[][] children) {
            this.groups = groups;
            this.children = children;

            inf = LayoutInflater.from(getActivity());
        }

        @Override
        public int getGroupCount() {
            return groups.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
             ViewHolder holder;
                if (convertView == null) {
                    convertView = inf.inflate(R.layout.list_item, parent, false);
                    holder = new ViewHolder();

                    holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }


                holder.text.setText(getChild(groupPosition, childPosition).toString());

                return convertView;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = inf.inflate(R.layout.list_group, parent, false);

                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.lblListHeader);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(getGroup(groupPosition).toString());

            ImageView Photo = (ImageView) convertView.findViewById(R.id.photo);


            //Images for all artists need to be hardcoded below

            if (groupPosition == 0) {
                Photo.setBackgroundResource(R.drawable.ic_launcher);
            }
            else if (groupPosition == 1) {
                Photo.setBackgroundResource(R.drawable.ic_launcher);
            }

            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        private class ViewHolder {
            TextView text;

        }

    }
}

ExpandableListAdapter.java

package com.example.expandabletest;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity _context;
    private List<String> _listDataHeader;  // header titles
    private HashMap<String, List<String>> _listDataChild; // child data in format of header title, child title

    public ExpandableListAdapter(Activity context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView,
            ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item,  null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group,  null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

MainActivity.java

package com.example.expandabletest;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/expListView"
        android:layout_width="match_parent"
        android:layout_height="310dp"
        android:layout_weight="0.14" >
    </ExpandableListView>

</LinearLayout>

list_group.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:fitsSystemWindows="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="6dip"
            android:paddingTop="6dip"
            android:textColor="#CC0000"
            android:textSize="20dp"
            android:textStyle="bold"
            tools:ignore="SpUsage" />
    </LinearLayout>

</LinearLayout>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="5dp"
        android:textColor="#ffffff"
        android:textSize="13sp" />

</LinearLayout>
Community
  • 1
  • 1
JWP
  • 195
  • 2
  • 10
  • Haven't been successful is not a good way of describing your problem, and you have a lot code there. – NameSpace Nov 14 '14 at 00:13
  • True. The app runs, the sliding menu works, and the xmls show up with the backgrounds, but the expandable view does not. I can't be any more specific because I truly don't know what the problem could be. Thanks – JWP Nov 14 '14 at 00:45

2 Answers2

1

You can check your program in debug mode. First,you must confirm your _listDataHeader and _listDataChild really have data; Second,your must confirm headerTitle and childText is not empty;

1

First of All, I recommend you understand the difference between fragments and activities. For Android an activity is as fixed screen (so to say) that remains persistent all throughout the usage of the app. On the other hand, a fragment can be removed, re-instated and changed during the usage of that app. Note also that a fragment needs to always be bound with an activity, but an activity can have many fragments. For more information about Activities and Fragments I would look at this

Having said that, this is the way you need to implement it:

Fragment Class

public static class LineupFragment extends Fragment {

View rootView;
ExpandableListView lv;
private String[] groups;
private String[][] children;


public LineupFragment() {

}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    groups = new String[] { "Test Header 1", "Test Header 2", "Test Header 3", "Test Header 4" };

    children = new String [][] {
        { "s simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." },
        { "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of comes from a line in section 1.10.32." },
        { "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)." },
        { "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc." }
    };
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_lineup, container, false);  

return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    lv = (ExpandableListView) view.findViewById(R.id.expListView);
    lv.setAdapter(new ExpandableListAdapter(groups, children));
    lv.setGroupIndicator(null);

}

Adapter Class

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private final LayoutInflater inf;
    private String[] groups;
    private String[][] children;

    public ExpandableListAdapter(String[] groups, String[][] children) {
        this.groups = groups;
        this.children = children;
        inf = LayoutInflater.from(getActivity());
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

         ViewHolder holder;
            if (convertView == null) {
                convertView = inf.inflate(R.layout.list_item, parent, false);
                holder = new ViewHolder();

                holder.text = (TextView) convertView.findViewById(R.id.lblListItem);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(getChild(groupPosition, childPosition).toString());

            return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inf.inflate(R.layout.list_group, parent, false);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.lblListHeader);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getGroup(groupPosition).toString());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ViewHolder {
        TextView text;
    }
}

Then I have two other lavout files for a custom layout.

One for the list_group.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_black"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:fitsSystemWindows="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:paddingLeft="6dip"
            android:paddingTop="6dip"
            android:textColor="#CC0000"
            android:textSize="20dp"
            android:textStyle="bold"
            tools:ignore="SpUsage" />

    </LinearLayout>

</LinearLayout>

And the other for the list_item.xml:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="5dp"
        android:textSize="13sp" />

</LinearLayout>

The activity class needs to be something like the following:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
Community
  • 1
  • 1
Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
  • Michele,Thank you for your feedback. However, I have recreated what you attached and when the app runs everything loads except for the expandable list. I must be doing something wrong, but can't figure it out. Also, you have inf = LayoutInflater.from(getActivity()); in the adapter class, but I don't see the method anywhere. Eclipse added it in for me, but it's empty. Is that correct? Thanks – JWP Nov 15 '14 at 02:37
  • can you update all the code you are using the question please for us at the SO community to be able to resolve it? – Michele La Ferla Nov 17 '14 at 08:46
  • It's you same code. All I did was create a new project and copy everything over to test on it's own before trying to implement it into my project. – JWP Nov 17 '14 at 20:49
  • Ok i will take a look tomorrow – Michele La Ferla Nov 17 '14 at 21:17
  • Your code is not exactly as indicated. I am sharing the correct classes for you to check it out. XML is correct as far as i can see. Adapter class - https://www.dropbox.com/s/md7x175zvoqdkwg/ExpandableListAdapter.java?dl=0 Actual Fragment - https://www.dropbox.com/s/f77a2rk0w45atsv/LineupFragment.java?dl=0 The main activity is ok. – Michele La Ferla Nov 18 '14 at 06:55
  • Thank you for that, but for the life of me, I can't figure out what I'm doing wrong. I have created a new project and done everything as your code. And again all I get is a black screen. – JWP Nov 18 '14 at 19:16
  • Ok, not sure what was happening, because the code won't work in a standalone project, but worked just fine when I added it to my actual app. Thanks for the help, Michele. – JWP Nov 19 '14 at 11:16
  • Must have been a slow emulator then :) could you accept and upvote please? – Michele La Ferla Nov 19 '14 at 11:17