現在、アプリ内で、データベースから ListView を更新するメソッドを呼び出すと、古いオブジェクトの例外が発生します。
データベースにクエリを実行するときにすべてのカーソルを閉じるように注意しました。「startManagingCursor」を使用して返されたときにカーソルも管理しますが、それでもこの問題が発生します。
現時点では、データベースに新しい項目を追加するときに、クラスのプライベート メソッド内で ListView 更新メソッドを呼び出します。これは、オブジェクトが例外をスローしている場所です。
セッションを「更新」している人々について読んだことがありますが、データベースのバージョンがこれに含まれると思いますか? しかし、全体的に私は立ち往生しており、これを解決できません。
更新されたエラー:
02-13 13:23:06.588: E/AndroidRuntime(287): 致命的な例外: メイン 02-13 13:23:06.588: E/AndroidRuntime(287): android.database.StaleDataException: 閉じたカーソルにアクセスする 02-13 13: 23:06.588: E/AndroidRuntime(287): android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217) 02-13 13:23:06.588: E/AndroidRuntime(287): android.database.AbstractWindowedCursor.getInt で(AbstractWindowedCursor.java:84) 02-13 13:23:06.588: E/AndroidRuntime(287): com.example.flybase2.ShoppingList.updateTotal(ShoppingList.java:248) 02-13 13:23:06.588: E /AndroidRuntime(287): com.example.flybase2.ShoppingList.access$1(ShoppingList.java:231) 02-13 13:23:06.588: E/AndroidRuntime(287): com.example.flybase2.ShoppingList$2. onClick(ShoppingList.java:350) 02-13 13:23:06.588: E/AndroidRuntime(287): com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 02-13 13:23:06.588: E/AndroidRuntime(287): android.os.Handler.dispatchMessage(Handler.java:99) 02-13 13:23:06.588: E/AndroidRuntime(287): android.os.Looper.loop(Looper.java:123) 02-13 13:23:06.588: E/AndroidRuntime(287): android.app.ActivityThread で.main(ActivityThread.java:4627) 02-13 13:23:06.588: E/AndroidRuntime(287): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 02-13 13:23:06.588: E/ AndroidRuntime(287): java.lang.reflect.Method.invoke(Method.java:521) 02-13 13:23:06.588: E/AndroidRuntime(287): com.android.internal.os.ZygoteInit$MethodAndArgsCaller で.run(ZygoteInit.java:868) 02-13 13:23:06.588: E/AndroidRuntime(287): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-13 13:23 :06.588:E/AndroidRuntime(287): dalvik.system.NativeStart.main(ネイティブメソッド)
これは、ListView を呼び出す私のクラスです。
package com.example.flybase2;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class ShoppingList extends ListActivity implements OnClickListener {
Button AddItem;
ListView showItems;
SimpleCursorAdapter cursorAdapter;
Long itemId;
EditText totalPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppinglistlayout);
AddItem = (Button) findViewById(R.id.btnAddItem);
showItems = (ListView)findViewById(android.R.id.list);
totalPrice = (EditText)findViewById(R.id.totalListPrice);
AddItem.setOnClickListener(this);
setList();
}
ERROR**********************************8
@Override
public void onClick(View clickedAdd) {
show();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long idd) {
super.onListItemClick(l, v, position, idd);
itemId = idd;
final CharSequence[] items = {"Edit Item", "Delete Item", "Show Most Purchased Item"};
Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingList.this);
alertDialogBuilder.setTitle("Item Options:");
alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Edit Item")) {
AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);
builder.setTitle("Edit Item");
DBHandlerShop setEdit = new DBHandlerShop(ShoppingList.this, null, null);
setEdit.open();
String itemName = setEdit.getItem(itemId);
int itemAmount = setEdit.getItemQuan(itemId);
int itemPrice = setEdit.getItemCost(itemId);
setEdit.close();
LinearLayout layout = new LinearLayout(ShoppingList.this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText titleBox = new EditText(ShoppingList.this);
titleBox.setText(itemName);
titleBox.setHint("Item Name:");
layout.addView(titleBox);
final EditText quantityBox = new EditText(ShoppingList.this);
quantityBox.setText(Integer.toString(itemAmount));
quantityBox.setHint("Item Quantity");
layout.addView(quantityBox);
final EditText priceBox = new EditText(ShoppingList.this);
priceBox.setText(Integer.toString(itemPrice));
priceBox.setHint("Item Price.");
layout.addView(priceBox);
builder.setView(layout);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable valueItem = titleBox.getText();
Editable valueAmount = quantityBox.getText();
Editable valuePrice = priceBox.getText();
String itemDescription = valueItem.toString();
String s = valueAmount.toString();
int itemAmount = Integer.parseInt(s);
String a = valuePrice.toString();
int itemPrice = Integer.parseInt(a);
try{
DBHandlerShop update = new DBHandlerShop(ShoppingList.this, null, null);
update.open();
update.updateItem(itemId, itemDescription, itemAmount, itemPrice);
update.close();
}
catch(Exception e)
{
Dialog e1 = new Dialog(ShoppingList.this);
e1.setTitle("Item unsuccesfully updated");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
}
finally
{
Dialog e1 = new Dialog(ShoppingList.this);
e1.setTitle("Item succesfully updated");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
setList();
int cost = updateTotal();
totalPrice.setText(cost);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.show();
}
else if (items[item].equals("Delete Item"))
{
DBHandlerShop delete = new DBHandlerShop(ShoppingList.this,null, null);
delete.open();
delete.deleteItem(itemId);
delete.close();
setList();
}
else if (items[item].equals("Show Most Purchased Item"))
{
}
}
});
alertDialogBuilder.show();
}
private void setList() {
DBHandlerShop DBShop = new DBHandlerShop(this, null, null);
DBHandlerShop searchItems = new DBHandlerShop(this, null, null);
searchItems.open();
Cursor cursor = searchItems.getItems();
startManagingCursor(cursor);
String [] from = new String [] {DBShop.KEY_ITEMSHOP, DBShop.KEY_ITEMNUM, DBShop.KEY_ITEMPRICE};
int [] to = new int [] {R.id.txtSetItem, R.id.txtSetAmount, R.id.txtSetPrice};
cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist, cursor, from, to);
showItems.setAdapter(cursorAdapter);
}
private int updateTotal() {
DBHandlerShop total = new DBHandlerShop(this, null, null);
int totalPrice = 0;
total.open();
Cursor totalPrices = total.getTotals();
total.close();
if (totalPrices != null) {
startManagingCursor(totalPrices);
if (totalPrices.moveToFirst()) {
do {
int cost = totalPrices.getInt(3);
totalPrice += cost;
} while (totalPrices.moveToNext());
return totalPrice;
}
}
return 0;
}
private void show()
{
AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);
builder.setTitle("Enter Item Details:");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText titleBox = new EditText(this);
titleBox.setHint("Item Name:");
layout.addView(titleBox);
final EditText quantityBox = new EditText(this);
quantityBox.setHint("Item Quantity");
layout.addView(quantityBox);
final EditText priceBox = new EditText(this);
priceBox.setHint("Item Price.");
layout.addView(priceBox);
builder.setView(layout);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try
{
Editable valueItem = titleBox.getText();
Editable valueAmount = quantityBox.getText();
Editable valuePrice = priceBox.getText();
String itemDescription = valueItem.toString();
String s = valueAmount.toString();
int itemAmount = Integer.parseInt(s);
String a = valuePrice.toString();
int itemPrice = Integer.parseInt(a);
DBHandlerShop addItem = new DBHandlerShop(ShoppingList.this, null, null);
addItem.open();
addItem.insertItems(itemDescription, itemAmount, itemPrice);
addItem.close();
}
catch(Exception e)
{
Dialog e1 = new Dialog(ShoppingList.this);
e1.setTitle("Item unsuccesfully added");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e1.setContentView(txt);
e1.show();
}
finally
{
Dialog e = new Dialog(ShoppingList.this);
e.setTitle("Item succesfully added.");
TextView txt = new TextView(ShoppingList.this);
txt.setText("Success");
e.setContentView(txt);
e.show();
setList();
int cost = updateTotal();
totalPrice.setText(cost);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.show();
}
}
これは、データベースから返されたカーソルです。
public Cursor getItems() {
String [] columns = new String[]{KEY_ROWSHOPID, KEY_ITEMSHOP, KEY_ITEMNUM, KEY_ITEMPRICE};
Cursor c = ourDatabase.query(DATABASE_TABLESHOP, columns, null, null, null, null, null);
if(c != null && c.getCount() > 0)
{
c.moveToFirst();
c.close();
return c;
}
c.close();
return null;
}