티스토리 뷰
ListView에 아이템을 띄우는 방법은 여러개가 있고 UI를 커스터마이징 하는 방법도 여러개가 있다.
보통 이것 저것 막 상속하고 클래스 여러개 만들고 그런식으로 하던데, SimpleCursorAdapter를 사용하면 쉽게 끝낼 수 있다.
예제 코드 다운로드
예제는 과일 이름과 수량, 노트를 등록하면
리스트에 이름과 수량이 뜨고, 리스트를 터치(클릭)시에 노트가 Toast로 뜨도록 하였다.
다른 부분은 다 필요 없고 젤 중요한 부분 소스는 아래와 같다.
SQLiteDatabase db = openOrCreateDatabase("roter.db", Context.MODE_PRIVATE, null); //DB Open
ListView listView = (ListView)findViewById(R.id.main_listview_fruit); //ListView 열기
mCursor = db.rawQuery("SELECT * FROM fruit", null); //쿼리 날리고
mCursor.moveToFirst(); //커서 처음으로 보내고
String[] from = new String[]{"name","num"}; //가져올 DB의 필드 이름
int[] to = new int[]{R.id.lf_tv_name,R.id.lf_tv_num}; //각각 대응되는 xml의 TextView의 id
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(
listView.getContext(), //ListView의 context
R.layout.list_fruit, //ListView의 Custom layout
mCursor, //Item으로 사용할 DB의 Cursor
from, //DB 필드 이름
to //DB필드에 대응되는 xml TextView의 id
);
listView.setAdapter(adapter); //어댑터 등록
db.close(); //DB를 닫음.
//Cursor는 닫으면 안된다. Cursor 닫으면 리스트에 항목들 안뜬다. Cursor는 onStop()에서 닫도록 한다.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Cursor c = (Cursor)adapter.getItem(position);
String note = c.getString(3); //note는 3번임.(4번째 필드)
Toast.makeText(getApplicationContext(), note, Toast.LENGTH_LONG).show();
}
});List의 UI를 Custom으로 만든 xml은 아래와 같다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/lf_tv_name"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/lf_tv_num"
android:layout_width="40dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/lf_tv_name"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/lf_tv_num"
android:layout_width="40dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
위처럼 해주면 리스트의 한 항목이
TextView TextView
이렇게 구성 된다. 맘대로 바꿔줄 수 있으니 맘대로 바꿔도 된다.
자세한건 예제 소스를 참고 하기 바람!!
'Development > Mobile' 카테고리의 다른 글
| [Android] 에뮬레이터에서 한글 키보드 사용하기 (0) | 2011.01.20 |
|---|---|
| [Android] LogCat에서 한글 나오게 만들기 (0) | 2011.01.20 |
| [Android] 내부 소스 참고 (0) | 2011.01.20 |
| [Android] 가로 세로 전환 시 onCreate/onDestroy 막기 (0) | 2011.01.20 |
| [Android] 갤럭시 탭용 어플 개발 시 화면에 꽉 채우게 하기 (0) | 2010.12.30 |
| [Android] 내부 저장소 사용하기(Data Storage) (0) | 2010.12.29 |
| [Android] DB - SQLite (2) | 2010.12.28 |
| [Android] SQLite3 기본 (0) | 2010.12.24 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Visual C++
- database
- algorithm
- db
- API
- C
- android
- source
- 리눅스
- gcc
- jni강좌
- NDK
- driver
- jni
- Troubleshooting
- Quiz
- MFC
- 드라이버
- kering
- Cloud
- C++
- AWS
- 프로그래밍
- linux
- it
- java
- 안드로이드
- 음악
- winapi
- Python
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함
CustomListViewSample.zip