package com.example.test;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity {
ListView list;
DBHelper dbHelper;
SQLiteDatabase db;
String sql;
Cursor cursor;
final static String dbName = "test2.db";
final static int dbVersion =2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.list);
dbHelper = new DBHelper(this, dbName, null, dbVersion);
selectDB();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
cursor.moveToPosition(position);
String str = cursor.getString(cursor.getColumnIndex("bankname"));
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
});
}
private void selectDB(){
db = dbHelper.getWritableDatabase();
sql = "SELECT * FROM t1;";
cursor = db.rawQuery(sql, null);
if(cursor.getCount() > 0){
startManagingCursor(cursor);
DBAdapter dbAdapter = new DBAdapter(this, cursor);
list.setAdapter(dbAdapter);
}
}
}
///////////////////////////////////////////////////////////////////////////////////
package com.example.test;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
String sql;
public DBHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table t1" +
"(id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"bankname text, " +
"name text, " +
"num double,"+
"kind integer);";
db.execSQL(sql);
db.execSQL("INSERT INTO TEST VALUES(NULL,'대구)유니온저축은행','신용부금',4.10,1);");
db.execSQL("INSERT INTO TEST VALUES(NULL,'대구)MS저축은행','정기적금',4.00,1);");
db.execSQL("INSERT INTO TEST VALUES(NULL,'대구)참저축은행','정기적금',3.90,1);");
db.execSQL("INSERT INTO TEST VALUES(NULL, '대구)드림저축은행','정기적금',3.80,1);");
db.execSQL("INSERT INTO TEST VALUES(NULL, '대구)MS저축은행','신용부금',3.80,1);");
db.execSQL("INSERT INTO TEST VALUES(NULL, '대구)대백저축은행','정기적금',3.50,1);");
db.execSQL("INSERT INTO TEST VALUES(NULL, '대구은행평생저축 - 꿈나무형','우대금리',2.65,0);");
db.execSQL("INSERT INTO TEST VALUES(NULL, '대구은행파랑새적금-만기지급식','우대금리',2.60,0);");
db.execSQL("INSERT INTO TEST VALUES(NULL, '대구은행평생저축-일반형','부가혜택',2.30,0);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//버전이 업그레이드 됐을 경우 작업할 내용을 작성합니다.
}
}
/////////////////////////////////////////////////////////////////
package com.example.test;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class DBAdapter extends CursorAdapter {
public DBAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ImageView image = (ImageView)view.findViewById(R.id.image);
final TextView bankname = (TextView)view.findViewById(R.id.bankname);
final TextView name = (TextView)view.findViewById(R.id.name);
final TextView num = (TextView)view.findViewById(R.id.num);
final TextView kind = (TextView)view.findViewById(R.id.kind);
image.setImageResource(R.drawable.ic_launcher);
bankname.setText("금융기관명 : "+cursor.getString(cursor.getColumnIndex("bankname")));
name.setText("상품명 : "+cursor.getString(cursor.getColumnIndex("name")));
num.setText("세전금리 : "+cursor.getString(cursor.getColumnIndex("num")));
kind.setText("금융기관종류 : "+cursor.getString(cursor.getColumnIndex("kind")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listlayout, parent, false);
return v;
}
}
안녕하세요 이클립스에서 디비를 생성하여 listview를 통해 화면에 출력을 하고싶습니다
그런데 실행을 하면 자꾸 오류가 납니다
자바 소스는 밑에 적겠습니다 많은 도움 부탁드립니다