https://github.com/stephanenicolas/typedadapter
Object Oriented android list view adapter(s) using generics to display given Pojos.
https://github.com/stephanenicolas/typedadapter
Last synced: 10 months ago
JSON representation
Object Oriented android list view adapter(s) using generics to display given Pojos.
- Host: GitHub
- URL: https://github.com/stephanenicolas/typedadapter
- Owner: stephanenicolas
- License: apache-2.0
- Created: 2014-05-14T14:03:40.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-05-15T05:50:18.000Z (over 11 years ago)
- Last Synced: 2025-01-25T12:28:03.639Z (12 months ago)
- Language: Java
- Size: 246 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
TypedAdapter
============
Object Oriented android list view adapter(s) using generics to display given Pojos.
This technique is an alternative to the view holder design pattern. It has the following advantages :
* is strongly typed ;
* allows to avoid the `setTag` ugly hack of the view holders ;
* gives the opportunity to create full android custom views, elegant and complete, that can even be reused as XML layout elements.
[](https://travis-ci.org/stephanenicolas/TypedAdapter)
Usage
-----
Let's say you got a POJO class :
```java
public class Pojo {
public String name;
}
```
To display a list of Pojo into a `ListView` using Typed Adapter, follow the 2 following steps.
Define your adapter using both the POJO type and your custom view type :
```java
public class PojoAdapter extends TypedArrayAdapter {
public PojoAdapter(Context context, List pojos) {
super(context, pojos);
}
@Override
public PojoView createView() {
return new PojoView(getContext());
}
}
````
Define your custom view type and implement `TypedCellView`
```java
public class PojoView extends RelativeLayout implements TypedCellView {
private TextView textViewName;
public PojoView(Context context) {
super(context);
LayoutInflater.from(context).inflate(, this);
textViewName = (TextView) findViewById(R.id.textView_name);
//good practice
onFinishInflate();
}
@Override
public void update(Pojo pojo) {
textViewName.setText(pojo.name);
}
}
```
You need a single constructor using a `Context` as parameter when creating a view programmatically (here, the adapter does). Add the other `View` constructors if you want to make your custom view a full android component that can be inflated via XML.