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'
}
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;
}
}
...
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();
...
...
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]
- 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