From e6cafe38ff3363eff77acdef2b77a655563e8c73 Mon Sep 17 00:00:00 2001 From: Suraj Patil Date: Wed, 25 Mar 2015 11:03:13 +0530 Subject: [PATCH 1/2] added search & share --- .../materialnotes/activity/MainActivity.java | 76 ++++++++++++++++++- .../materialnotes/widget/NotesAdapter.java | 53 ++++++++++++- app/src/main/res/menu/context_note.xml | 13 +++- app/src/main/res/menu/main.xml | 10 ++- app/src/main/res/values/strings.xml | 6 +- 5 files changed, 149 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/materialnotes/activity/MainActivity.java b/app/src/main/java/com/materialnotes/activity/MainActivity.java index 2316db7..2ff241d 100644 --- a/app/src/main/java/com/materialnotes/activity/MainActivity.java +++ b/app/src/main/java/com/materialnotes/activity/MainActivity.java @@ -68,6 +68,7 @@ public void onClick(View view) { } }); selectedPositions = new ArrayList<>(); + listView.setTextFilterEnabled(true); setupNotesAdapter(); setupActionModeCallback(); setListOnItemClickListenersWhenNoActionMode(); @@ -75,9 +76,43 @@ public void onClick(View view) { } /** {@inheritDoc} */ + @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); + // Get the SearchView and set the searchable configuration + SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); + SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); + // Assumes current activity is the searchable activity + searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); + searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default + + SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() + { + @Override + public boolean onQueryTextChange(String query) + { + + listAdapter.getFilter().filter(query); + return true; + } + @Override + public boolean onQueryTextSubmit(String query) + { + // this is your adapter that will be filtered + + return false; + } + }; + + searchView.setOnCloseListener( new SearchView.OnCloseListener() { + @Override + public boolean onClose() { + listAdapter.getFilter().filter(""); + return false; + } + }); + searchView.setOnQueryTextListener(textChangeListener); return true; } @@ -156,6 +191,16 @@ public void onClick(DialogInterface dialog, int which) { .show(); } else mode.finish(); return true; + + case R.id.action_share: + if (!selectedPositions.isEmpty()){ + shareNotes(selectedPositions); + } + return true; + + case R.id.action_select_all: + selectAll(); + return true; default: return false; } @@ -193,6 +238,15 @@ private void updateView() { } } + /* Method called when select all menu is clicked */ + private void selectAll() { + for ( int i=0; i < listAdapter.getCount(); i++) { + selectedPositions.add(i); + notesData.get(i).setSelected(true); + listAdapter.notifyDataSetChanged(); + } + } + /** * Agrega una nota a lista y la fuente de datos. * @@ -245,6 +299,26 @@ private void updateNote(Intent data) { listAdapter.notifyDataSetChanged(); } + /* +this method is called when the contextual action button of share is pressed + */ + private void shareNotes(ArrayList selectedPositions) { + StringBuilder shareBody = new StringBuilder(); + // Primero borra de la base de datos + for (int position : selectedPositions) { + NotesAdapter.NoteViewWrapper noteViewWrapper = notesData.get(position); + shareBody.append(noteViewWrapper.getNote().getTitle()); + shareBody.append(noteViewWrapper.getNote().getContent()); + shreBody.append("\n") + } + + Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); + sharingIntent.setType("text/plain"); + + sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody.toString()); + startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using))); + } + /** Reinicia las notas seleccionadas a no seleccionadas y limpia la lista de seleccionados. */ private void resetSelectedListItems() { for (NotesAdapter.NoteViewWrapper noteViewWrapper : notesData) noteViewWrapper.setSelected(false); @@ -309,4 +383,4 @@ public void onItemClick(AdapterView parent, View view, int position, long id) } }); } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/materialnotes/widget/NotesAdapter.java b/app/src/main/java/com/materialnotes/widget/NotesAdapter.java index 98f1728..968d785 100644 --- a/app/src/main/java/com/materialnotes/widget/NotesAdapter.java +++ b/app/src/main/java/com/materialnotes/widget/NotesAdapter.java @@ -8,6 +8,8 @@ import com.materialnotes.R; import com.materialnotes.data.Note; +import android.widget.Filter; +import android.widget.Filterable; import java.text.DateFormat; import java.util.List; @@ -18,7 +20,7 @@ * @author Daniel Pedraza Arcega * @see Building Layouts with an Adapter */ -public class NotesAdapter extends BaseAdapter { +public class NotesAdapter extends BaseAdapter implements Filterable { /** Wrapper para notas. Util para cambiar el fondo de los item seleccionados. */ public static class NoteViewWrapper { @@ -47,6 +49,7 @@ public void setSelected(boolean isSelected) { private static final DateFormat DATETIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); private final List data; + private List filtered_data; /** * Constructor. @@ -55,12 +58,13 @@ public void setSelected(boolean isSelected) { */ public NotesAdapter(List data) { this.data = data; + this.filtered_data = data; } /** @return cuantos datos hay en la lista de notas. */ @Override public int getCount() { - return data.size(); + return filtered_data.size(); } /** @@ -69,7 +73,7 @@ public int getCount() { */ @Override public NoteViewWrapper getItem(int position) { - return data.get(position); + return filtered_data.get(position); } /** @@ -100,7 +104,7 @@ public View getView(int position, View convertView, ViewGroup parent) { convertView.setTag(holder); } else holder = (ViewHolder) convertView.getTag(); // ya existe, solo es reciclarlo // Inicializa la vista con los datos de la nota - NoteViewWrapper noteViewWrapper = data.get(position); + NoteViewWrapper noteViewWrapper = filtered_data.get(position); holder.noteIdText.setText(String.valueOf(noteViewWrapper.note.getId())); holder.noteTitleText.setText(noteViewWrapper.note.getTitle()); // Corta la cadena a 80 caracteres y le agrega "..." @@ -136,4 +140,45 @@ private ViewHolder(View parent) { noteDateText = (TextView) parent.findViewById(R.id.note_date); } } + + + @Override + public Filter getFilter() { + Filter filter = new Filter(){ + + @Override + protected FilterResults performFiltering(CharSequence query) { + FilterResults results = new FilterResults(); + ArrayList filtered = new ArrayList<>(); + query = query.toString().toLowerCase(); + if(query!=null && query.length()>0){ + for(int i=0;i) results.values; + NotesAdapter.this.notifyDataSetChanged(); + } + }; + + return filter; + } + } \ No newline at end of file diff --git a/app/src/main/res/menu/context_note.xml b/app/src/main/res/menu/context_note.xml index 03a1d69..85ee6fc 100644 --- a/app/src/main/res/menu/context_note.xml +++ b/app/src/main/res/menu/context_note.xml @@ -7,5 +7,16 @@ NotepadApp:showAsAction="ifRoom" android:icon="@drawable/ic_action_delete" android:title="@string/action_delete"/> + + + - \ No newline at end of file + diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index bd8ec50..7298876 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -1,7 +1,13 @@ - + + - \ No newline at end of file + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3e27a47..9f0dc58 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,6 +10,10 @@ About Open source licenses Delete %s note(s)? + + Search notes + Select all + Open source licenses @@ -32,4 +36,4 @@ Created at: Updated at: - \ No newline at end of file + From 6cb9b24958e1b0f33e576c087b985b548fcb1d50 Mon Sep 17 00:00:00 2001 From: Suraj Patil Date: Wed, 25 Mar 2015 11:07:37 +0530 Subject: [PATCH 2/2] changed or to || --- app/src/main/java/com/materialnotes/widget/NotesAdapter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/materialnotes/widget/NotesAdapter.java b/app/src/main/java/com/materialnotes/widget/NotesAdapter.java index 968d785..434b08e 100644 --- a/app/src/main/java/com/materialnotes/widget/NotesAdapter.java +++ b/app/src/main/java/com/materialnotes/widget/NotesAdapter.java @@ -155,7 +155,7 @@ protected FilterResults performFiltering(CharSequence query) { for(int i=0;i