https://github.com/aryaxt/abstractgroupedadapter
A simple way to implement grouped ListView on Android
https://github.com/aryaxt/abstractgroupedadapter
Last synced: 11 months ago
JSON representation
A simple way to implement grouped ListView on Android
- Host: GitHub
- URL: https://github.com/aryaxt/abstractgroupedadapter
- Owner: aryaxt
- License: other
- Created: 2013-10-05T22:39:04.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-10-05T23:13:42.000Z (over 12 years ago)
- Last Synced: 2025-06-20T08:06:46.723Z (12 months ago)
- Language: Java
- Size: 520 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: License.txt
Awesome Lists containing this project
README
AbstractGroupedAdapter
======================
A simple way to implement grouped ListView on Android
Step 1
------------------
Create 2 xml files 1 representing the header, and another representing the row
Header
```xml
```
Row
```xml
```
Step 2
------------------
Create a new Adapter inheriting from AbstractGroupedAdapter.
AbstractGroupedAdapter takes 2 generic types, the first one represents the object used to populate the header, the second one represents the object that is used to populate each row.
```java
public class MyAdapter extends AbstractGroupedAdapter {
public MyAdapter(Context context) {
super(context);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (this.isItemHeader(position)) {
String header = this.getHeader(position);
view = getReusableView(view, R.layout.header);
TextView textView = (TextView) view.findViewById(R.id.txtHeaderTitle);
textView.setText(header);
}
else {
Person person = this.getItem(position);
view = getReusableView(view, R.layout.row);
TextView textView = (TextView) view.findViewById(R.id.txtPersonName);
textView.setText(person.getName());
}
return view;
}
}
```
Step 3
------------------
Create a Map with your data and pass it to the adapter
```java
Map> data = new HashMap>();
List aPersons = new ArrayList();
aPersons.add(new Person("Aryan"));
aPersons.add(new Person("Alex"));
aPersons.add(new Person("Andrew"));
data.put("A", aPersons);
List bPersons = new ArrayList();
bPersons.add(new Person("Bob"));
bPersons.add(new Person("Bastard"));
bPersons.add(new Person("Banana"));
bPersons.add(new Person("Brandon"));
data.put("B", bPersons);
MyAdapter adapter = new MyAdapter(this);
adapter.setData(data);
listView.setAdapter(adapter);
```