Skip to content

Instantly share code, notes, and snippets.

@llavin
Last active August 17, 2016 00:19
Show Gist options
  • Save llavin/5855908 to your computer and use it in GitHub Desktop.
Save llavin/5855908 to your computer and use it in GitHub Desktop.
This is a generic hash map adapter that I made in a project for an android view.
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class HashMapAdapter<K, V> extends BaseAdapter{
private HashMap<K, V> mData = new HashMap<K, V>();
private ArrayList<K> mKeys;
public HashMapAdapter(HashMap<K, V> data) {
mData = data;
mKeys = new ArrayList<K>(mData.keySet());
Collections.sort(mKeys, Collections.reverseOrder());
}
public K getKey(int position)
{
return (K) mKeys.get(position);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public V getItem(int position) {
return mData.get(mKeys.get(position));
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
K key = getKey(pos);
V value = getItem(pos);
return onGetView(pos, key, value, convertView, parent );
}
protected abstract View onGetView(int pos, K key, V value, View convertView,
ViewGroup parent);
}
@m7mdra
Copy link

m7mdra commented Aug 17, 2016

hello , can you provide an implementation for this class , good work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment