I am creating ExpandableListViewwith data from a database. For this, I use CursorTreeAdapter, and I populate it with an object Cursorthat contains the data that I retrieve from the database.
I thought that by default, Android would consider groups without a child "non-extensible".
However, it still shows the extension icon in the line, and when I click on it, it does nothing. I do not want him to show this icon.
I want only groups that have children to display an extension icon, which is not happening. It shows an extension icon for all rows (those that have a child and do not have a child).
UPDATE
I studied my code and I saw that the problem was setting up groupIndicatorfor groups. However, I tried many approaches, such as creating a selector and setting it based on state and extensibility, for example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true"
android:state_expanded="false"
android:drawable="@android:color/transparent"/>
<item android:drawable="@drawable/expander_ic_maximized" />
</selector>
But when the group broke up, it hides for all groups, including those that have children (because Android recognized collapsed groups as empty).
What is the best approach to set the indicator only for groups with children?
Here is my code.
public class ContactsExpandableListAdapter extends CursorTreeAdapter
{
TextView mContactNameTextView, mContactNumberTextView;
Cursor mChildrenCursor = null;
public ContactsExpandableListAdapter(Cursor cursor, Context context)
{
super(cursor, context);
}
@Override
protected Cursor getChildrenCursor(Cursor cursor)
{
if(mContactId == -1)
mContactId = null;
return mController.getContactById(mContactId);
}
public int getChildrenCount(int groupPosition)
{
return mChildrenCursor.getCount();
}
@Override
protected View newGroupView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup)
{
View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false);
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null) mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
else
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
view.setTag(cursor.getString(cursor.getColumnIndex("contact_id")));
return view;
}
@Override
protected View newChildView(Context context, Cursor cursor, boolean paramBoolean, ViewGroup viewGroup)
{
View view = LayoutInflater.from(context).inflate(R.layout.filterbycontact, viewGroup, false);
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
{
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
else
{
mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number);
mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
view.setTag(cursor.getString(cursor.getColumnIndex("contact_number")));
return view;
}
@Override
protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean)
{
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
else
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_name")));
view.setTag(cursor.getString(cursor.getColumnIndex("contact_id")));
}
@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean paramBoolean)
{
if(cursor.getString(cursor.getColumnIndex("contact_name")) == null)
{
mContactNameTextView = (TextView) view.findViewById(R.id.contact_name);
mContactNameTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
else
{
mContactNumberTextView = (TextView) view.findViewById(R.id.contact_number);
mContactNumberTextView.setText(cursor.getString(cursor.getColumnIndex("contact_number")));
}
}
}