簡體   English   中英

如何在ListView Android中從SQLCipher數據庫加載數據

[英]How to load data from SQLCipher Database in ListView Android

我在將表從SQLCipher數據庫顯示到ListView時遇到麻煩。

目前,我的程序僅允許用戶向數據庫添加“秘密”。 當我的數據庫是SQLite時,我能夠查看數據,但是轉到SQLCipher之后,我遇到了一些錯誤。

這是我遇到的一些錯誤:

java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.matc.secretkeeper/edu.matc.secretkeeper.ListData}: java.lang.IllegalStateException: get field slot from row 0 col 1 failed

和這個:

Finalizing a Cursor that has not been deactivated or closed. database = /data/user/0/edu.matc.secretkeeper/databases/secrets1.db, table = null, query = SELECT * FROM secretTable net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

這是我的代碼以幫助進行故障排除。

這是我的ListData.java類:

public class ListData extends AppCompatActivity {

private static final String TAG = "activity_list_data" ;
MyDBHandler myDB;
private ListView myListView;
Button refreshList, goHome;

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

    myListView = (ListView) findViewById(R.id.listView);
    myDB = new MyDBHandler(this);

    populateListView();

    refreshList = (Button) findViewById(R.id.refreshListBtn); //refresh button
    refreshList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(ListData.this,
                    ListData.class);
            startActivity(myIntent);
        }
    });

    goHome = (Button) findViewById(R.id.homeBtn); //gohome button
    goHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(ListData.this,
                    MainActivity.class);
            startActivity(myIntent);
        }
    });


}

//this doesnt seem to be populating the list view
private void populateListView() {
    Log.d(TAG, "populateListView: Displaying data in the ListView.");

    //get the data and append to a list
    Cursor data = myDB.getData();
    ArrayList<String> listData = new ArrayList<>();
    while(data.moveToNext()){
        //get the value from the database in column 1
        //then add it to the ArrayList
        listData.add(data.getString(1));


        data.close(); //close cursor
    }

    Log.d(TAG, "array list while loop completed");


    //create the list adapter and set the adapter
    ListAdapter adapter = new ArrayAdapter<>(this, 
 android.R.layout.simple_list_item_1, listData);
    myListView.setAdapter(adapter);

    //set an onItemClickListener to the ListView
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int 
i, long l) {
            String secret = adapterView.getItemAtPosition(i).toString();
            Log.d(TAG, "onItemClick: You Clicked on " + secret);

            Cursor data = myDB.getItemID(secret); //get the id associated 
with that secret
            int itemID = -1;
            while(data.moveToNext()){
                itemID = data.getInt(0);
            }
            if(itemID > -1){
                Log.d(TAG, "onItemClick: The ID is: " + itemID);
                Intent editScreenIntent = new Intent(ListData.this, 
EditData.class);
                editScreenIntent.putExtra("id",itemID);
                editScreenIntent.putExtra("secret",secret);
                startActivity(editScreenIntent);
            }
            else{
                toastMessage("No ID associated with that secret");
            }
        }
    });
}

private void toastMessage(String message){
    Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}


}

我在MyDBHelper類中的getData方法:

 public Cursor getData(){ 
    SQLiteDatabase db = this.getWritableDatabase("temp123");
    String query = "SELECT * FROM " + TABLE_SECRET;
    Cursor data = db.rawQuery(query,null);
    Log.d("Cursor Object", DatabaseUtils.dumpCursorToString(data));
    db.close();
    return data;

}

請讓我知道是否需要添加任何細節以繪制出更好的圖片。

溫暖的問候,

Ĵ

在我的populateListView()方法中,我關閉了while循環內的光標。 在將data.close() while循環之外之后,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM