TextView
showing the selected entry:
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:orientation='vertical'
android:layout_width='fill_parent'
android:layout_height='fill_parent' >
<TextView
android:id='@+id/selection'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
/>
<ListView
android:id='@android:id/list'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
android:drawSelectorOnTop='false'
/>
</LinearLayout>
In terms of Java code, most of the guts of the activities are poured into an abstract LoremBase
class:
abstract public class LoremBase extends ListActivity {
abstract ListAdapter makeMeAnAdapter(Intent intent);
private static final int LOCAL_SEARCH_ID = Menu.FIRST+1;
private static final int GLOBAL_SEARCH_ID = Menu.FIRST+2;
private static final int CLOSE_ID = Menu.FIRST+3;
TextView selection;
ArrayList<String> items = new ArrayList<String>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView)findViewById(R.id.selection);
try {
XmlPullParser xpp = getResources().getXml (R.xml.words);
while (xpp.getEventType()! =XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType()==XmlPullParser.START_TAG) {
if (xpp.getName().equals('word')) {
items.add(xpp.getAttributeValue(0));
}
}
xpp.next();
}
} catch (Throwable t) {
Toast
.makeText(this, 'Request failed: ' + t.toString(), 4000).show();
}
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
ListAdapter adapter = makeMeAnAdapter(intent);
if (adapter==null) {
finish();
} else {
setListAdapter(adapter);
}
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(items.get (position).toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, LOCAL_SEARCH_ID, Menu.NONE, 'Local Search')
.setIcon(android.R.drawable.ic_search_category_default);
menu.add(Menu.NONE, GLOBAL_SEARCH_ID, Menu.NONE, 'Global Search')
.setIcon(R.drawable.search).setAlphabeticShortcut (SearchManager.MENU_KEY);
menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, 'Close')
.setIcon(R.drawable.eject).setAlphabeticShortcut ('c');
return(super.onCreateOptionsMenu (menu));
}
@Override