Android Open Source - book Translations Table






From Project

Back to project page book.

License

The source code is released under:

MIT License

If you think the Android project book listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com;
/*w  w w. j  av a  2 s  . com*/
import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import book.Book;
/*
 * 
 * 
 *   table for translations
 * helper class for reading the table
 */
public class TranslationsTable{

  private SQLiteDatabase db;
  
  private static final String TABLE_NAME = "translation";
  
  public TranslationsTable(SQLiteDatabase db) {
    this.db = db;
    db.execSQL("create table IF NOT EXISTS "+TABLE_NAME+" (id integer primary key autoincrement,word char(255), translation char(255))");
  }

  public void clear(){
    db.execSQL("delete from "+TABLE_NAME +" if exists");
  }
  
  public Book get(Book book){
    book.setWords( this.get( book.getWordsAsList() ) );
    return book;
  }
  
  // get a list of words with translations
  public HashMap<String, List<String>> get(List<String> words){
    HashMap<String, List<String>> result = new HashMap<String, List<String>>(); 

    for (String word : words){      
      result.put(word, this.getTranslation(word));
    }
    
    return result;
  }
  
  // get translation for word
  public List<String> getTranslation(String word){    
    List<String> translations = new ArrayList<String>();
    
    Cursor cursor = db.query(TABLE_NAME, new String[] {"translation"}, "word=\""+word+"\"", null, null,null, null);    
    cursor.moveToFirst();
    while (!cursor.isAfterLast()){      
      translations.add(cursor.getString(0));    
      cursor.moveToNext();
    }
    return translations;        
  }

  // if the word is already saved into databse
  public boolean hasWord(String word){
    Cursor cursor = db.rawQuery("select word from "+TABLE_NAME+" where word=\""+word+"\"", null);
    
    if (cursor.getCount() > 0){
      System.out.println("juu on");
      return true;
    } else {
      return false;  
    }
  }
  

  public boolean hasTranslation(String word, String translation){
    Cursor cursor = db.rawQuery("select * from "+TABLE_NAME+" where word=\""+word+"\" and translation=\""+translation+"\" ", null);    
    
    if (cursor.getCount() > 0){
      return true;
    } else {
      return false;  
    }    
  }

  public void save(Book book){
    HashMap<String, List<String>> words = book.getWords();
    
    for (String word : words.keySet()){
      this.save(word, words.get(word));
    }
  }
  
  public void save(String word, List<String> translations){  
    System.out.println("SAVE ..............."+word);
    
    if (translations.size() > 0){        
      for (String translation : translations){        
        System.out.println("SAVE ...............TRANSLATION: "+translation);
        if (!this.hasTranslation(word, translation)){
                
          ContentValues val = new ContentValues();
          val.put("word",word);
          val.put("translation",translation);
          db.insert(TABLE_NAME,null, val);
        }          
      }
    }
  }
    
}




Java Source Code List

book.BookListAdapter.java
book.Book.java
book.ExpandableListAdapter.java
book.SelectableWordListAdapter.java
com.BookTable.java
com.Db.java
com.TranslationsTable.java
com.WordTable.java
fragments.BookListFragment.java
fragments.BookViewFragment.java
fragments.CameraFragment.java
fragments.EditBookFragment.java
fragments.PageAdapter.java
fragments.PagerActivity.java
translator.TranslateWordTask.java
translator.Translator.java