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

쌩초보가 질문합니다 도와주세요 ㅠㅠ

0 추천

이클립스 만진지 얼마 안 됐습니다 ㅠ

물론 자바도 생판 처음 만지는거라 답답하네요...

 

간단히 사지선다형 퀴즈를 구현하고 싶은데요

미리 작성해 assets 폴더에 박아둔 데이터베이스에서 임의로 영어단어 하나를 끄집어내서 보이게 하고,

그 아랫쪽에 데이터베이스에서 읽어온 단어 뜻이 적힌 버튼/이미지버튼/혹은 그냥 이미지(onTouchEvent로 어찌 할 수 있는지 방법 좀... 흐흑) 네개를 배치하고

정답을 고르면 큰 동그라미 표시가 나타나고, 오답이면 큰 가위표가 나타나게 하려고 합니다.

 

이렇게 큰 판을 짜놨는데 실상은 db에 있는 단어들 읽어서 랜덤으로 쏘는 것에 허덕이고 있습니다...

데이터베이스 구현도 경로를 하드코딩하지 말라느니 뭐하느니 그러고 있으니...

염치없지만 고수 분들이 초보 하나 키우시는 셈 치고 도와주세요 ㅠㅠㅠㅠ

 

코드는 대략 이 정도밖에...

 

package com.again.studyenglish1;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
 
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.widget.TextView;
 
public class Activity_Three extends Activity {
     
    private static String DB_PATH = "/sdcard/";
    private static String DB_NAME = "dictionary.sqlite";
    private int listcount = 0;
    private String[] wordList = null;
    private String[] definitionList = null;
     
    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("Error copying the Database");
            }
        }
    }
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wordtest);
        }
    TextView word = (TextView) findViewById(R.id.textView1);
     
}

 

저 뻘짓한걸까요...?

해츨링 (290 포인트) 님이 2013년 6월 13일 질문

1개의 답변

0 추천

제가 만든 메서드 입니다.

public String[] randomStringArray(String[] words, int random_count) {
    if (words == null || words.length < random_count)
        return null;
    String[] selected = new String[random_count];
    int selected_count = 0;
    Random r = new Random(new Date().getTime());
 
    boolean is_not_equal = true;
    while (selected_count < random_count) {
        int random_idx = Math.abs(r.nextInt() % words.length);
        String word = words[random_idx];
        for (int i = 0; i < selected_count; i++) {
            if (selected[i].equals(word)) {
                is_not_equal = false;
                break;
            } else {
                is_not_equal = true;
            }
        }
        if (is_not_equal) {
            selected[selected_count] = word;
            selected_count++;
        }
    }
 
    return selected;
}

 

메서드 사용 방법 입니다.

String[] words = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
 
// randomStringArray() 의
// 첫번째 인자는  랜덤 문자열을 뽑아올 원본 문자열 배열이고요
// 두번재는 몇개 가져올것인가? 에 대한 갯수 입니다.
// 리턴값으로 중복되지 않는 랜덤한 문자열 배열을 받아오니 이걸 사용하시면 됩니다.
String[] ran_words = randomStringArray(words, 5);
 
if (ran_words != null) {
    for (int i = 0; i < ran_words.length; i++) {
        System.out.println(ran_words[i]);
    }
}

 

Frog (3,910 포인트) 님이 2013년 6월 14일 답변
Frog님이 2013년 6월 14일 수정
저기, 죄송하지만... db에서 무작위로 값을 뽑아 뿌려주는 걸 원했는데..
조금만 더 자세히 가르쳐주실수 없을까요?
이건 String 배열에서 뽑아오는건데...
...