Back to project page Langleo.
The source code is released under:
Apache License
If you think the Android project Langleo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.atteo.langleo_trial.activities; /*w ww. j av a 2 s . co m*/ import java.util.ArrayList; import java.util.HashMap; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import com.atteo.langleo_trial.R; import com.atteo.langleo_trial.models.List; import com.atteo.langleo_trial.models.Word; public class Words extends ListActivity { private SimpleCursorAdapter adapter; private List list; private Cursor cursor; private final int REQUEST_NEW_WORD = 1; private final int REQUEST_EDIT_WORD = 2; ArrayList<HashMap<String, String>> datalist; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.words_list); list = new List(); list.loadBundle(getIntent().getBundleExtra("list")); ListView list = getListView(); list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { editWord((int) id); } }); registerForContextMenu(list); } @Override public void onResume() { super.onResume(); refreshList(); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.words, menu); return true; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.word, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); switch (item.getItemId()) { case R.id.delete_word: deleteWord((int) info.id); return true; case R.id.edit_word: editWord((int) info.id); return true; default: return super.onContextItemSelected(item); } } @Override public boolean onOptionsItemSelected(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.new_word: Intent intent = new Intent(getApplicationContext(), EditWord.class); Word word = new Word(); word.setList(list); intent.putExtra("word", word.toBundle()); startActivityForResult(intent, REQUEST_NEW_WORD); break; } return true; } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (resultCode == RESULT_CANCELED) return; Bundle b; switch (requestCode) { case REQUEST_NEW_WORD: case REQUEST_EDIT_WORD: b = intent.getBundleExtra("word"); Word word = new Word(); word.loadBundle(b); word.save(); refreshList(); break; } } public void editWord(int id) { Intent intent = new Intent(getApplicationContext(), EditWord.class); intent.putExtra("word", new Word(id).toBundle()); startActivityForResult(intent, REQUEST_EDIT_WORD); } public void deleteWord(int id) { new Word(id).delete(); refreshList(); } public void refreshList() { ListView list = getListView(); datalist = new ArrayList<HashMap<String, String>>(); this.list.load(); this.cursor = this.list.getWords().orderByInPlace("word").getCursor(); adapter = new SimpleCursorAdapter(this, R.layout.word_item, this.cursor, new String[] { "word", "translation" }, new int[] { R.id.word_word, R.id.word_translation }); list.setAdapter(adapter); } }