진득하니 봐주시면 정말 감사드리겠습니다 ㅠ
package com.again.studyenglish1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class FlashingWords2 extends Activity {
private static String DB_PATH = "/sdcard/";
private static String DB_NAME = "dictionary.sqlite";
private int listcount = 0;
private int currentPos = 0;
private boolean isPlaying = false;
private String[] wordList = null;
private String[] definitionList = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flashingwords2);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
loadData();
Button startButton = (Button)findViewById(R.id.startbtn2);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
isPlaying = true;
}
});
Button stopButton = (Button)findViewById(R.id.stopbtn2);
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
isPlaying = false;
}
});
setWord();
}
public void setWordData()
{
if (isPlaying == true)
{
TextView wordText = (TextView)findViewById(R.id.word2);
TextView definitionText = (TextView)findViewById(R.id.definition2);
wordText.setText(wordList[currentPos]);
definitionText.setText(definitionList[currentPos]);
currentPos++;
if (currentPos >= listcount)
{
currentPos = 0;
}
}
}
public void setWord()
{
setWordData();
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable()
{
@Override
public void run()
{
setWord();
}
}, 1000);
}
private boolean checkDatabase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
catch(SQLiteException e){}
if (checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException
{
InputStream myInput = this.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int total_length = 0;
int length;
while ((length = myInput.read(buffer))>0);
{
myOutput.write(buffer, 0, length);
total_length += length;
}
total_length += length;
myOutput.flush();
myOutput.close();
myInput.close();
}
public void createDatabase() throws IOException
{
boolean dbExist = checkDatabase();
if (dbExist){}
else
{
try
{
copyDatabase();
}
catch (IOException e)
{
throw new Error("DB를 복사하지 못했습니다!");
}
}
}
public void loadData()
{
try
{
createDatabase();
}
catch (IOException e)
{
Toast.makeText(this, "DB 파일을 생성할 수 없습니다.", Toast.LENGTH_LONG).show();
}
listcount = 0;
try
{
Cursor cursor;
SQLiteDatabase db =
SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, 1);
String[] FROM = {"*"};
cursor = db.query("vocabulary2", FROM, null, null, null, null, null);
startManagingCursor(cursor);
while (cursor.moveToNext())
{
wordList[listcount] = cursor.getString(0);
definitionList[listcount] = cursor.getString(1);
listcount++;
}
if (db != null)
db.close();
}
catch (Exception e)
{
Toast.makeText(this, "에러 : " + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
이건 단어 암기 기능을 구현한 건데요.
DB에 저장되어있는 단어와 그 뜻을 TextView에다가 뿌려주고. 1초마다 다음 단어로 넘어가게 하고, 시작과 중지를 할 수 있게 구현하려고 한 겁니다.
사실 이 액티비티에만 이 기능이 구현된게 아니라 이 전의 액티비티 한 개에도 이 기능이 구현되어 있는데요.
그건 no such table 따위 뜨지 않고 잘만 돌아갑니다.
두번째 것인 이것만 계속 오류메세지를 뱉어내고 있네요 ㅠㅠ
앞으로 몇 개를 똑같이 더 해야 하는데 이거 큰일났습니다 흑...
로그캣에 찍힌 문장은 정확히 이렇습니다:
(1) no such table: vocabulary2 (//1인지 l인지 확실치는 않지만...)
아무리 db를 다시 들여봐도 맞춤법도 틀린 것 하나 없고, 제대로 assets 파일에 들어가 있고, 무엇보다 이전에 구현해놓은 액티비티에서는 잘만 돌아간다는 사실이 저를 환장하게 만드는군요.
혹시 이런 류의 코딩을 할때 데이터베이스에 복수 테이블을 둬서 가져오는 게 불가능한 건가요?
그렇다고 한다면 저걸 어떻게 때려잡아야 하나요?
제발 초보 좀 살려주세요 ㅠㅠㅠ
(아, 혹시 심심하신 분 계시면 CursorLoader 클래스 쓰는 법 좀 가르쳐주세요... startManagingCursor는 deprecated 됐더군요... 옛날 책 보니 이런 단점이...)