jcaruso
jcaruso

Reputation: 2494

Custom ExpandableListView not showing fully

Edit: Null exception fixed.

After attempting to follow tutorial http://en.wikicode.org/index.php/Custom_ExpandableListView Only group 1 header is showing up with no children. No other headers of children are showing up. Why would it be cut off?

Here's my code:

public class WelcomeFragment extends Fragment {

    private UiLifecycleHelper uiHelper;
    WebView mWebView;
    TextView mName, mother, mlblName, mlblOther;
    ImageView mImage;
    LoginButton mAuthButton;


    final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;
    String[] permissions = { "user_relationships" };

    private ArrayList<String> groups;
    private ArrayList<ArrayList<ArrayList<String>>> childs;


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

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_welcome, null);

        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);

        mName = (TextView) view.findViewById(R.id.txtName);

        mother = (TextView) view.findViewById(R.id.txtOther);   
        mImage = (ImageView) view.findViewById(R.id.profilepicture);  
        mAuthButton = (LoginButton) view.findViewById(R.id.authButton);

        // Get the data
        Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null);

        if((c.moveToFirst()) && (c.getCount()>0))
        {
            mAuthButton.setVisibility(View.GONE);
            mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME))); //name


            //new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL)); //call asynctask
            mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER))); //significant_other


            AQuery aq = new AQuery(view);

           //returns the cached file by url, returns null if url is not cached
            File file = aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));

            if (file == null) {
                Log.i("PROJECTCARUSO", "Did not find user picture on file" );
                //load an image to an ImageView from network, cache image to file and memory
                aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));
            } else {
                Log.i("PROJECTCARUSO", "Found user picture on file" );
                Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                mImage.setImageBitmap(myBitmap);
            }

            Log.i("PROJECTCARUSO", "Found user information in database" );
        } else {

            mlblName = (TextView) view.findViewById(R.id.lblName);

            mlblOther = (TextView) view.findViewById(R.id.lblOther);

            //Hide everything
            mName.setVisibility(View.GONE);
            mother.setVisibility(View.GONE);
            mImage.setVisibility(View.GONE);
            mlblName.setVisibility(View.GONE);
            mlblOther.setVisibility(View.GONE);

            Log.i("PROJECTCARUSO", "Did not find user information in database" );
        }

        mAuthButton.setFragment(this);
        mAuthButton.setReadPermissions(Arrays.asList("email"));

        ExpandableListView l = (ExpandableListView) getActivity().findViewById(R.id.ExpandableListView01);

        loadData();

        myExpandableAdapter adapter = new myExpandableAdapter(getActivity(), groups, childs);
        l.setAdapter(adapter);     
        return view;
    }   

    public void refreshView(){

        // Get the data
        Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null);

        if((c.moveToFirst()) && (c.getCount()>0))
        {
            mAuthButton.setVisibility(View.GONE);


            //Show everything
            mName.setVisibility(View.VISIBLE);

            mother.setVisibility(View.VISIBLE);
            mImage.setVisibility(View.VISIBLE);
            mlblName.setVisibility(View.VISIBLE);
            mlblOther.setVisibility(View.VISIBLE);

            mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME))); //name


            //new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL)); //call asynctask
            mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER))); //significant_other


            AQuery aq = new AQuery(getActivity());

           //returns the cached file by url, returns null if url is not cached
            File file = aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));

            if (file == null) {
                Log.i("PROJECTCARUSO", "Did not find user picture on file" );
                //load an image to an ImageView from network, cache image to file and memory
                aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));
            } else {
                Log.i("PROJECTCARUSO", "Found user picture on file" );
                Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                mImage.setImageBitmap(myBitmap);
            }

            Log.i("PROJECTCARUSO", "Found user information in database" );
        } else {
            Log.i("PROJECTCARUSO", "Did not find user information in database" );
        }


    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {

            // Request user data and show the results
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        Utility.userUID = user.getId();
                        final String name = user.getName();
                        final String fname = user.getFirstName();
                        final String username = user.getUsername();
                        final String email = (String) user.getProperty("email");
                        String picURL = null;
                        String significant_other = null;
                        try {
                            String inputLine = user.getProperty("significant_other").toString();
                            JSONObject json = new JSONObject(inputLine.toString());
                            significant_other = json.get("name").toString();
                        } catch (JSONException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        picURL = "http://graph.facebook.com/"+Utility.userUID+"/picture";
                        Log.i("PROJECTCARUSO", "Logged in...");

                        try{            
                                ContentValues values = new ContentValues();
                                values.put(StatusData.KEY_USER_ROWID, Utility.userUID);
                                values.put(StatusData.KEY_USER_NAME, name);
                                values.put(StatusData.KEY_USER_FNAME, fname);
                                values.put(StatusData.KEY_USER_USERNAME, username);
                                values.put(StatusData.KEY_USER_EMAIL, email);
                                values.put(StatusData.KEY_USER_PICTURE, picURL);
                                values.put(StatusData.KEY_USER_OTHER, significant_other);


                                StatusData StatusData = new StatusData(getActivity());

                                StatusData.insertOrReplaceUser(values);

                                Log.i("PROJECTCARUSO", "Insert of facebook information was successfull ");
                                refreshView();
                        } catch (Exception e ) {
                            Log.i("PROJECTCARUSO", "Insert of facebook information was not successfull ");
                        }
                    }
                }
            });

        } else if (state.isClosed()) {
            Log.i("PROJECTCARUSO", "Logged out...");
            Log.i("PROJECTCARUSO", "Exception: " + exception);
        }
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    @Override
    public void onResume() {
        super.onResume();

        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        Session session = Session.getActiveSession();
        if (session != null &&
               (session.isOpened() || session.isClosed()) ) {
            onSessionStateChange(session, session.getState(), null);
        }

        uiHelper.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

    public class myExpandableAdapter extends BaseExpandableListAdapter {

        private ArrayList<String> groups;

        private ArrayList<ArrayList<ArrayList<String>>> children;

        private Context context;

        public myExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
            this.context = context;
            this.groups = groups;
            this.children = childs;
        }


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


        @Override
        public ArrayList<String> getChild(int groupPosition, int childPosition) {
            return children.get(groupPosition).get(childPosition);
        }

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


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

            String child = (String) ((ArrayList<String>)getChild(groupPosition, childPosition)).get(0);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_child, null);
            }

            TextView childtxt = (TextView) convertView.findViewById(R.id.TextViewChild01);

            childtxt.setText(child);

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children.get(groupPosition).size();
        }

        @Override
        public String getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }

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

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

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

            String group = (String) getGroup(groupPosition);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_group, null);
            }

            TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGroup);

            grouptxt.setText(group);

            return convertView;
        }

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

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

    }

    private void loadData(){
        groups= new ArrayList<String>();
        childs= new ArrayList<ArrayList<ArrayList<String>>>();

        groups.add("Group 1");
        groups.add("Group 2");
        groups.add("Group 3");

        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(0).add("Child 1 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(1).add("Child 2 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(2).add("Child 3 group 1");

        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(0).add("Child 1 group 2");
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(1).add("Child 2 group 2");
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(2).add("Child 3 group 2");

        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(0).add("Child 1 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(1).add("Child 2 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(2).add("Child 3 group 3");
    }
}

XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="6dip"
        android:layout_marginTop="20dp"
        android:background="#ffcccccc"
        android:textIsSelectable="false" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffcccccc" >

        <ImageView
            android:id="@+id/profilepicture"
            android:layout_width="100sp"
            android:layout_height="100sp"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/lblPicture"
            android:src="@drawable/photofemale" />

        <!-- Name -->

        <TextView
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lblName"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="20dp"
            android:layout_toRightOf="@+id/profilepicture"
            android:text="@string/txtname"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- significant_other -->

        <TextView
            android:id="@+id/lblOther"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtName"
            android:layout_below="@+id/txtName"
            android:text="@string/lblother"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- Logged in Credentials -->

        <TextView
            android:id="@+id/lblName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtName"
            android:layout_below="@+id/textView1"
            android:text="@string/lblname"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/txtOther"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/lblOther"
            android:layout_below="@+id/lblOther"
            android:text="@string/txtother"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- Logged in Credentials -->
        <com.facebook.widget.LoginButton
            android:id="@+id/authButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="6dip"
        android:layout_marginBottom="5dp"
        android:background="#ffcccccc"
        android:textIsSelectable="false" />


    <ExpandableListView 
        android:id="@+id/ExpandableListView01" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    >
    </ExpandableListView>

</LinearLayout>
</ScrollView>

For sanity: expandablelistview_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#666666"
>

<TextView
    android:id="@+id/TextViewChild01" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30px"
>
</TextView>

<TextView
    android:id="@+id/TextViewChild02" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
>
</TextView>

<TextView
    android:id="@+id/TextViewChild03" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
>
</TextView>

</LinearLayout>

expandablelistview_group.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
    android:id="@+id/TextViewGroup" 
    android:layout_width="wrap_content"
    android:layout_height="50px"
    android:layout_marginLeft="50px"
    android:gravity="center_vertical"
>
</TextView>

</LinearLayout>

Upvotes: 0

Views: 1904

Answers (1)

verybadalloc
verybadalloc

Reputation: 5798

There are two possible causes of your error:

  • There is no element with id ExpandableListView01 in your view
  • The view with id ExpandableListView01 is not of type ExpandableListView

In this case, my bet is on the first cause. Indeed, you are calling getViewById() on the activity, which has not loaded the layout yet. Replace this line:

ExpandableListView l = (ExpandableListView) getActivity().findViewById(R.id.ExpandableListView01);

by this line:

ExpandableListView l = (ExpandableListView) view.findViewById(R.id.ExpandableListView01);

Upvotes: 1

Related Questions