从填充Android的SQLite数据库字符串,然后列表视图 [英] Populating a string and then list view from Android SQLite Database

查看:96
本文介绍了从填充Android的SQLite数据库字符串,然后列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我想要做的就是填充的ListView 从我的SQLite 数据库。
我知道如何使用数组来做到这一点。有人可以帮我做这个?
我重视用这个需要查询类的一些修改我的的SQLite 库。
我所寻找的完整的图书馆和类code的查询和数据库的构造函数。
我也是在寻找code。通过查询生成的ListView 活动我自己的数据库中所提到的code:

 包com.sqlite.www;进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;进口android.content.Context;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteException;
进口android.database.sqlite.SQLiteOpenHelper;公共类DataBaseHelper扩展SQLiteOpenHelper {    //你的应用程序的数据库而Android的默认系统路径。
    私人静态字符串DB_PATH =/data/data/com.sqlite.www/databases/;
    私人静态字符串DB_NAME =TheProjectDatabase.sqlite;
    私人SQLiteDatabase MYDATABASE;
    私人最终上下文myContext;    / **
     *构造
     *注意到并保持以访问该应用程序的资产和资源所传递的上下文的引用。
     * @参数方面
     * /
     公共DataBaseHelper(上下文的背景下){        超级(上下文,DB_NAME,空,1);
        this.myContext =背景;
    }    / **
     *在系统上创建一个空数据库,并与自己的数据库重写它。
    * * /
    公共无效的CreateDatabase()抛出IOException        布尔dbExist = checkDataBase();        如果(dbExist){
            //什么也不做 - 已存在于数据库
        }其他{
            //通过调用此方法与空的数据库将被创建到默认的系统路径
            //你的应用程序,所以我们要能够覆盖该数据库与我们的数据库。
            this.getReadableDatabase();
            尝试{
                copyDataBase();
            }赶上(IOException异常五){
                抛出新的错误(错误复制数据库);
            }
       }
  }  / **
   *检查是否已存在于数据库,以避免重新复制每次打开应用程序时的文件。
   *如果存在返回:真的,假的,如果它不
   * /
   私人布尔checkDataBase(){       SQLiteDatabase CHECKDB = NULL;       尝试{
           字符串mypath中= DB_PATH + DB_NAME;
           CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
       }赶上(SQLiteException E){
        //数据库开不存在。
       }       如果(CHECKDB!= NULL){
            checkDB.close();
       }       返回CHECKDB!= NULL?真假;
   }   / **
    *副本数据库从本地资产文件夹在刚才创建的空数据库
    *系统文件夹,从那里可以访问和处理。
    *这是通过转流的字节流进行。
    * * /
    私人无效copyDataBase()抛出IOException        //打开本地数据库的输入流
        InputStream的myInput = myContext.getAssets()开(DB_NAME)。        //路径刚刚创建的空分贝
        字符串outFileName = DB_PATH + DB_NAME;        //打开空分贝的输出流
        的OutputStream myOutput =新的FileOutputStream(outFileName);        //传递从inputfile中字节到OUTPUTFILE
        字节[]缓冲区=新的字节[1024];
        INT长;
        而((长度= myInput.read(缓冲液))大于0){
            myOutput.write(缓冲液,0,长度);
        }        //关闭流
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }    公共无效的openDatabase()抛出的SQLException {        //打开数据库
        字符串mypath中= DB_PATH + DB_NAME;
        MYDATABASE = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
    }    @覆盖
    公共同步无效的close(){
        如果(MYDATABASE!= NULL)
            myDataBase.close();
        super.close();
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){    }    //添加您的公开辅助方法访问并从数据库中获取的内容。
   //你可以做返回游标返回myDataBase.query(....),因此这将会是很容易
   //你创建你的意见适配器。
}

主要的例子:

 公共类SQLiteActivity延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        DataBaseHelper myDbHelper;
        myDbHelper =新DataBaseHelper(本);        尝试{
            myDbHelper.createDataBase();
        }赶上(IOException异常IOE){
            抛出新的错误(无法创建数据库);
        }
        尝试{
            myDbHelper.openDataBase();
        }赶上(的SQLException SQLE){
            扔SQLE;
        }
    }
}


解决方案

SimpeCursorAdapter 可能是你正在寻找德适配器。

基于注释更新(如何查询数据)

 私人光标queryData(){
    返回myDataBase.query(
        tableName值
        新的String [] {清单,中,colums,来,选择},
        COLUMNNAME =?,//你的WHERE条件
        新的String [] {你?repleacements},
        NULL,//没有GROUP BY
        NULL,//没有有
        空值); //订单由
}

I'm new to android. What I'm trying to do is to populate a ListView from my SQlite database. I know how to do it using arrays. Can somebody help me doing this? I have attached my SQLite library used this need some modification for the query class. What I am looking for the complete library and class code for the query and the constructor of the databases. Also I am looking for the code to generate the ListView Activity by query my own database as mentioned in the code:

package com.sqlite.www;

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

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

public class DataBaseHelper extends SQLiteOpenHelper {

    //The Android s default system path of your application database.
    private static String DB_PATH = "/data/data/com.sqlite.www/databases/";
    private static String DB_NAME = "TheProjectDatabase.sqlite";
    private SQLiteDatabase myDataBase; 
    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
     public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }   

    /**
     * Creates a empty database on the system and rewrites it with your own database.
    * */
    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist){
            //do nothing - database already exist
        }else{
            //By calling this method and empty database will be created into the default system path
            //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
       }
  }

  /**
   * Check if the database already exist to avoid re-copying the file each time you open the application.
   * @return true if it exists, false if it doesn't
   */
   private boolean checkDataBase(){

       SQLiteDatabase checkDB = null;

       try{
           String myPath = DB_PATH + DB_NAME;
           checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
       }catch(SQLiteException e){
        //database does't exist yet.
       }

       if(checkDB != null){
            checkDB.close();
       }

       return checkDB != null ? true : false;
   }

   /**
    * Copies your database from your local assets-folder to the just created empty database in the
    * system folder, from where it can be accessed and handled.
    * This is done by transfering bytestream.
    * */
    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public void openDataBase() throws SQLException{

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

    @Override
    public synchronized void close() {
        if(myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    // Add your public helper methods to access and get content from the database.
   // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
   // to you to create adapters for your views.
}

MAIN for an example:

public class SQLiteActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DataBaseHelper myDbHelper ;
        myDbHelper = new DataBaseHelper(this);

        try {
            myDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            myDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }  
    }
}

解决方案

SimpeCursorAdapter might be de adapter which you are looking for.

Update based on comment (how to query data)

private Cursor queryData() {
    return myDataBase.query(
        "tableName",
        new String[] {"list", "of", "colums", "to", "select"},
        "columnName = ?", // your where condition
        new String[] {"your ? repleacements"},
        null, // no group by
        null, // no having
        null); // on order by
}

这篇关于从填充Android的SQLite数据库字符串,然后列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆