I am having some trouble displaying a table, from an SQLCipher database, into a ListView.
My program, right now, only lets users add "secrets" to a database. I was able to view the data when my database was SQLite, but after moving to SQLCipher I get some errors.
Here are some errors I am getting:
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
and this:
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
Here is my code to help troubleshoot.
Here is my ListData.java class:
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();
}
}
my getData method inside MyDBHelper class:
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;
}
Please let me know if there are any details I need to add to paint a better picture.
Warm regards,
J
In my populateListView()
method, I closed the cursor inside of the while
loop. After moving the data.close()
outside the while loop everything worked as normal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.