Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expandable listview without children

I am new to android development and also using expandable listview for the first time. I am creating an app where in I get all the contents of d expandable listview from webservice. I get all contents from the wevservice as well but then when there is no child of a parent in expandable listview the custom adapter merhod of expandable listview getchildcount () returns null and gives a null pointer exception how can I show only parent in the list with no child and both when both are available??

Thank you

public View getChildView(int p_id, int c_id, boolean bln1, View view,ViewGroup viewgroup) {
    // TODO Auto-generated method stub

     if (view == null) {
            view = inflater.inflate(com.example.eventlive.R.layout.homescreen_list_item_child, viewgroup,false);
        }

        TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
        //"i" is the position of the parent/group in the list and 
        //"i1" is the position of the child
        textView.setText(mParent.get(p_id).getArrayChildren().get(c_id));
        view.setBackgroundResource(R.drawable.child_bg);
        //return the entire view
        return view;
}

//counts the number of children items so the list knows how many times calls getChildView() method
@Override
public int getChildrenCount(int p_id) {
    // TODO Auto-generated method stub
    return mParent.get(p_id).getArrayChildren().size();
}
like image 466
android_developer Avatar asked Apr 22 '13 17:04

android_developer


People also ask

What is an Expandable list view?

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.

How do you click on a group if a child is empty in expandable ListView android?

call its method ExpandableListView. OnChildClickListener inside that get count of child by passing groupPosition that will return count of childs related to the groupPosition you pass.


1 Answers

Just initialize an empty children array like this:

private HashMap<String, ArrayList<String>> mGroupsItems =
        new HashMap<String, ArrayList<String>>();
. . .
mGroupsItems.put(name_of_empty_group, new ArrayList<String>());
like image 177
Alexander Zhak Avatar answered Oct 06 '22 06:10

Alexander Zhak