mikeappell
mikeappell

Reputation: 403

Problems with behavior of ExpandableListView and ExpandableListAdapter

So, I created a custom ExpandableListAdapter because I wanted an expandable list capable of having multiple children, essentially a two-tiered list. It's been working perfectly fine, as long as it was the first and only View in the Activity. However, I recently added some other Views (a set of TextViews) to the LinearLayout it's in, and all of a sudden it's stopped displaying at all, without giving any errors.

First, I'm wondering if it is, for some reason, required to be the first/only view in the Activity, and if so, if there's any way of getting around that. Code is below.

expListView = (ExpandableListView) this.findViewById(R.id.finalcalculation_list_view);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
        this, groupList, totalCollection);
expListView.setAdapter(expListAdapter);

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

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" > 

        <!-- Here are some TextViews -->

    </LinearLayout>

    <ScrollView android:id="@+id/finalcalculation_scrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

        <ExpandableListView
        android:id="@+id/finalcalculation_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        </ExpandableListView>

    </ScrollView>

</LinearLayout>

Here is how it was originally, when it was last working:

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

    <ExpandableListView
    android:id="@+id/finalcalculation_list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    </ExpandableListView>

</LinearLayout>

And here is the unfortunately large code-dump of my ExpandableListAdapter.

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity context;
    private Map<String, List<String>> collections;
    private List<String> groups;

    public ExpandableListAdapter(Activity context, List<String> groups,
            Map<String, List<String>> collections) {
        this.context = context;
        this.collections = collections;
        this.groups = groups;
    }

    public Object getChild(int groupPosition, int childPosition) {
        return collections.get(groups.get(groupPosition)).get(childPosition);
    }

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

    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final String laptop = (String) getChild(groupPosition, childPosition);
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.finalcalculation_child_item, null);
        }

        TextView item = (TextView) convertView.findViewById(R.id.finalcalculation_child_item_textview);

        item.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Do you want to remove?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                List<String> child = 
                                    collections.get(groups.get(groupPosition));
                                child.remove(childPosition);
                                notifyDataSetChanged();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });

        item.setText(laptop);
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return collections.get(groups.get(groupPosition)).size();
    }

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

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

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

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String itemName = (String) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater groupInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = groupInflater.inflate(R.layout.finalcalculation_group_item, null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.finalcalculation_group_item_textview);
        item.setTypeface(null, Typeface.BOLD);
        item.setText(itemName);
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

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

I've tried changing the Inflators for both getChildView and getGroupView from

convertView = inflater.inflate(R.layout.finalcalculation_child_item, null);

to both

convertView = inflater.inflate(R.layout.finalcalculation_child_item, parent, false); //AND
convertView = inflater.inflate(R.layout.finalcalculation_child_item, parent, true); //AND

But it either doesn't work, or throws an error.

All help and input greatly appreciated.

Upvotes: 0

Views: 813

Answers (1)

rubenlop88
rubenlop88

Reputation: 4221

The layout_height of the nested LinearLayout is set to match_parent, which causes the nested LinearLayout to be the same height of the parent LinearLayout and hides the ScrollView. It should be something like wrap_content or a fixed size. Also, you don't need a ScrollView.

Try something like this:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" > 

        <!-- Here are some TextViews -->

    </LinearLayout>

   <ExpandableListView
       android:id="@+id/finalcalculation_list_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

    </ExpandableListView>

</LinearLayout>

Upvotes: 4

Related Questions