Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpandableListView - Is conditional expansion possible?

I have an ExpandableListView bound to a SQLite database. To simplify things, lets assume the database contains two columns: title and body. In the ExpandableListView, there is a group for each title, and a childfor each corresponding body.

Now to make things more interesting, some of the rows in the SQLite database do not have a body (that is... they only have a title). As you can see, if there is no body, then there is no reason to expand the group... because the child will be empty (i.e. String body == "").

I'm searching for a way to catch a situation like this, and skip the group's expansion. I don't want a blank child to be expanded. To put it in psuedo code, I want something like this:

if (body.getText() == "") {
  //DO NOT EXPAND
  //DO OTHER STUFF
}

Any suggestions?

like image 694
dfetter88 Avatar asked Aug 12 '10 03:08

dfetter88


1 Answers

The trick is in the OnGroupClickListener. The following code shoud work. Put at the end of your onCreate method for the ExpandableListActivity.

lv.setOnGroupClickListener(new OnGroupClickListener() {

  @Override
  public boolean onGroupClick(ExpandableListView parent, View v,
      int groupPosition, long id) {

    Cursor c = mDbHelper.fetchRow(id); //Get Title & body from SQLite table
    if (c.getString(Constants.bodyColumn).equals("")) {
      //If the body returned from SQLite is empty, return true
      return true; //True means the click HAS been handled.
    }
    return false; //Click has not been handled, so let Android expand here.
  }
});
like image 169
dfetter88 Avatar answered Oct 29 '22 16:10

dfetter88