Pergunta

I have an Expandable ListView but I'm facing two problems. Q1. If the header doesn't contain any children I would like to hide/remove it from the list. For this I have inside getGroupView method.

if ( getChildrenCount( groupPosition ) == 0 ) 
{
   convertView.setVisibility(View.GONE);
}

Let's say that I have 10 headers and the header 3,5, and 8 doesn't have any children. When I use the above code it hides the 3,5,8th headers but the problem is it leaves blank space there and it won't look like a list. So any idea how to make it look like a list ?

Q2. I want to display a toast message saying "Data is not available" when the header doesn't have any children. For this I have used below code in getGroupView method,

     convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if ( getChildrenCount( groupPosition ) == 0 )
                        Toast.makeText(_context, "No data available", Toast.LENGTH_SHORT).show();

                }
            });

This shows the toast message but the problem here is - the headers which have children doesn't get expanded when I click on them. How to solve this problem?

Here is the code of populating the dataset

listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

    listDataHeader.add(dayName_first);
    listDataHeader.add(dayName_second);
    listDataHeader.add(dayName_third);
    listDataHeader.add(dayName_fourth);
    listDataHeader.add(dayName_fifth);
    listDataHeader.add(dayName_sixth);
    listDataHeader.add(dayName_seventh);

    firstDay = checkEmpty(firstDay);  // Function to check whether the list is empty or not. Do I need to check each list here and add or somewhere else?
    listDataChild.put(listDataHeader.get(0), firstDay);
    listDataChild.put(listDataHeader.get(1), secondDay);
    listDataChild.put(listDataHeader.get(2), thirdDay);
    listDataChild.put(listDataHeader.get(3), fourthDay);
    listDataChild.put(listDataHeader.get(4), fifthDay);
    listDataChild.put(listDataHeader.get(5), sixthDay);
    listDataChild.put(listDataHeader.get(6), seventhDay);

    listAdapter = new StaffExpandableListAdapter(this, listDataHeader,
            listDataChild);
    llExpandable.invalidateViews();

    llExpandable.setAdapter(listAdapter);
Foi útil?

Solução

Let me answer the second question first, because that is more straightforward.

The problem is that when you set you custom click listener, it overrides the default one of the list.

So, what you need to do is first check for the child count. If the count is zero, then only add the listener.

if ( getChildrenCount( groupPosition ) == 0 )
 convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                   Toast.makeText(_context, "No data available", Toast.LENGTH_SHORT).show();

                }
            });

Now, coming back to the first question. I am assuming that you have a dataset in which you store all the elements. Before you create the adapter for the expandable listview, loop through the dataset and remove all the elements which don't have any children. After this, create the adapter from the dataset.

Outras dicas

Try this code

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

   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 Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context 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 childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@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;
}

}

Use the following link http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ If it is useful accept the ans... give me credits

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top