반응형
커서 어댑터 및 sqlite 예제
안녕하세요 저는 커서 어댑터가 sqlite와 함께 사용되는 샘플 코드를 찾고 있습니까?
정말 간단한 예입니다.
다음은 정말 간단하지만 매우 효과적인 예입니다. 기본 사항을 숙지하면 쉽게 구축 할 수 있습니다.
SQLite 에서 커서 어댑터 를 사용하는 데는 두 가지 주요 부분이 있습니다 .
데이터베이스에서 적절한 커서 를 만듭니다 .
데이터베이스에서 커서 데이터 를 가져 와서 데이터 를 나타낼 보기 와 쌍을 이루는 사용자 지정 커서 어댑터 를 만듭니다 .
1. 데이터베이스에서 적절한 커서를 만듭니다.
활동에서 :
SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper(
context, DATABASE_NAME, null, DATABASE_VERSION);
SQLiteDatabase sqLiteDatabase = sqLiteOpenHelper.getReadableDatabase();
String query = "SELECT * FROM clients ORDER BY company_name ASC"; // No trailing ';'
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
ClientCursorAdapter adapter = new ClientCursorAdapter(
this, R.layout.clients_listview_row, cursor, 0 );
this.setListAdapter(adapter);
2. 사용자 지정 커서 어댑터를 만듭니다.
참고 :에서 확장은 ResourceCursorAdapter
XML을 사용하여보기를 작성한다고 가정합니다.
public class ClientCursorAdapter extends ResourceCursorAdapter {
public ClientCursorAdapter(Context context, int layout, Cursor cursor, int flags) {
super(context, layout, cursor, flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(cursor.getString(cursor.getColumnIndex("name")));
TextView phone = (TextView) view.findViewById(R.id.phone);
phone.setText(cursor.getString(cursor.getColumnIndex("phone")));
}
}
Android에서 sqlite의 원시 쿼리와 함께 커서를 사용하는 방법 :
Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM mytable " +
"where Age > 10 LIMIT 5", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
int age = c.getInt(c.getColumnIndex("Age"));
results.add("" + firstName + ",Age: " + age);
}while (c.moveToNext());
}
}
c.close();
Sqlite를 사용한 CursorAdapter 예제
...
DatabaseHelper helper = new DatabaseHelper(this);
aListView = (ListView) findViewById(R.id.aListView);
Cursor c = helper.getAllContacts();
CustomAdapter adapter = new CustomAdapter(this, c);
aListView.setAdapter(adapter);
...
class CustomAdapter extends CursorAdapter {
// CursorAdapter will handle all the moveToFirst(), getCount() logic for you :)
public CustomAdapter(Context context, Cursor c) {
super(context, c);
}
public void bindView(View view, Context context, Cursor cursor) {
String id = cursor.getString(0);
String name = cursor.getString(1);
// Get all the values
// Use it however you need to
TextView textView = (TextView) view;
textView.setText(name);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Inflate your view here.
TextView view = new TextView(context);
return view;
}
}
private final class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db_name";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_TABLE_TIMELINE = "CREATE TABLE IF NOT EXISTS table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_TIMELINE);
db.execSQL("INSERT INTO ddd (name) VALUES ('One')");
db.execSQL("INSERT INTO ddd (name) VALUES ('Two')");
db.execSQL("INSERT INTO ddd (name) VALUES ('Three')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getAllContacts() {
String selectQuery = "SELECT * FROM table_name;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
}
참고 URL : https://stackoverflow.com/questions/5457699/cursor-adapter-and-sqlite-example
반응형
'Nice programing' 카테고리의 다른 글
부트 스트랩 서버에 com.XXXXX.deviceapp을 등록 할 수 없습니다. (0) | 2020.12.07 |
---|---|
자바 컴파일러에 의한 최적화 (0) | 2020.12.07 |
포트에 바인딩 할 때 SSL 인증서 추가에 실패했습니다. (0) | 2020.12.06 |
Laravel : DB :: transaction ()과 함께 try… catch 사용 (0) | 2020.12.06 |
Django 1.9 지원 중단 경고 app_label (0) | 2020.12.06 |