2017-10-14 73 views
-1

顯示的SQLite數據我有一個在src/main/assets稱爲books.sqlpro與列idtitleauthorbody數據庫。不能在ListView中

我想按字母順序與數據庫中的所有作者一起填充ListView。

我並不感到驚訝它沒有工作,因爲我沒有指定我的數據庫位置,但它給出的唯一錯誤是Cannot resolve method 'getReadableDatabase()'

這裏是我的代碼:

package com.example.apple.bookshelf; 

import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class MainActivity extends AppCompatActivity { 

    private ListView mainListView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mainListView = (ListView) findViewById(R.id.mainListView); 

     String Table_Name = "books"; 
     String selectQuery = "SELECT * FROM " + Table_Name; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     cursor.moveToFirst(); 
     String[] NameArray = new String[cursor.count()]; 

     ArrayAdapter<String> arrayAdapter = 
       new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, NameArray); 

     mainListView.setAdapter(arrayAdapter); 

    } 

} 

預先感謝您的幫助!

回答

0

使用this.getReadableDatabase()會發現不起作用,因爲它是您需要子類的SQLiteOpenHelper類的一種方法。

以下是獲取作者列表的非常粗糙的方法。基本上它將資產文件中的數據庫複製到標準數據庫位置(如果它尚不存在)。獲取所有行的遊標,然後將它們顯示在ListView中。

final String DBNAME = "books.sqlpro"; 
    final String DBPATH = this.getDatabasePath(DBNAME).getPath(); 
    final String TBNAME = "books"; 
    final String AUTHOR_COL = "author"; 
    String outfilename = DBPATH + DBNAME; 
    SQLiteDatabase booksdb; 

    try { 
     booksdb = SQLiteDatabase.openDatabase(outfilename, 
      null, 
      SQLiteDatabase.OPEN_READONLY 
     ); 
    } catch (Exception e) { 
     try { 
      InputStream is = this.getAssets().open(DBNAME); 
      OutputStream os = new FileOutputStream(outfilename); 

      byte[] buffer = new byte[32768]; 
      int length; 
      while ((length = is.read(buffer)) > 0) { 
       os.write(buffer); 
      } 
      is.close(); 
      os.flush(); 
      os.close(); 

     } catch (IOException io) { 
      io.printStackTrace(); 
     } 
     try { 
      booksdb = SQLiteDatabase.openDatabase(outfilename, 
       null, 
       SQLiteDatabase.OPEN_READONLY 
      ); 
     } catch (Exception e2) { 
      e2.printStackTrace(); 
     } 
    } 
    Cursor csr = booksdb.query(TBNAME,null,null,null,null,null,null); 
    String[] authorlist = new String[csr.getCount()]; 
    while (csr.moveToNext()) { 
     authorlist[csr.getPosition()] = csr.getString(csr.getColumnIndex(AUTHOR_COL)); 
    } 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, 
     authorlist 
    ); 
    mListView1.setAdapter(adapter); 

由此看來,SQLite的管理器中創建和複製到項目的資產文件夾books.sqlpro數據庫: -

enter image description here

對此,與列表視圖作者(注意劫持了另一個項目,因此其他名單): -

enter image description here

相關問題