마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드에서 디비를 생성하여 listview로 출력

0 추천
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 
&nbsp; public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
&nbsp;&nbsp;&nbsp; cursor.moveToPosition(position); 
&nbsp;&nbsp;&nbsp; String str = cursor.getString(cursor.getColumnIndex("bankname")); 
&nbsp;&nbsp;&nbsp; Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show(); 
&nbsp; } 
&nbsp; });&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; } 
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; private void selectDB(){ 
&nbsp;&nbsp;&nbsp; db = dbHelper.getWritableDatabase(); 
&nbsp;&nbsp;&nbsp; sql = "SELECT * FROM t1;"; 
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; cursor = db.rawQuery(sql, null); 
&nbsp;&nbsp;&nbsp; if(cursor.getCount() > 0){ 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; startManagingCursor(cursor); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DBAdapter dbAdapter = new DBAdapter(this, cursor); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list.setAdapter(dbAdapter); 
&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; } 
&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
} 
/////////////////////////////////////////////////////////////////////////////////// 
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 { 
&nbsp;String sql; 
&nbsp; 
&nbsp;public DBHelper(Context context, String name, CursorFactory factory, int version) { 
&nbsp; super(context, name, factory, version); 
} 

&nbsp;@Override 
&nbsp;public void onCreate(SQLiteDatabase db) { 
String sql = "create table t1" + 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "(id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "bankname text, " + 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "name text, " + 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "num double,"+ 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "kind integer);"; 
&nbsp;&nbsp;&nbsp; db.execSQL(sql); 
&nbsp; 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL,'대구)유니온저축은행','신용부금',4.10,1);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL,'대구)MS저축은행','정기적금',4.00,1);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL,'대구)참저축은행','정기적금',3.90,1);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL, '대구)드림저축은행','정기적금',3.80,1);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL, '대구)MS저축은행','신용부금',3.80,1);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL, '대구)대백저축은행','정기적금',3.50,1);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL, '대구은행평생저축 - 꿈나무형','우대금리',2.65,0);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL, '대구은행파랑새적금-만기지급식','우대금리',2.60,0);"); 
&nbsp; db.execSQL("INSERT INTO TEST VALUES(NULL, '대구은행평생저축-일반형','부가혜택',2.30,0);"); 
&nbsp;} 
&nbsp; 
&nbsp;@Override 
&nbsp;public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
&nbsp; //버전이 업그레이드 됐을 경우 작업할 내용을 작성합니다. 
&nbsp;} 
} 


///////////////////////////////////////////////////////////////// 
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 { 
&nbsp; 
&nbsp; 
&nbsp;public DBAdapter(Context context, Cursor c) { 
&nbsp; super(context, c); 
&nbsp;} 
&nbsp;@Override 
&nbsp;public void bindView(View view, Context context, Cursor cursor) { 
&nbsp; 
&nbsp; final ImageView image = (ImageView)view.findViewById(R.id.image); 
&nbsp; final TextView bankname = (TextView)view.findViewById(R.id.bankname); 
&nbsp; final TextView name = (TextView)view.findViewById(R.id.name); 
&nbsp; final TextView num = (TextView)view.findViewById(R.id.num); 
&nbsp; final TextView kind = (TextView)view.findViewById(R.id.kind); 
&nbsp; 
&nbsp; image.setImageResource(R.drawable.ic_launcher); 
&nbsp; bankname.setText("금융기관명 : "+cursor.getString(cursor.getColumnIndex("bankname"))); 
&nbsp; name.setText("상품명 : "+cursor.getString(cursor.getColumnIndex("name"))); 
&nbsp; num.setText("세전금리 : "+cursor.getString(cursor.getColumnIndex("num"))); 
&nbsp; kind.setText("금융기관종류 : "+cursor.getString(cursor.getColumnIndex("kind"))); 
&nbsp; 
&nbsp;} 
&nbsp;@Override 
&nbsp;public View newView(Context context, Cursor cursor, ViewGroup parent) {&nbsp; 
&nbsp; LayoutInflater inflater = LayoutInflater.from(context); 
&nbsp; View v = inflater.inflate(R.layout.listlayout, parent, false); 
&nbsp; return v; 
&nbsp;} 
} 

안녕하세요 이클립스에서 디비를 생성하여 listview를 통해 화면에 출력을 하고싶습니다

그런데 실행을 하면 자꾸 오류가 납니다

자바 소스는 밑에 적겠습니다 많은 도움 부탁드립니다

 

pjch29 (120 포인트) 님이 2014년 5월 27일 질문

1개의 답변

0 추천
이럴때는 소스를 올리는게 아니고 오류 로그를 올리세요
aucd29 (218,390 포인트) 님이 2014년 5월 28일 답변
...