From b5d61e4ab5c2f964e447da05d1491d3011522a26 Mon Sep 17 00:00:00 2001 From: ispedals Date: Sun, 15 Sep 2013 09:15:00 -0400 Subject: [PATCH] Make entries and adapter fields This prevents the needless recreation of the adapter every time it is cleared and set. --- .../FloatingJapaneseDictionaryService.java | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java b/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java index 3ba816e..c3e4a79 100644 --- a/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java +++ b/src/pedals/is/floatingjapanesedictionary/FloatingJapaneseDictionaryService.java @@ -73,6 +73,9 @@ public class FloatingJapaneseDictionaryService extends StandOutWindow { private static StandOutLayoutParams openedParams; private static StandOutLayoutParams expandedParams; + private static final DictionaryEntries entries = new DictionaryEntries(); + private static ArrayAdapter adapter; + public static boolean RUNNING = false; private static int windowState = OPENED; @@ -131,6 +134,38 @@ public void onClick(final View searchView) { } }); + adapter = new ArrayAdapter(this, + R.layout.dictionaryentry, entries); + ListView listView = (ListView) view.findViewById(R.id.results); + listView.setAdapter(adapter); + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + + @Override + public void onItemClick(AdapterView parent, View view, + int position, long id) { + + DictionaryEntry entry = (DictionaryEntry) parent + .getItemAtPosition(position); + Log.d(TAG, "item clicked " + entry.toString()); + try { + FileWriter filewriter = new FileWriter(saveLocation, true); + filewriter.append("\r\n" + entry.toString()); + filewriter.close(); + Log.d(TAG, + "item saved to " + saveLocation.getAbsolutePath()); + Toast.makeText(FloatingJapaneseDictionaryService.this, + "Saved", Toast.LENGTH_SHORT).show(); + } + catch (IOException e) { + Log.d(TAG, "Save failed"); + e.printStackTrace(); + Toast.makeText(FloatingJapaneseDictionaryService.this, + "Could not save", Toast.LENGTH_SHORT).show(); + } + + } + }); + Log.d(TAG, "Creating window; id: " + id); } @@ -238,10 +273,9 @@ private void clearText(Window window) { Log.d(TAG, "clearing text"); TextView status = (TextView) window.findViewById(R.id.status); - ListView listView = (ListView) window.findViewById(R.id.results); status.setText(""); - listView.setAdapter(new ArrayAdapter(window.getContext(), - R.layout.dictionaryentry)); + entries.clear(); + adapter.notifyDataSetChanged(); } @@ -249,38 +283,10 @@ private void displayDefinition(final Window window, ArrayList arrayList) { Log.d(TAG, "displaying Definition"); - DictionaryEntries entries = DictionaryEntries.fromParcelable(arrayList); - ArrayAdapter adapter = new ArrayAdapter( - window.getContext(), R.layout.dictionaryentry, entries); - ListView listView = (ListView) window.findViewById(R.id.results); - listView.setAdapter(adapter); - listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - - @Override - public void onItemClick(AdapterView parent, View view, - int position, long id) { - - DictionaryEntry entry = (DictionaryEntry) parent - .getItemAtPosition(position); - Log.d(TAG, "item clicked " + entry.toString()); - try { - FileWriter filewriter = new FileWriter(saveLocation, true); - filewriter.append("\r\n" + entry.toString()); - filewriter.close(); - Log.d(TAG, - "item saved to " + saveLocation.getAbsolutePath()); - Toast.makeText(window.getContext(), "Saved", - Toast.LENGTH_SHORT).show(); - } - catch (IOException e) { - Log.d(TAG, "Save failed"); - e.printStackTrace(); - Toast.makeText(window.getContext(), "Could not save", - Toast.LENGTH_SHORT).show(); - } + entries.clear(); + entries.addAll(DictionaryEntries.fromParcelable(arrayList)); + adapter.notifyDataSetChanged(); - } - }); } private void displayText(Window window, String text) {