Skip to content

Commit

Permalink
Make entries and adapter fields
Browse files Browse the repository at this point in the history
This prevents the needless recreation of the adapter
every time it is cleared and set.
  • Loading branch information
ispedals committed Sep 18, 2013
1 parent 12d5035 commit b5d61e4
Showing 1 changed file with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DictionaryEntry> adapter;

public static boolean RUNNING = false;
private static int windowState = OPENED;

Expand Down Expand Up @@ -131,6 +134,38 @@ public void onClick(final View searchView) {
}
});

adapter = new ArrayAdapter<DictionaryEntry>(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);
}

Expand Down Expand Up @@ -238,49 +273,20 @@ 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<Object>(window.getContext(),
R.layout.dictionaryentry));
entries.clear();
adapter.notifyDataSetChanged();

}

private void displayDefinition(final Window window,
ArrayList<Parcelable> arrayList) {

Log.d(TAG, "displaying Definition");
DictionaryEntries entries = DictionaryEntries.fromParcelable(arrayList);
ArrayAdapter<DictionaryEntry> adapter = new ArrayAdapter<DictionaryEntry>(
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) {
Expand Down

0 comments on commit b5d61e4

Please sign in to comment.