<?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"
- >
+ android:orientation="vertical" android:layout_width="fill_parent"
+ android:layout_height="fill_parent">
-<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/SearchText" android:hint="Search Text"></EditText>
+ <EditText android:layout_width="fill_parent"
+ android:layout_height="wrap_content" android:id="@+id/SearchText"
+ android:hint="Search Text"></EditText>
+
+ <ListView android:id="@id/android:list"
+ android:layout_width="fill_parent" android:layout_height="fill_parent"
+ android:choiceMode="singleChoice" android:clickable="true"></ListView>
-<ListView android:layout_width="wrap_content" android:layout_height="fill_parent" android:id="@+id/SearchResults" android:choiceMode="singleChoice"></ListView>
</LinearLayout>
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
-import android.app.Activity;
+import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
+import android.view.ContextMenu;
+import android.view.KeyEvent;
+import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
+import android.view.Window;
+import android.view.ContextMenu.ContextMenuInfo;
+import android.view.MenuItem.OnMenuItemClickListener;
+import android.view.View.OnLongClickListener;
+import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.AdapterView.OnItemLongClickListener;
import com.hughes.util.FileUtil;
-public class Dictionary extends Activity {
+public class Dictionary extends ListActivity {
public static final String INDEX_FORMAT = "%s_index_%d";
private File dictionaryFile = new File("/sdcard/dict-de-en.txt");
private RandomAccessFile dictionaryRaf;
private final Index[] indexes = new Index[2];
private final byte lang = Entry.LANG1;
-
+
final Handler uiHandler = new Handler();
private final Object mutex = new Object();
private SearchOperation searchOperation = null;
private List<Entry> entries = Collections.emptyList();
private DictionaryListAdapter dictionaryListAdapter = new DictionaryListAdapter();
+ private int selectedItem = -1;
/** Called when the activity is first created. */
@Override
EditText searchText = (EditText) findViewById(R.id.SearchText);
searchText.addTextChangedListener(new DictionaryTextWatcher());
- ListView searchResults = (ListView) findViewById(R.id.SearchResults);
- searchResults.setAdapter(dictionaryListAdapter);
+ setListAdapter(dictionaryListAdapter);
+
try {
loadIndex();
} catch (Exception e) {
throw new RuntimeException(e);
}
onSearchTextChange("");
+
+ registerForContextMenu(getListView());
+ getListView().setOnItemLongClickListener((new OnItemLongClickListener() {
+ public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
+ long arg3) {
+ selectedItem = arg2;
+ return false;
+ }
+ }));
+ }
+
+ @Override
+ public void onCreateContextMenu(ContextMenu menu, View v,
+ ContextMenuInfo menuInfo) {
+ if (selectedItem == -1) {
+ return;
+ }
+ final MenuItem addToWordlist = menu.add("Add to wordlist.");
+ addToWordlist.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(MenuItem item) {
+ Log
+ .d("THAD", "context menu: "
+ + entries.get(selectedItem).getRawText());
+ return false;
+ }
+ });
+ }
+
+ @Override
+ protected void onListItemClick(ListView l, View v, int position, long id) {
+ Log.d("THAD", "Clicked: " + entries.get(position).getRawText());
+ selectedItem = position;
+ openContextMenu(getListView());
}
private void loadIndex() throws IOException, ClassNotFoundException {
final String searchText;
final int count = 100;
final AtomicBoolean interrupted = new AtomicBoolean(false);
-
+
public SearchOperation(final String searchText) {
- this.searchText = searchText;
+ this.searchText = searchText.toLowerCase(); // TODO: better
}
-
+
public void run() {
Log.d("THAD", "SearchOperation: " + searchText);
final List<Entry> newEntries = new ArrayList<Entry>(count);
}
entries = newEntries;
}
-
+
uiHandler.post(new Runnable() {
public void run() {
synchronized (mutex) {
dictionaryListAdapter.notifyDataSetChanged();
+ if (entries.size() > 0) {
+ setSelection(0);
+ }
}
- }});
+ }
+ });
}
}
return position;
}
- public View getView(int position, View convertView, ViewGroup parent) {
+ public View getView(final int position, final View convertView,
+ final ViewGroup parent) {
TextView result = null;
if (convertView instanceof TextView) {
result = (TextView) convertView;
return lookup(text, 0, root);\r
}\r
\r
- private Node lookup(final String text, final int pos, final Node n) throws IOException {\r
+ private Node lookup(final String text, final int pos, final Node node) throws IOException {\r
if (pos == text.length()) {\r
- return n;\r
+ return node;\r
}\r
\r
// Check whether any prefix of text is a child.\r
for (int i = pos + 1; i <= text.length(); ++i) {\r
- final Integer child = n.children.get(text.substring(pos, i));\r
+ final Integer child = node.children.get(text.substring(pos, i));\r
if (child != null) {\r
return lookup(text, i, getNode(text.substring(0, i), child));\r
}\r
\r
// Check whether any child starts with what's left of text.\r
final String remainder = text.substring(pos);\r
- for (final Map.Entry<String, Integer> childEntry : n.children.entrySet()) {\r
+ for (final Map.Entry<String, Integer> childEntry : node.children.entrySet()) {\r
if (childEntry.getKey().startsWith(remainder)) {\r
- return getNode(n.text + childEntry.getKey(), childEntry.getValue());\r
+ return getNode(node.text + childEntry.getKey(), childEntry.getValue());\r
}\r
}\r
\r
- return n;\r
+ return node;\r
}\r
\r
private Node getNode(final String text, final int indexOffset) throws IOException {\r