Android Open Source - LearnByHeart Main Activity






From Project

Back to project page LearnByHeart.

License

The source code is released under:

Apache License

If you think the Android project LearnByHeart 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.mps.learn.pb.ui.activities;
/*from www  .j  a v a  2s  .c  o  m*/
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

import com.mps.learn.pb.PhraseManager;
import com.mps.learn.pb.R;
import com.mps.learn.pb.adapter.AllPhraseAdapter;
import com.mps.learn.pb.http.DataUpdaterCallback;
import com.mps.learn.pb.http.DataUpdaterTask;
import com.mps.learn.pb.model.PhraseBook;
import com.mps.learn.pb.model.PhraseModel;
import com.mps.learn.pb.util.Debugger;

public class MainActivity extends ActionBarActivity implements  DataUpdaterCallback{

  private ProgressDialog mLoadingProgressDialog ;
  private ListView mPhraseListView ;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPhraseListView = (ListView) findViewById(R.id.list_view_phrases);
    
    // register for context menu
    registerForContextMenu(mPhraseListView);
    
    mLoadingProgressDialog = ProgressDialog.show(this,"" , "Loading..", true);
    mLoadingProgressDialog.setCancelable(false);
    
    DataUpdaterTask updateTask = new DataUpdaterTask(this);
    updateTask.execute();
  }
  
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    if (v.getId()==R.id.list_view_phrases) {
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
      // menu.setHeaderTitle(""); // info.positino
      
      String[] menuItems = getResources().getStringArray(R.array.phrase_list_contextmenu);
      for (int i = 0; i<menuItems.length; i++) {
        menu.add(Menu.NONE, i, i, menuItems[i]);
      }
    }
  }
  
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    int menuItemIndex = item.getItemId();
    String[] menuItems = getResources().getStringArray(R.array.phrase_list_contextmenu);
    String menuItemName = menuItems[menuItemIndex];
    //Toast.makeText(this, menuItemName, Toast.LENGTH_SHORT).show();
    if( menuItemName.contains("Add")){
      showAddMoreDialog();
    }
    return true;
  }

  private void showAddMoreDialog() {
    // TODO Auto-generated method stub
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog_add_more_phrase);
    dialog.setTitle("Add More");

    // set the custom dialog components - text, image and button
    Spinner spChooseLang = (Spinner) dialog.findViewById(R.id.spinner_choose_lang);
   
    EditText phrase = (EditText) dialog.findViewById(R.id.et_phrase_box);

    Button dialogButton = (Button) dialog.findViewById(R.id.btnSave);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.dismiss();
      }
    });

    dialog.show();
  }

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_settings:
              startActivity(new Intent(this,SettingActivity.class));
            return true;
            case R.id.menu_add:
              startActivity(new Intent(this,AddPhraseActivity.class));
            return true;            
            default:
                return super.onOptionsItemSelected(item);
        }
    }

  @Override
  public void callback(PhraseBook phraseBook) {
    displayPhraseData(phraseBook);
    
    // Set the adapter on the ListView
    AllPhraseAdapter adapter = new AllPhraseAdapter();
    mPhraseListView.setAdapter(adapter);
    
    mLoadingProgressDialog.dismiss();
    
     
  }

  private void displayPhraseData(PhraseBook phraseBook) {
    if( phraseBook != null){
      Log.i(Debugger.TAG, "Size of json phrases : " + phraseBook.getPhraseList().size());
      
      // clear all phrase data
      PhraseManager.getInstance().getPhraseList().clear();
      
      for( PhraseModel phrase : phraseBook.getPhraseList()){
        // save to db
        PhraseManager.getInstance().addPhrase(0, phrase);
      }// end for
    }// end if    
  }
}




Java Source Code List

com.mps.learn.pb.App.java
com.mps.learn.pb.PhraseManager.java
com.mps.learn.pb.adapter.AllPhraseAdapter.java
com.mps.learn.pb.alarm.ReminderReceiver.java
com.mps.learn.pb.alarm.ReminderUtil.java
com.mps.learn.pb.db.DataSourcePhrase.java
com.mps.learn.pb.db.DatabaseHelper.java
com.mps.learn.pb.db.PhraseReaderContract.java
com.mps.learn.pb.gcm.GcmBroadcastReceiver.java
com.mps.learn.pb.gcm.GcmIntentService.java
com.mps.learn.pb.http.DataUpdaterCallback.java
com.mps.learn.pb.http.DataUpdaterTask.java
com.mps.learn.pb.http.HttpConstant.java
com.mps.learn.pb.http.PhraseParser.java
com.mps.learn.pb.model.PhraseBook.java
com.mps.learn.pb.model.PhraseModel.java
com.mps.learn.pb.ui.activities.AddPhraseActivity.java
com.mps.learn.pb.ui.activities.MainActivity.java
com.mps.learn.pb.ui.activities.PhraseViewActivity.java
com.mps.learn.pb.ui.activities.SettingActivity.java
com.mps.learn.pb.ui.factivities.BaseActivity.java
com.mps.learn.pb.ui.factivities.FActivity.java
com.mps.learn.pb.ui.factivities.FoldableListActivity.java
com.mps.learn.pb.ui.factivities.ItemsAdapter.java
com.mps.learn.pb.ui.factivities.PaintingsAdapter.java
com.mps.learn.pb.ui.factivities.SpannableBuilder.java
com.mps.learn.pb.ui.factivities.UnfoldableDetailsActivity.java
com.mps.learn.pb.ui.views.FoldableItemLayout.java
com.mps.learn.pb.ui.views.FoldableListLayout.java
com.mps.learn.pb.ui.views.UnfoldableView.java
com.mps.learn.pb.ui.views.shading.FoldShading.java
com.mps.learn.pb.ui.views.shading.GlanceFoldShading.java
com.mps.learn.pb.ui.views.shading.SimpleFoldShading.java
com.mps.learn.pb.util.CommonConstants.java
com.mps.learn.pb.util.Debugger.java