androidandroid-listview

Creating a sectioned listview


I am currently in the process of creating a sectioned listview. So far I've made a single listview with a header and a list of items. Eventually this should turn into a template that I can use in my project. What I need to know is how to Add a header and a corresponding list of items after this header in the following code:

public class MainActivity extends Activity {

private ListView listView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Preferences preference1_data[] = new Preferences[]
            {
                new Preferences(R.drawable.bird, "Bird Preference"),
                new Preferences(R.drawable.gear, "Gear Preference"),
                new Preferences(R.drawable.planet, "Planet Preference"),
            };

    Preferences preference2_data[] = new Preferences[]
            {
                new Preferences(R.drawable.bird, "Bird Preference"),
                new Preferences(R.drawable.gear, "Gear Preference"),
                new Preferences(R.drawable.planet, "Planet Preference"),
            };

    PreferencesAdapter adapter = new PreferencesAdapter(this, 
            R.layout.listview_item_row, preference1_data);


    listView1 = (ListView)findViewById(R.id.listView1);

    // Add Preference 1 section
    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);
    listView1.setAdapter(adapter);

    // Add Preference 2 section

    // What code goes here?
}

}


Solution

  • Check this library out, handles everything for you.