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
{
Map
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
// 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
// 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();
}
});
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.