ExpandableListView - Is conditional expansion possible?
Asked Answered
O

2

8

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?

Oscillator answered 12/8, 2010 at 3:42 Comment(0)
O
16

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.
  }
});
Oscillator answered 12/8, 2010 at 13:8 Comment(0)
L
0

This can be done in a very simple and easy manner

expListView.setEnabled(false);

whenever you want to disable it, and then when your task, for which you are waiting for is complete then you can call

expListView.setEnabled(true);
Lavern answered 8/4, 2016 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.