Back to project page SNET.
The source code is released under:
GNU Lesser General Public License
If you think the Android project SNET 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.snet.notepad; //w ww . j av a2s . c o m import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Locale; import com.snet.R; import com.snet.R.id; import com.snet.R.layout; import com.snet.MainActivity; import com.snet.textedit.TextEdition; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.content.res.XmlResourceParser; import android.graphics.Color; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Debug; import android.os.Environment; import android.preference.PreferenceManager; import android.text.Editable; import android.text.Html; import android.text.Layout.Alignment; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.TextWatcher; import android.text.SpannableString; import android.text.style.AlignmentSpan; import android.text.style.StyleSpan; import android.util.Log; import android.view.ContextMenu.ContextMenuInfo; import android.view.*;//ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.*; import android.widget.AdapterView.AdapterContextMenuInfo; public class NoteMain extends Activity { final String EXTRA_TITLE = "TitreNoteEdition"; final String EXTRA_NOTE = "NoteEdition"; final String EXTRA_EDITION = "edition"; final String EXTRA_ID = "id"; ArrayAdapter<Note> simpleAdpt; EditText editsearch; List<Note> listeNotes; NotesBDD noteBdd; ListView lv; SharedPreferences pref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notemain); pref = PreferenceManager.getDefaultSharedPreferences(this); noteBdd = new NotesBDD(this); noteBdd.open(); listeNotes = noteBdd.getAllNotes(Integer.parseInt(pref.getString("pref_tri", "1")), pref.getBoolean("pref_ordretri", false)); /****************************************************************************************/ // The data to show lv = (ListView) findViewById(R.id.listView); simpleAdpt = new ArrayAdapter<Note>(this, R.layout.notelist, listeNotes ){ public View getView(int position, View view, ViewGroup viewGroup) { view = super.getView(position, view, viewGroup); Note n = (Note) this.getItem(position); if( pref.getBoolean("pref_date", false) == false) ((TextView)view).setText(Html.fromHtml("<b>"+n.getTitre() + "</b> <br/>"+n.getNoteHead())); else ((TextView)view).setText(Html.fromHtml("<b>"+n.getTitre() + "</b> " + " <br/>"+n.getNoteHead()+"<br/>" + n.getDateCreationFormated())); return view; } }; lv.setAdapter(simpleAdpt); // React to user clicks on item lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) { Note n = (Note) parentAdapter.getItemAtPosition(position); // We know the View is a TextView so we can cast it TextView clickedView = (TextView) view; Intent intentTextEdition = new Intent(NoteMain.this , NoteEdition.class); intentTextEdition.putExtra(EXTRA_TITLE, n.getTitre()); intentTextEdition.putExtra(EXTRA_NOTE, n.getNote()); intentTextEdition.putExtra(EXTRA_EDITION, true); intentTextEdition.putExtra(EXTRA_ID, n.getId()); NoteMain.this.startActivity(intentTextEdition); } }); // we register for the contextmneu registerForContextMenu(lv); // Locate the EditText in listview_main.xml editsearch = (EditText) findViewById(R.id.search); editsearch.setVisibility(View.GONE); // Capture Text in EditText editsearch.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub // adapter.filter(text); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { String text = editsearch.getText().toString().toLowerCase(Locale.getDefault()); ArrayList<Note> listeNotesRecherche = noteBdd.getSearchedNotes(text, pref.getBoolean("contentSearch", false)); simpleAdpt = new ArrayAdapter<Note> (getApplicationContext(), R.layout.notelist, listeNotesRecherche ){ public View getView(int position, View view, ViewGroup viewGroup) { View v = super.getView(position, view, viewGroup); //((TextView)v).setTextSize(14); Note n = (Note) this.getItem(position); ((TextView)v).setText(Html.fromHtml("<b>"+n.getTitre() + "</b> <br/>"+n.getNoteHead())); return v; } }; lv.setAdapter(simpleAdpt); //simpleAdpt.notifyDataSetChanged(); } }); noteBdd.close(); } @Override public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Note Menu"); menu.add(0, v.getId(), 0, "diter"); menu.add(0, v.getId(), 0, "Supprimer"); menu.add(0, v.getId(), 0, "Dtails"); } @Override public boolean onContextItemSelected(MenuItem item) { NotesBDD noteBdd = new NotesBDD(this); noteBdd.open(); AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item.getMenuInfo(); int noteid = (int) item.getItemId(); Note note = (Note) simpleAdpt.getItem(aInfo.position); if(item.getTitle().equals("diter")) { Intent intentTextEdition = new Intent(NoteMain.this , NoteEdition.class); intentTextEdition.putExtra(EXTRA_TITLE, note.getTitre()); intentTextEdition.putExtra(EXTRA_NOTE, note.getNote()); intentTextEdition.putExtra(EXTRA_EDITION, true); intentTextEdition.putExtra(EXTRA_ID, note.getId()); NoteMain.this.startActivity(intentTextEdition); } else if(item.getTitle().equals("Supprimer")) { simpleAdpt.remove(note); noteBdd.removeNoteWithID(note.getId()); Toast.makeText(this, "Note supprime ! ", Toast.LENGTH_LONG).show(); simpleAdpt.notifyDataSetChanged(); // Refresh main activity upon close of dialog box /*Intent refresh = new Intent(this, NoteMain.class); startActivity(refresh); this.finish(); //*/ //onCreate(null); } if(item.getTitle().equals("Dtails")) { AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("Dtails"); String dateC = note.getDateCreation(); String dateM = note.getDateModification(); if (dateC.equals(dateM)) { alertDialog.setMessage(Html.fromHtml("<b>Titre : "+note.getTitre() + "</b> <br/>"+note.getNoteHead() + "<br/>Nombre de caractres : " +note.getNote().length() + "<br/><i>Cre le "+ note.getDateCreationFormated()+"</i>"+ "<br/><i>Non modifi </i>")); } else { alertDialog.setMessage(Html.fromHtml("<b>Titre : "+note.getTitre() + "</b> <br/>"+note.getNoteHead() + "<br/>Nombre de caractres : " +note.getNote().length() + "<br/><i>Cre le "+ note.getDateCreationFormated()+"</i>" + "<br/><i>Modifi le "+ note.getDateModificationFormated() +"</i>")); } alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Add your code for the button here. } }); // Set the Icon for the Dialog //alertDialog.setIcon(R.drawable.icon); alertDialog.show(); } else { return false; } noteBdd.close(); return true; } protected void onActivityResult(int requestCode, int resultCode, Intent data) { onCreate(null); } protected void onResume() { onCreate(null); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_SEARCH: if(editsearch.getVisibility() == View.VISIBLE) { editsearch.setVisibility(View.GONE); } else { editsearch.setVisibility(View.VISIBLE); } return true; case KeyEvent.KEYCODE_MENU: Intent i = new Intent(this, Preference.class); startActivity(i); return true; default: return super.onKeyUp(keyCode, event); } } public void addNote(View v ) { Intent intentTextEdition = new Intent(NoteMain.this , NoteEdition.class); NoteMain.this.startActivity(intentTextEdition); } public void quit(View v) { /* Intent intent = new Intent(NoteMain.this , MainActivity.class); NoteMain.this.startActivity(intent);*/ this.finish(); } }