Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpandableListView is always collapsed

I am trying to build basic android app using ExpandableListView. After putting data into view I only see the groups, if I click on groups they don't expand.

Here is my code, can someone please help?

Adapter



    public class PhraseAdapter extends BaseExpandableListAdapter {

        private LayoutInflater inflater;

        private List data;

        public PhraseAdapter(Context context) {
            inflater = LayoutInflater.from(context);

        }

        public void setData(List data) {
            this.data = data;
        }

        public View inflate(int viewId) {
            View view = inflater.inflate(viewId, null);
            return view;
        }

        public void updateView(List categories) {
            setData(categories);
            notifyDataSetChanged();
        }

        static class ViewHolder {
            TextView name;
        }

        static class ViewChildHolder {
            TextView phonetix;
            TextView orginal;
        }

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

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

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            final ViewChildHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.phrases__extra_item, null);
                holder = new ViewChildHolder();
                holder.phonetix = (TextView) convertView
                        .findViewById(R.id.phrases_phonetix);
                holder.orginal = (TextView) convertView
                        .findViewById(R.id.phrases_original);

                convertView.setTag(holder);
                convertView.setClickable(true);

            } else {
                holder = (ViewChildHolder) convertView.getTag();
            }

            holder.phonetix.setText(data.get(groupPosition).getPhonetix());
            holder.orginal.setText(data.get(groupPosition).getOriginal());
            convertView.setFocusableInTouchMode(true);
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }

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

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

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

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

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.phrases_item, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView
                        .findViewById(R.id.phrases_text);
                convertView.setTag(holder);
                convertView.setClickable(true);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.name.setText(data.get(groupPosition).getName());
            return convertView;

        }

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

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

PhrasesActivity



    public class PhrasesActivity extends Activity {
        private PhraseAdapter phraseAdapter;
        private PhraseDao dao;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.phrases);
            dao = new PhraseDao();


            final ExpandableListView mainListView = (ExpandableListView) findViewById(R.id.phrases_list);
            phraseAdapter = new PhraseAdapter(this);
            phraseAdapter.setData(dao.getPhrasesByCategoryId(1));
            mainListView.setAdapter(phraseAdapter);

        }
    }

And simple xml layouts

phrases.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:orientation="vertical" >



<ExpandableListView
    android:id="@+id/phrases_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:groupIndicator="@null"
    android:clickable="true" />

</LinearLayout>

phrases_item.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="wrap_content"
android:clickable="true"
android:gravity="center_vertical|left"
android:orientation="vertical"
android:paddingBottom="0dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="0dip" >

<TextView
    android:id="@+id/phrases_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:paddingTop="0dip" >
</TextView>

</LinearLayout>

phrases__extra_item.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="wrap_content"
android:clickable="true"
android:gravity="center_vertical|left"
android:orientation="vertical"
android:paddingBottom="0dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="0dip" >

<TextView
    android:id="@+id/phrases_phonetix"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:paddingTop="0dip" >
</TextView>

<TextView
    android:id="@+id/phrases_original"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="0dip"
    android:paddingLeft="0dip"
    android:paddingRight="0dip"
    android:paddingTop="0dip" >
</TextView>

</LinearLayout>

thanks

like image 466
PulsAm Avatar asked Nov 14 '12 08:11

PulsAm


2 Answers

Hi, i think below code helps you. Which prevent the collapse the list view.

    int count = exadapter.getGroupCount();

    for (int i = 0; i < count; i++)
    {
        expListView.expandGroup(i);
    }

use above code in your expandablelist view .it solve your problem

like image 193
vadher jitendra Avatar answered Oct 24 '22 05:10

vadher jitendra


public int getChildrenCount(int groupPosition) {
 return 1;
 }

This returns 1 as child. That might be a problem. Instead of it return number of child elements

like image 34
MysticMagicϡ Avatar answered Oct 24 '22 04:10

MysticMagicϡ