5

Hi I am having an issue trying to understand how sectioned ListViews work. I had it working into a normal list view. but now I want to add sections to my list. How to I ad a section header in.

Heres my code that works.

public class ChooseTeamActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    setContentView(R.layout.chooseact);    

    String FullData = getIntent().getStringExtra("FullData");

    try{

      JSONObject obj = new JSONObject(FullData);
      List<String> leagues = new ArrayList<String>();

      JSONObject objData = obj.getJSONObject("data");

      JSONArray jArray = objData.getJSONArray("structure");


      for (int i=0; i < jArray.length(); i++) {
        JSONObject oneObject = jArray.getJSONObject(i);   
        leagues.add(oneObject.getString("league_website_name"));
        JSONArray DivisionsArray = oneObject.getJSONArray("divisions");

        for (int d=0; d < DivisionsArray.length(); d++){            
           JSONObject DivDict = DivisionsArray.getJSONObject(d);   
           leagues.add(DivDict.getString("name"));              
        }               
     }         

      setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, 
                                                                    leagues));

      ListView list = getListView();
      list.setTextFilterEnabled(true);

    } catch (JSONException e) {
        e.printStackTrace();
    }     
    }
}
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
iamlukeyb
  • 6,487
  • 12
  • 29
  • 40
  • possible duplicate of [Creating categories in a ListView?](http://stackoverflow.com/questions/1013765/creating-categories-in-a-listview) – slayton Apr 25 '12 at 15:51

2 Answers2

10

A quick google of "android sectioned listview" will return results for example http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/

In fast summary though you end up writing a list adapter that returns a header layout when needed, and a row layout when needed.

huntsfromshadow
  • 1,085
  • 8
  • 12
5

The correct answer is that section are not supported at all. You have to fake them.

dwery
  • 1,128
  • 10
  • 20
  • 2
    This is the correct answer. _sections_ are not supported. the author is trying to do something that is not supported, thus there's no answer that could be provided to solve its issue. if you want them, you build them from scratch. Faking it, as suggested by the other answer, does not make them "sections", they're just normal row items. – dwery Jul 15 '15 at 09:15