From a556b9a6fdb433d21345f6f30b5eae1d741ae95e Mon Sep 17 00:00:00 2001 From: lado Date: Mon, 10 Oct 2011 01:42:42 +0200 Subject: implemented firtelable --- .../de/bjusystems/vdrmanager/gui/EventAdapter.java | 101 +++++++++++++++++++-- 1 file changed, 94 insertions(+), 7 deletions(-) (limited to 'vdrmanager/src/de/bjusystems') 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 { +abstract class EventAdapter extends ArrayAdapter implements + Filterable { + + private static final ForegroundColorSpan HIGHLIGHT = new ForegroundColorSpan( + Color.RED); protected final int layout; protected final LayoutInflater inflater; - //protected final List items; - + protected final List items = new ArrayList(); + protected boolean hideChannelName = false; public EventAdapter(final Context context, int layout) { @@ -29,6 +41,14 @@ abstract class EventAdapter extends ArrayAdapter { inflater = LayoutInflater.from(context); } + @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 { } 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 { 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 result = new ArrayList(); + 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) arg1.values) { + addSuper(item); + } + notifyDataSetChanged(); + } + }; + } } \ No newline at end of file -- cgit v1.2.3