From 54cd37ae861e57b33c0a6f1b6b5098da848ec05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 29 Aug 2013 10:13:56 +0200 Subject: Added a custom adapter to the ProviderListFragment I've created a new adapter using code from http://www.sherif.mobi/2012/01/listview-with-ability-to-hide-rows.html and my own getView (moved from ProviderListFragment). --- .../leap/bitmaskclient/ProviderListFragment.java | 25 +------ src/se/leap/leapclient/ProviderListAdapter.java | 85 ++++++++++++++++++++++ 2 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 src/se/leap/leapclient/ProviderListAdapter.java diff --git a/src/se/leap/bitmaskclient/ProviderListFragment.java b/src/se/leap/bitmaskclient/ProviderListFragment.java index 05cd4b78..b9a10925 100644 --- a/src/se/leap/bitmaskclient/ProviderListFragment.java +++ b/src/se/leap/bitmaskclient/ProviderListFragment.java @@ -26,8 +26,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; -import android.content.Context; -import android.widget.TwoLineListItem; /** * A list fragment representing a list of Providers. This fragment @@ -91,27 +89,12 @@ public class ProviderListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - content_adapter = new ArrayAdapter( + content_adapter = new ProviderListAdapter( getActivity(), android.R.layout.simple_list_item_activated_2, - ProviderListContent.ITEMS) { - @Override - public View getView(int position, View convertView, ViewGroup parent){ - TwoLineListItem row; - if (convertView == null) { - LayoutInflater inflater = (LayoutInflater)getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); - row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null); - } else { - row = (TwoLineListItem)convertView; - } - ProviderListContent.ProviderItem data = ProviderListContent.ITEMS.get(position); - row.getText1().setText(data.domain); - row.getText2().setText(data.name); - - return row; - } - }; - setListAdapter(content_adapter); + ProviderListContent.ITEMS); + + setListAdapter(content_adapter); } @Override diff --git a/src/se/leap/leapclient/ProviderListAdapter.java b/src/se/leap/leapclient/ProviderListAdapter.java new file mode 100644 index 00000000..441763d0 --- /dev/null +++ b/src/se/leap/leapclient/ProviderListAdapter.java @@ -0,0 +1,85 @@ +package se.leap.leapclient; + +import java.util.List; +import java.lang.reflect.Array; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +import android.content.Context; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TwoLineListItem; + +public class ProviderListAdapter extends ArrayAdapter { + T[] items = null; + boolean[] hidden = null; + + public void hide(int position) { + hidden[getRealPosition(position)] = true; + notifyDataSetChanged(); + notifyDataSetInvalidated(); + } + public void unHide(int position) { + hidden[getRealPosition(position)] = false; + notifyDataSetChanged(); + notifyDataSetInvalidated(); + } + + private int getRealPosition(int position) { + int hElements = getHiddenCountUpTo(position); + int diff = 0; + for(int i=0;i objects) { + super(mContext, layout, objects); + items = objects.toArray((T[])new Object[0]); + hidden = new boolean[items.length]; + for (int i = 0; i < items.length; i++) + hidden[i] = false; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + TwoLineListItem row; + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null); + } else { + row = (TwoLineListItem)convertView; + } + ProviderListContent.ProviderItem data = ProviderListContent.ITEMS.get(position); + row.getText1().setText(data.domain); + row.getText2().setText(data.name); + + return row; + } +} -- cgit v1.2.3