Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:57:15+00:00 2026-06-17T14:57:15+00:00

I’m trying to customize an expandableListView by adapting some code I found. This invokes

  • 0

I’m trying to customize an expandableListView by adapting some code I found. This invokes a custom adapter, and inflates the required rows for the list view.

My problem: I need to use that thing with other layout components, so in order I need to call setContentView and THEN inflate the listView where I need it. All my attempts end in an app crash, can someone tell me how to adapt the below code to work inside a Layout..?

MainActivity:

 public class MainActivity extends ExpandableListActivity implements
    OnChildClickListener {
private static final String[] Groups = { "G1", "G2",
        "G3", "G4" };
private static final String[] Services = { "Service1",
        "Service2", "Service3", "Service4", "Service5",
        "Service6" };
private static final String[] Test = { "Test1", "Test2",
    "Test3", "Test4" };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ExpandableListView expandbleLis = getExpandableListView();
    expandbleLis.setDividerHeight(2);
    expandbleLis.setGroupIndicator(null);
    expandbleLis.setClickable(true);

    setGroupData();
    setChildGroupData();

    NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
    mNewAdapter
            .setInflater(
                    (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                    this);
    expandbleLis.setAdapter(mNewAdapter);
    expandbleLis.setOnChildClickListener(this);
}

public void setGroupData() {
    for (int i = 0; i < 4; i++) {
        groupItem.add(Groups[i]);
    }
}

ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();

public void setChildGroupData() {
    /**
     * Add Data For Services
     */
    ArrayList<String> child = new ArrayList<String>();
    for (int i = 0; i < 6; i++) {
        child.add(Services[i]);
    }
    childItem.add(child);

    /**
     * Add Data For Test
     */
    child = new ArrayList<String>();
    for (int i = 0; i < 4; i++) {
        child.add(Test[i]);
    }
    childItem.add(child);
    /**
     * 
     */
    child = new ArrayList<String>();
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    childItem.add(child);
    /**
     * 
     */
    child = new ArrayList<String>();
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    child.add("Dummy Text");
    childItem.add(child);
}

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    Toast.makeText(MainActivity.this, "Clicked On Child",
            Toast.LENGTH_SHORT).show();
    return true;
}
     }

The Adapter class:

public class NewAdapter extends BaseExpandableListAdapter {

public ArrayList<String> groupItem, tempChild;
public ArrayList<Object> Childtem = new ArrayList<Object>();
public LayoutInflater minflater;
public Activity activity;

public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) {
    groupItem = grList;
    this.Childtem = childItem;
}

public void setInflater(LayoutInflater mInflater, Activity act) {
    this.minflater = mInflater;
    activity = act;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return null;
}

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

@SuppressWarnings("unchecked")
@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    tempChild = (ArrayList<String>) Childtem.get(groupPosition);
    TextView text = null;
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.childrow, null);
    }
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(tempChild.get(childPosition));
    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity, tempChild.get(childPosition),
                    Toast.LENGTH_SHORT).show();
        }
    });
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<String>) Childtem.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return null;
}

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

@Override
public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.grouprow, null);
    }
    ((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
    ((CheckedTextView) convertView).setChecked(isExpanded);
    return convertView;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:57:17+00:00Added an answer on June 17, 2026 at 2:57 pm

    You have to call setContentView passing a layout which contains an ExpandableListView whose id is @android:id/list.
    Then, you can get this ExpandableListView calling getExpandableListView().

    Btw, because you can customize your ExpandableListView by XML, you won’t have to call this method and will use setListAdapter() only.
    Your activity won’t have to implement OnChildClickListener and call setOnChildClickListener, you just have to override the method onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) of ExpandableListActivity.

    See this link to have an example of layout.
    And for the next times, don’t forget to post the logs of the exception you get.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.