Notice
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- annotation
- junit
- ERD
- TDD
- 우분투
- 오류
- 안드로이드
- 메모
- java se
- 초대장
- 실수
- 오라클
- eclipse
- action
- Modeling
- 초대장 배포
- 디자인 오류
- Log4j
- struts2
- TAG 오류
- Android
- DATABASE
- eclipse plugin
- java
- iBATIS
- Spring Framework
- 관심책
- derby
- Oracle
- Spring
- Today
- 0
- Total
- 579,107
거꾸로 토마토
[안드로이드] ListView 사용법 (Item추가, custom Adapter 작성) 본문
ListView는 Adapter를 통해서 Item을 제어합니다. Adapter를 custom 으로 작성하면 item의 구성 및 기타 스타일 또한 일일히 변경가능합니다. 다음과 같이 ArrayAdapter을 상속한 custom adapter을 작성합니다. Activity 내에 다음과 같은 inner class로 작성을 했습니다.
private SearchItemAdapter adapter;
...
adapter = new SearchItemAdapter(this, R.layout.searcharitemsrow, arItems);
resultListView.setAdapter(adapter);
resultListView.setDividerHeight(1);
...
private class SearchItemAdapter extends ArrayAdapter<CustomItem> {
public Vector<CustomItem> items ;
private int textViewResourceId;
public SearchItemAdapter(Context context, int textViewResourceId, Vector<CustomItem> items) {
super(context, textViewResourceId, items);
this.items = items;
this.textViewResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(this.textViewResourceId, null);
}
if(items!=null && items.size() > 0) {
CustomItem item = items.get(position);
if(item != null) {
TextView nmView = (TextView) view.findViewById(R.id.searchRow_nm);
nmView.setText(item.getName());
}
}
return view;
}
}
생성자를 구성하고 getView(...) method를 구현합니다. 이 때 CustomItem는 각 Item에 적용할 data를 보유하고 있는 객체입니다. getView는 각 item을 어떻게 구성 할 것인지를 구현합니다.
adapter = new SearchItemAdapter(this, R.layout.searcharitemsrow, arItems);
이 부분에서 adapter 객체를 생성하고 ListView에 adapter로 등록하고 사용하면 됩니다. "searcharitemsrow"는 layout을 구성한 xml 명입니다. row한 단위의 layout을 지정해 놓습니다.
이렇게 구성된 ListView에서 row(Item)을 변경한다면 adapter의 객체를 조작하면 됩니다.
adapter.clear() - ListView의 모든 row가 제거됨.
adapter.add(customItem) - ListView에 row를 추가
CustomItem adapter.getItem(int position) - 해당 index의 item 객체 얻기
adapter.add(customItem) - ListView에 row를 추가
CustomItem adapter.getItem(int position) - 해당 index의 item 객체 얻기
등의 작업을 통해 제어 할 수 있습니다. adapter의 기타 method를 통해 더 다양한 처리가 가능합니다.
'Programming' 카테고리의 다른 글
대용량 데이터 엑셀파일 생성 (0) | 2011.02.11 |
---|---|
[안드로이드] ListView 사용법 (Item추가, custom Adapter 작성) (0) | 2010.11.03 |
안드로이드:Activity 방향 고정 방법 (0) | 2010.10.01 |
Java 오류 : java.lang.UnsupportedClassVersionError: Bad version number in .class file (0) | 2010.06.01 |
0 Comments