diff options
author | Parménides GV <parmegv@sdf.org> | 2013-08-29 10:13:56 +0200 |
---|---|---|
committer | cyBerta <richy@cyborgsociety.org> | 2013-11-15 23:25:16 +0100 |
commit | 54cd37ae861e57b33c0a6f1b6b5098da848ec05b (patch) | |
tree | fed36f614ac1cd6e5b106cf0ec1b5eda70b82093 /src/se | |
parent | 6d5838ae7d1ffb022c28ce7d6a20a0045cb56e70 (diff) |
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).
Diffstat (limited to 'src/se')
-rw-r--r-- | src/se/leap/bitmaskclient/ProviderListFragment.java | 25 | ||||
-rw-r--r-- | src/se/leap/leapclient/ProviderListAdapter.java | 85 |
2 files changed, 89 insertions, 21 deletions
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<ProviderListContent.ProviderItem>(
+ content_adapter = new ProviderListAdapter<ProviderListContent.ProviderItem>(
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<T> extends ArrayAdapter<T> { + 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<hElements;i++) { + diff++; + if(hidden[position+diff]) + i--; + } + return (position + diff); + } + private int getHiddenCount() { + int count = 0; + for(int i=0;i<items.length;i++) + if(hidden[i]) + count++; + return count; + } + private int getHiddenCountUpTo(int location) { + int count = 0; + for(int i=0;i<=location;i++) { + if(hidden[i]) + count++; + } + return count; + } + + @Override + public int getCount() { + return (items.length - getHiddenCount()); + } + + public ProviderListAdapter(Context mContext, int layout, List<T> 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; + } +} |