SQLite返回String的Textview的setText

时间:2013-04-30 08:23:43

标签: android android-sqlite textview

我有这个DataBaseCreator Java FIle。

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseCreator extends SQLiteOpenHelper {
    private static String DB_PATH = "/GetInspiredAndHaveFun/assets/Jokes.db";
    private static String DB_NAME = "Jokes.db";
    private static int DB_VERSION = 1;
    public static String DATABASE_TABLE = "myjoke";
    public String temp = "";

    private SQLiteDatabase myDataBase;
    private final Context myContext;

    public DataBaseCreator(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;

    }

    public void openDatabase() throws IOException {
        boolean dbe = checkDataBase();
        if (dbe) {
            Log.i("Tag", "dbe" + dbe);

        } else {
            Log.i("Tag", "dbdoesnotexist" + dbe);
            this.getReadableDatabase();
            copyDataBase();

        }

    }

    public void copyDataBase() throws IOException {
        InputStream inp = myContext.getAssets().open(DB_NAME);
        String ofn = DB_PATH + DB_NAME;
        OutputStream oup = new FileOutputStream(ofn);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inp.read(buffer)) > 0) {
            oup.write(buffer, 0, length);
        }

        oup.flush();
        inp.close();
        oup.close();

    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;

            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        }

        catch (SQLiteException e) {

        }

        return checkDB != null ? true : false;
    }

    public void openDataBase() throws SQLException {

        // Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
        passText();

    }

    public final String passText() {

        Cursor cursor = myDataBase.query(DATABASE_TABLE, null, null, null,
                null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            temp += cursor.getString(1);
            cursor.moveToNext();

        }
        return temp;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

我想要的是将我的Jokes活动中的textview文本设置为passText9方法返回的字符串。但是,我收到了一个错误。 “temp”无法解析为变量。

主档

import java.io.IOException;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

    {

        DataBaseCreator myDbHelper = new DataBaseCreator(this);

        try{
            myDbHelper.copyDataBase();
        } 
        catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        myDbHelper.openDataBase();
        //here.
     t1.setText(temp);


    }
    }

1 个答案:

答案 0 :(得分:1)

temp是DataHelper实例的成员:

t1.setText(myDbHelper.temp);