diff options
author | lado <herrlado@gmail.com> | 2011-10-10 01:42:42 +0200 |
---|---|---|
committer | lado <herrlado@gmail.com> | 2011-10-10 01:42:42 +0200 |
commit | a556b9a6fdb433d21345f6f30b5eae1d741ae95e (patch) | |
tree | 2ea81adbe8c619e2a63f79ffcb75f9e6bf85cbb5 | |
parent | 2c9a6ea80f7fc39e931f45c5a71c841aa3fce728 (diff) | |
download | vdr-manager-a556b9a6fdb433d21345f6f30b5eae1d741ae95e.tar.gz vdr-manager-a556b9a6fdb433d21345f6f30b5eae1d741ae95e.tar.bz2 |
implemented firtelable
-rw-r--r-- | vdrmanager/src/de/bjusystems/vdrmanager/gui/EventAdapter.java | 101 |
1 files changed, 94 insertions, 7 deletions
diff --git a/vdrmanager/src/de/bjusystems/vdrmanager/gui/EventAdapter.java b/vdrmanager/src/de/bjusystems/vdrmanager/gui/EventAdapter.java index 181195a..9cf1b6a 100644 --- a/vdrmanager/src/de/bjusystems/vdrmanager/gui/EventAdapter.java +++ b/vdrmanager/src/de/bjusystems/vdrmanager/gui/EventAdapter.java @@ -1,12 +1,20 @@ package de.bjusystems.vdrmanager.gui; +import java.util.ArrayList; +import java.util.List; + import android.content.Context; import android.graphics.Color; +import android.text.Spannable; +import android.text.SpannableString; import android.text.TextUtils; +import android.text.style.ForegroundColorSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.Filter; +import android.widget.Filterable; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; @@ -15,12 +23,16 @@ import de.bjusystems.vdrmanager.data.Event; import de.bjusystems.vdrmanager.data.EventFormatter; import de.bjusystems.vdrmanager.data.EventListItem; -abstract class EventAdapter extends ArrayAdapter<EventListItem> { +abstract class EventAdapter extends ArrayAdapter<EventListItem> implements + Filterable { + + private static final ForegroundColorSpan HIGHLIGHT = new ForegroundColorSpan( + Color.RED); protected final int layout; protected final LayoutInflater inflater; - //protected final List<EventListItem> items; - + protected final List<EventListItem> items = new ArrayList<EventListItem>(); + protected boolean hideChannelName = false; public EventAdapter(final Context context, int layout) { @@ -30,6 +42,14 @@ abstract class EventAdapter extends ArrayAdapter<EventListItem> { } @Override + public void add(EventListItem object) { + items.add(object); + super.add(object); + } + + private String highlight; + + @Override public View getView(final int position, final View convertView, final ViewGroup parent) { @@ -104,14 +124,18 @@ abstract class EventAdapter extends ArrayAdapter<EventListItem> { } final EventFormatter formatter = getEventFormatter(item.getEvent()); itemHolder.time.setText(formatter.getTime()); - if(hideChannelName){ + if (hideChannelName) { itemHolder.channel.setVisibility(View.GONE); } else { itemHolder.channel.setText(item.getChannelName()); } - itemHolder.title.setText(formatter.getTitle()); - itemHolder.shortText.setText(TextUtils.isEmpty(formatter - .getShortText()) ? " " : formatter.getShortText().trim()); + + + CharSequence title = highlight(formatter.getTitle(), highlight); + CharSequence shortText = highlight(formatter.getShortText(), highlight); + + itemHolder.title.setText(title); + itemHolder.shortText.setText(shortText); int p = Utils.getProgress(item); if (p == -1) { itemHolder.progress.setVisibility(View.GONE); @@ -136,8 +160,71 @@ abstract class EventAdapter extends ArrayAdapter<EventListItem> { return view; } + private CharSequence highlight(String where, String what){ + if(TextUtils.isEmpty(what)){ + return where; + } + + String str = where.toLowerCase(); + int idx = str.indexOf(highlight); + if(idx == -1){ + return where; + } + SpannableString ss = new SpannableString(str); + ss.setSpan(HIGHLIGHT, idx, idx + highlight.length(), + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + return ss; + } + protected EventFormatter getEventFormatter(Event event) { return new EventFormatter(event); } + private void addSuper(EventListItem item) { + super.add(item); + } + + public Filter getFilter() { + return new Filter() { + EventListItem prevHead = null; + + @Override + protected FilterResults performFiltering(CharSequence arg0) { + highlight = arg0.toString().toLowerCase(); + ArrayList<EventListItem> result = new ArrayList<EventListItem>(); + for (EventListItem event : items) { + if (event.isHeader()) { + prevHead = event; + //result.add(event); + continue; + } + if (event.getTitle().toLowerCase() + .indexOf(String.valueOf(arg0).toLowerCase()) != -1 + || event.getShortText() + .toLowerCase() + .indexOf(String.valueOf(arg0).toLowerCase()) != -1) { + if(prevHead != null){ + result.add(prevHead); + prevHead = null; + } + result.add(event); + } + } + + FilterResults fr = new FilterResults(); + fr.count = result.size(); + fr.values = result; + return fr; + } + + @Override + protected void publishResults(CharSequence arg0, FilterResults arg1) { + EventAdapter.this.clear(); + for (EventListItem item : (ArrayList<EventListItem>) arg1.values) { + addSuper(item); + } + notifyDataSetChanged(); + } + }; + } }
\ No newline at end of file |