Wednesday, September 28, 2011

ListView Sectioned Headers in Android

Sectioned headers in a list are great when you want to display categorized items eg. by time/day, by product category or sales price.

In this example, we will be using Jeff Sharkey’s Sectioned Headers to display journal entries by day.
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListSample extends Activity
{

public final static String ITEM_TITLE = "title";
public final static String ITEM_CAPTION = "caption";

// SectionHeaders
private final static String[] days = new String[]{"Mon", "Tue", "Wed", "Thur", "Fri"};

// Section Contents
private final static String[] notes = new String[]{"Ate Breakfast", "Ran a Marathan ...yah really", "Slept all day"};

// MENU - ListView
private ListView addJournalEntryItem;

// Adapter for ListView Contents
private SeparatedListAdapter adapter;

// ListView Contents
private ListView journalListView;

public Map createItem(String title, String caption)
{
Map item = new HashMap();
item.put(ITEM_TITLE, title);
item.put(ITEM_CAPTION, caption);
return item;
}

@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);

// Sets the View Layer
setContentView(R.layout.main);

// Interactive Tools
final ArrayAdapter journalEntryAdapter = new ArrayAdapter(this, R.layout.add_journalentry_menuitem, new String[]{"Add Journal Entry"});

// AddJournalEntryItem
addJournalEntryItem = (ListView) this.findViewById(R.id.add_journalentry_menuitem);
addJournalEntryItem.setAdapter(journalEntryAdapter);
addJournalEntryItem.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView parent, View view, int position, long duration)
{
String item = journalEntryAdapter.getItem(position);
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
}
});

// Create the ListView Adapter
adapter = new SeparatedListAdapter(this);
ArrayAdapter listadapter = new ArrayAdapter(this, R.layout.list_item, notes);

// Add Sections
for (int i = 0; i < days.length; i++) { adapter.addSection(days[i], listadapter); } // Get a reference to the ListView holder journalListView = (ListView) this.findViewById(R.id.list_journal); // Set the adapter on the ListView holder journalListView.setAdapter(adapter); // Listen for Click events journalListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long duration)
{
String item = (String) adapter.getItem(position);
Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
}
});
}

}

Download
http://www.filefactory.com/file/b3g5g1b/n/TestSectionedHeaderList.zip 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.