Reputation: 956
I have created a custom expandable list view and a custom adapter. The list view consists in 3 XML layouts (please ignore the dumb use of table layout!):
One that defines the ExpandableListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ExpandableListView
android:id="@+id/expandable_list"
android:groupIndicator="@null"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000" >
<!-- android:listSelector="@android:color/transparent" -->
</ExpandableListView>
</LinearLayout>
Another that defines the parent (group):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TableRow android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="100dip"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TableRow android:layout_gravity="center_vertical">
<TextView
android:id="@+id/list_item_text_view"
android:textColor="@color/tab_sel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:gravity="center_vertical"
android:fillViewport="true"
android:textSize="17sp"
android:layout_marginLeft="10dip"
android:textStyle="bold"/>
</TableRow>
</TableLayout>
<ImageView
android:layout_width="25dip"
android:id="@+id/rightcheck"
android:layout_height="25dip"
android:layout_gravity="left|center_vertical"
android:scaleType="centerCrop"
android:background="@drawable/sectionuncheck"
android:layout_marginRight="8sp"
android:layout_marginTop="0dip"/>
</TableRow>
</TableLayout>
And the last one defines the children:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="top">
<!-- android:background="@drawable/bar_bg" -->
<TableRow>
<ImageView
android:id="@+id/image"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginLeft="20dip"
android:src="@drawable/arrow"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="65dp"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="top"
android:gravity="center_vertical" >
<TableRow>
<TextView
android:id="@+id/list_item_text_child"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="15sp"
android:layout_marginLeft="20dip"
android:layout_marginRight="35dip"
android:text=""
android:textColor="#003479"/>
</TableRow>
<TableRow >
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="15sp"
android:layout_marginLeft="20dip"
android:layout_marginRight="35dip"
android:text=""
android:textColor="#44B5E7"/>
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/ivProdCheck"
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_gravity="left|center_vertical"
android:scaleType="centerCrop"
android:layout_marginRight="8dp"
android:background="@drawable/check"
android:visibility="gone"/>
</TableRow>
</TableLayout>
Every time I click on a child, it gets selected or unselected, meaning that the ivProdCheck gets visible or not
if(is_preffered == 0)
{
Glob.getPreferred().add(prod_curent);
image.setVisibility(View.VISIBLE);
}
else
{
image.setVisibility(View.GONE);
}
Same thing happens in the adapter, to display the checked/uncheck items on activity start:
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup)
{
/*........*/
if(Glob.getPreferred() != null)
{
for (Product to_compare : Glob.getPreferred())
{
if(to_compare.id_produs == id_produs_curent)
{
image.setVisibility(View.VISIBLE);
break;
}
}
}
view.setTag(holder);
//return the entire view
return view;
}
When I click on the group/parent, I select/deselect all children and change the image according to that
if(counter != prod_in_cat)
{
for(Product p : cat_curent.getProd())
{
Glob.getPreferred().add(p);
}
imageParent.setImageResource(R.drawable.sectioncheck);
}
else
{
imageParent.setImageResource(R.drawable.sectionuncheck);
}
Every time a group is selected (meaning all children are selected), if I then deselect one child, I also want to change the parent image. Also, if I manually select/deselect all children in one group, I want to change the group image too. Same thing goes for onCreate()
.
I tried this on child click (the logs show what it's supposed to, so no error on that side):
if(counter != cat_curent.getProduse().size())
{
// Log.e("!= -> ","counter " + counter);
// Log.e("!= -> ","cat_curent" + cat_curent.getProduse().size());
imageParent.setImageResource(R.drawable.sectioncheck);
}
else
{
// Log.e("!= -> ","counter " + counter);
// Log.e("!= -> ","cat_curent" + cat_curent.getProduse().size());
imageParent.setImageResource(R.drawable.sectionuncheck);
}
and this on onCreate()
if((prod_pref > 0) && (prod_pref < prod_cat_glob))
{
Log.e("Log 4", "Categoria " + i + " Produse " + prod_pref);
imageParent.setImageResource(R.drawable.sectionuncheck); //NullPointerException here
}
else if(prod_pref == prod_cat_glob)
{
imageParent.setImageResource(R.drawable.sectioncheck); //NullPointerException here
}
and every time I get NullPointerException
when I use setImageResource()
What's the catch here?
Upvotes: 0
Views: 604
Reputation: 956
Nevermind, I got it working. The data source for my list view was always updated when I clicked a parent or child. The problem is the list wasn't refreshing. I had something similar to fix a few days ago and trying .notifyDataSetChanged() didn't work so I didn't use it for this. It turns out however, that this works. Thanks anyway @m0skit0. Still not sure however, why the NPE. I'll try to figure it out.
Upvotes: 0