Skip to content
cab404 edited this page Oct 14, 2017 · 3 revisions

To use chumroll adapter you will need to create ViewConverters.
Those things should grab data type of your choice and create a view for it - almost like in BaseAdapter, yet with several differences.
It doesn't manage data, unless you want it to. All the data is held in ChumrollAdapter.
It won't throw any nulls or views of different types at you, only views you created inside of it.

Well. Not like BaseAdapter at all.

For this example I will use simple cat POJO

public class Cat {
    public String name = "Tom";
    public int color = 0xff0c4b40;
}

To make a ViewConverter, you'll need to implement three methods:
createView - creates new layouts
convert - fills views you created with supplied data from ChumrollAdapter
enabled - functionality of isEnabled method of BaseAdapter

After you've done, it may look somewhat like this:

public class CatViewConverter implements ViewConverter<Cat> {

    @Override
    public View createView(LayoutInflater inflater, ViewGroup parent, ChumrollAdapter adapter) {
        return inflater.inflate(R.layout.cat_info, parent, false);
    }

    @Override
    public void convert(View view, Cat data, int index, ViewGroup parent, ChumrollAdapter adapter) {
        TextView name = (TextView) view.findViewById(R.id.name);
        name.setText(data.name);
        name.setTextColor(~data.color | 0xff000000);
        view.setBackgroundColor(data.color);
    }

    @Override
    public boolean enabled(Cat data, int index, ChumrollAdapter adapter) {
        return true;
    }

}

As you can see, this thing is full of boilerplate code, so I would suggest making something butterknife-y like this:

public abstract class AbsViewConverter<A> implements ViewConverter<A> {

    @LayoutRes
    protected int layoutID;

    @Override
    public View createView(LayoutInflater inflater, ViewGroup parent, ChumrollAdapter adapter) {
        return inflater.inflate(layoutID, parent, false);
    }

    @Override
    public void convert(View view, A data, int index, ViewGroup parent, ChumrollAdapter adapter) {
        ButterKnife.bind(this, view);
    }

    @Override
    public boolean enabled(A data, int index, ChumrollAdapter adapter) {
        return false;
    }

}

then you will get something like this in your converters:

public class CatViewConverter extends AbsViewConverter<Cat> {

    {layoutID = R.layout.cat_info;}

    @Bind(R.id.name)
    TextView name;

    @Override
    public void convert(View view, Cat data, int index, ViewGroup parent, ChumrollAdapter adapter) {
        super.convert(view, data, index, parent, adapter);
        name.setText(data.name);
        name.setTextColor(~data.color | 0xff000000);
        view.setBackgroundColor(data.color);
    }

}

I didn't include any of abstract classes like this because it's pretty easy to do it yourself, and you may not be liking butterknife, or may even not use xml layouts. So you can do it as you please.

And that's all about converters. To try it out we can use something like this:

public class TestActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView list = new ListView(this);
        setContentView(list);

        ChumrollAdapter adapter = new ChumrollAdapter();
        adapter.add(CatViewConverter.class, new Cat());
        list.setAdapter(adapter);
    }
}

If you want to add data after you set an adapter, then add converter instance to it beforehand, like so:

/* ... */
ChumrollAdapter adapter = new ChumrollAdapter();
adapter.prepareFor(new CatViewConverter());
list.setAdapter(adapter);
adapter.add(new Cat())
/* ... */

It is needed because number of data types is fixed inside of listview at the moment of setting adapter, and won't change until you unplug and replug adapter to it.

Clone this wiki locally