EnterURLpublic class EnterURL extends android.app.ListActivity
Fields Summary |
---|
private static final int | DATABASE_VERSION | View.OnClickListener | mViewItemAction | MenuItem.OnMenuItemClickListener | mClearBookmarks | private ArrayList | mHistory | private UrlEditText | mUrlField | private DisplayEditText | mDisplayField | private android.database.Cursor | mCursor |
Methods Summary |
---|
private void | fillListView()
loadPrefs();
ArrayList<HashMap<String, String>> d = new ArrayList<HashMap<String, String>>();
int count = mHistory.size();
for (int i=0; i<count; i++) {
HashMap<String, String> m = new HashMap<String, String>();
HistoryEntry he = mHistory.get(i);
m.put("title", he.url + " (" + he.display + ")");
d.add(m);
}
setListAdapter(new SimpleAdapter(this, d, R.layout.url_list,
new String[] {"title"},
new int[] {android.R.id.text1}));
| private void | loadPrefs()
SQLiteDatabase db = openDB();
Cursor c = db.query("History",
new String[] { "url", "display", "lastAccessTime" },
null, null, null, null, "lastAccessTime DESC");
int urlCol = c.getColumnIndex("url");
int accessCol = c.getColumnIndex("lastAccessTime");
int displayCol = c.getColumnIndex("display");
mHistory.clear();
while (c.moveToNext()) {
HistoryEntry he = new HistoryEntry();
he.url = c.getString(urlCol);
he.display = c.getString(displayCol);
he.lastAccessTime = c.getString(accessCol);
mHistory.add(he);
}
c = db.query("FieldState", null, null, null, null, null, null);
if (c.moveToNext()) {
urlCol = c.getColumnIndex("url");
displayCol = c.getColumnIndex("display");
mUrlField.setText(c.getString(urlCol));
mDisplayField.setText(c.getString(displayCol));
} else {
mDisplayField.setText("_id");
mUrlField.setText("content://");
}
db.close();
| public void | onCreate(android.os.Bundle icicle)
super.onCreate(icicle);
setContentView(R.layout.enter_url);
// display
mDisplayField = (DisplayEditText)findViewById(R.id.display_edit_text);
mDisplayField.setOnClickListener(mViewItemAction);
// url
mUrlField = (UrlEditText)findViewById(R.id.url_edit_text);
mUrlField.setOnClickListener(mViewItemAction);
| public boolean | onCreateOptionsMenu(android.view.Menu menu)
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "Clear Bookmarks").setOnMenuItemClickListener(mClearBookmarks);
return true;
| public android.view.View | onCreateView(java.lang.String name, android.content.Context context, android.util.AttributeSet attrs)
if (name.equals("com.android.development.UrlEditText")) {
return new UrlEditText(this, attrs);
}
if (name.equals("com.android.development.DisplayEditText")) {
return new DisplayEditText(this, attrs);
}
return null;
| protected void | onListItemClick(android.widget.ListView l, android.view.View v, int position, long id)
HistoryEntry he = mHistory.get(position);
viewItem(he.url, he.display);
| public void | onResume()
super.onResume();
// show the history
loadPrefs();
fillListView();
if (mHistory.size() > 0) {
ListView lv = this.getListView();
lv.setSelection(0);
lv.requestFocus();
}
| public void | onStop()
super.onStop();
if (mCursor != null) {
mCursor.deactivate();
}
| android.database.sqlite.SQLiteDatabase | openDB()
SQLiteDatabase db = null;
db = openOrCreateDatabase("inspector.db", 0, null);
int version = db.getVersion();
if (version != DATABASE_VERSION) {
db.execSQL("CREATE TABLE History ("
+ " url TEXT,"
+ " display TEXT,"
+ " lastAccessTime TEXT"
+ ");");
db.execSQL("CREATE TABLE FieldState ("
+ " url TEXT,"
+ " display TEXT"
+ ");");
db.setVersion(DATABASE_VERSION);
}
return db;
| private void | savePrefs()
ContentValues m;
HistoryEntry he;
SQLiteDatabase db = openDB();
db.execSQL("DELETE FROM History;");
int count = mHistory.size();
for (int i=0; i<count; i++) {
m = new ContentValues();
he = mHistory.get(i);
m.put("url", he.url);
m.put("display", he.display);
m.put("lastAccessTime", he.lastAccessTime);
db.insert("History", null, m);
}
db.execSQL("DELETE FROM FieldState");
m = new ContentValues();
m.put("url", mUrlField.getText().toString());
m.put("display", mDisplayField.getText().toString());
db.insert("FieldState", null, m);
db.close();
| private final void | viewItem(java.lang.String url, java.lang.String display)
// -------------- save this in the history ----------------
// look in the history
int count = mHistory.size();
int i;
for (i=0; i<count; i++) {
HistoryEntry he = mHistory.get(i);
if (he.url.equals(url) && he.display.equals(display)) {
he.updateAccessTime();
mHistory.remove(i);
mHistory.add(0, he);
break;
}
}
if (i >= count) {
// didn't find it, add it first
HistoryEntry he = new HistoryEntry();
he.url = url;
he.display = display;
he.updateAccessTime();
mHistory.add(0, he);
}
savePrefs();
// -------------- view it ---------------------------------
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setClass(this, DataList.class);
intent.putExtra("display", display);
startActivity(intent);
|
|