Home » Android » android – ExpandableListView – Is conditional expansion possible?

android – ExpandableListView – Is conditional expansion possible?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&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.
  }
});

Answer:

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);