CodePrimate
CodePrimate

Reputation: 6666

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?
}

}

Upvotes: 1

Views: 1850

Answers (3)

Sri
Sri

Reputation: 53

I faced a similar problem and found an excelent tutorial for that.

Here it is: http://blogingtutorials.blogspot.co.uk/2010/11/android-listview-header-two-or-more-in.html

Hope it helps :)

Upvotes: 1

Android-iPhone-rahul
Android-iPhone-rahul

Reputation: 447

this code will help you

LayoutInflater inflater = this.getLayoutInflater();
        LinearLayout listFooterView = (LinearLayout)inflater.inflate(
                com.demo.list.R.layout.footerview, null);

        LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
                com.demo.list.R.layout.headerview, null);
        ListView lv = getListView();
        lv.setTextFilterEnabled(false);
        lv.addFooterView(listFooterView);
        lv.addHeaderView(listHeaderView);

here headerview and footerview is different layout in which i am showing buttons with listview.

Upvotes: 0

Joakim Berglund
Joakim Berglund

Reputation: 2871

Check this library out, handles everything for you.

Upvotes: 3

Related Questions