Android Open Source - book Edit Book Fragment






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 fragments;
/* w w  w  . ja  v a 2 s  .co  m*/
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewDebug.CapturedViewProperty;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import book.Book;
import book.SelectableWordListAdapter;

import com.example.book.R;

import edu.sfsu.cs.orange.ocr.OcrResult;

public class EditBookFragment extends Fragment implements OnClickListener{
    
  private Book book;  
  private PagerActivity pager;  
  private View view;
    
  private SelectableWordListAdapter wordAdapter;
  private ListView list;
  private EditText textfield;
  
  private OcrResult ocrResult;
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        
    
    if (container == null) {
            return null;
        }
    
        this.pager = (PagerActivity) getActivity();
        this.view = (View)inflater.inflate(R.layout.editbook, container, false);
        
        this.textfield = (EditText) this.view.findViewById(R.id.bookName);        
        this.list = (ListView)this.view.findViewById(R.id.wordList);
        
        if (pager.editBook != null){
          this.setBook(pager.editBook);
          
        }
        
    // shitbutton for shit
        LinearLayout toolbar = (LinearLayout) this.view.findViewById(R.id.toolbar);        
        
        for (int i = 0; i < toolbar.getChildCount(); i++){
          toolbar.getChildAt(i).setOnClickListener(this);
        }     
    /*
        Bundle args = this.getArguments();
        if (args != null){
          Book bk = this.pager.createBook(); 
          bk.setName(args.getString("name"));
          this.setBook(bk);
        }
        */
    
        return this.view;
    }
   
  public void setName(String name){    
    if (name != null){
      this.textfield.setText(name);  
    }        
  }
  
  public String getName(){
    return this.textfield.getText().toString();
  }
  
  public void createNew(String name){
    this.book = this.pager.createBook();
    this.book.setName(name);
    this.setBook(this.book);
  }
  
  public void setBook(String text){
    if (this.book == null){
      this.book = this.pager.createBook();
    }
    
    this.book.setString(text);
    this.setBook(this.book);
  }
  // set the book...
  public void setBook(Book book){    
    this.clear();
    this.book = book;
    this.setName( book.getName() );    
    this.wordAdapter.set(this.book.getWords());        
    this.selectAll(true);
  }
  
  
  // selects all list items
  public void selectAll(Boolean check){
    for (int index  = 0; index < this.list.getChildCount(); index++){
      View listItem = this.list.getChildAt(index);
      CheckBox item = (CheckBox) listItem.findViewById(R.id.select);
      item.setChecked(check);
    }
  }
  
  // select none
  public void clearSelected(){
    this.selectAll(false);
  }
  
  public void clear(){
    this.book = null;
    this.setName("");
    
    this.wordAdapter = new SelectableWordListAdapter(this.pager);
    this.list.setAdapter(this.wordAdapter);    
  }
  
  // get selected items from list
  public List<String> getSelected(){
      List<String> words = new ArrayList<String>();
      
    for (int index  = 0; index < this.list.getChildCount(); index++){
      View listItem = this.list.getChildAt(index);
      CheckBox item = (CheckBox) listItem.findViewById(R.id.select);
      
      if (item.isChecked() == true){
        words.add( (String) listItem.getTag() );
      }
    }
      return words;
      
  }
  
  public void addWords(String text){
    if (this.book == null){
      this.book = this.pager.createBook();      
      this.book.setName(this.getName());
    }
        
    String[] words = text.split(" ");
    
    for (String word : words){
      this.book.addWord(word);  
    }

    if (this.wordAdapter == null){
      this.wordAdapter = new SelectableWordListAdapter(this.pager);
    }
    
    this.wordAdapter.set(this.book.getWords());
    this.list.setAdapter(this.wordAdapter);
  }
  
  public void showAddDialog(){
    final EditBookFragment me = this;
    final EditText textBox = new EditText(this.pager);
    new AlertDialog.Builder(this.pager).setTitle("Add words").setMessage("add new words to collection")    
    .setView(textBox)    
    .setPositiveButton("Create", new DialogInterface.OnClickListener() {    
      public void onClick(DialogInterface dialog, int whichButton) {            
        me.addWords(textBox.getText().toString());
    }}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {}}).show();            
  }  
  
  // return the data
  @Override
  public void onClick(View v) {
    switch ( v.getId() ){
      case R.id.ok:
        Book bk =  this.pager.createBook();
        bk.setName(this.getName());
        bk.addWords(this.getSelected());        
        
        pager.addBook(bk);
        
        this.pager.hideKeyboard(this.textfield);
        this.clear();
        pager.show(pager.BOOKLIST,true);
        
        
      break;
      case R.id.opencamera:
        Log.d("Debug",pager.cameraWord);
      break;
      case R.id.addstring:
        if (this.book == null){
          this.book = this.pager.createBook();
        }
        this.showAddDialog();
        break;
      case R.id.deselect:
        this.clearSelected();
      break;
      case R.id.select:
        this.selectAll(true);
      break;
    }
  }



    
    
}




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