Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

deckyfx/simpleadapter

Repository files navigation

Simple Adapter

Adapterview made easy

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
	...
		maven { url 'https://jitpack.io' }
	}
}

Add the dependency

dependencies {
	compile 'com.github.deckyfx:simpleadapter:-SNAPSHOT'
	// include the parser, gson	
	compile 'com.google.code.gson:gson:2.8.1'
	// if you prefer moshi
	compile 'com.squareup.moshi:moshi:1.5.0'
	// if you prefer jakson
    compile 'com.fasterxml.jackson.core:jackson-databind:2.8.4'
    compile 'com.fasterxml.jackson.core:jackson-core:2.8.4'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.4'

}

Sample Code

extends JSONParserAdapter to your liking

class MyParser extends JSONParserAdapter{
    @Override
    public void init() {
        // init the parser
    }

    @Override
    public Object fromJson(String json, Class<? extends BaseItem> klas) {
        //return parser deserialize result;
    }

    @Override
    public Object fromJsonArray(String json, Class<? extends BaseItem[]> klas) {
        //return parser deserialize result;
    }

    @Override
    public String toJson(BaseItem baseItem) {
        //return parser serialize result;
    }
}

Simple Adapter

...
MyParser parser = New MyParser();
BaseItem.setJSONParser(parser);

private AdapterDataSet<AdapterItem> dataset = new AdapterDataSet<AdapterItem>();
private SimpleAdapter<AdapterItem> adapter;
...
...
adapter = new SimpleAdapter<AdapterItem>(this, dataset);
listview.setAdapter(adapter);
dataset.add(new AdapterItem("Item 1"));
dataset.add(new AdapterItem("Item 2"));
adapter.notifyDataSetChange();
...

Custom View

...
adapter = new SimpleAdapter<AdapterItem>(this, dataset, R.layout.item_layoout, ItemVH);
listview.setAdapter(adapter);
dataset.add(new AdapterItem("Item 1"));
dataset.add(new AdapterItem("Item 2"));
adapter.notifyDataSetChange();
private static class ItemVH<E extends AdapterItem> extends AbstractViewHolder<E> {

    public ItemVH(View itemView) {
        super(itemView);

        // init your view here
    }

    @Override
    public void setupView(Context ctx, int groupPosition, int itemPosition, AdapterItem item) {
        // setup your view here
    }

}
...

More sample is [here]

Feature:

  • List View Adapter
  • Expandable List View Adapter
  • Recycle View Adapter
  • Using Gson to parse JSON
  • Parse date from string / viceversa
  • Listen to item view onclick, ontouch, and onbindview