SimpleCursorAdaptor와 ListView의 사용예
Activity를 MyList.java로 만들고 아래 코드들을 추가
DB는 단순하게 사용하였음


### xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView android:id="@+id/IndexList"
        android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</LinearLayout>

main.xml

### java

package com.yesum.listDBSample;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MyList extends Activity {
    private static final String DATABASE_NAME = "sample.db";
    public static final String TABLE_NAME = "mytable";
    private static final String COLUMN_KEY_ID = "_id";
    private static final String COLUMN_NAME = "name";

    private ListView m_listIndex;
    private SQLiteDatabase mDatabase;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_listIndex = (ListView) this.findViewById(R.id.IndexList);
        m_listIndex.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
            }
        });
        //createDB();
        //insert("ryuan");
        //insert("yesum");
        //insert("dongsik");
        setAdaptor();
    }

    private long insert(String name) {
        if (mDatabase == null) {
            mDatabase = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        }
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_NAME, name);
        return mDatabase.insert(TABLE_NAME, null, cv);
    }

    private void createDB() {
        // create sample DB
        if (mDatabase == null) {
            mDatabase = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        }
        String createSQL = "CREATE TABLE " + TABLE_NAME + " (_id integer primary key, " +
            "name)";
        mDatabase.execSQL(createSQL);

    }

    private void setAdaptor() {
        if (mDatabase == null) {
            mDatabase = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        }
        Cursor cursor = null;
        final CursorAdapter adaptor;
        String[] columns = new String[] {COLUMN_KEY_ID, COLUMN_NAME};

        cursor = mDatabase.query(TABLE_NAME, columns, null, null, null, null, null);
        adaptor = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,
                cursor,
                new String[] {COLUMN_NAME},
                new int[] {android.R.id.text1}
                );
       
        m_listIndex.setAdapter(adaptor);
    }
}

MyList.java

Posted by 소혼

티스토리 툴바