Updating UI from AsyncTask after exiting and re-entering app - android

I am currently working on an app that runs an AsyncTask in the background and updates the UI with a progressbar. The progress bar works fine when the app is running, however, when the user exits the app and re-enters, the AsyncTask is still running in the background, but the progress bar won't update. It's as if the AsyncTask has detached itself from the activity. Does anyone have any ideas what might be causing this such as general rules involved with AsyncTasks. I can provide code if needed, but it is rather lengthy, so just let me know what parts you would need to see. I should also note that the AsyncTask does complete, I can tell this because it uploads a database to the server when it finishes.
Here is the code:
public class BackgroundAsyncTask extends AsyncTask {
int myProgress;
#Override
protected void onPostExecute(Void result) {
((TextView) findViewById(R.id.tv1)).setText("");
Cursor cur = sql3.query("videohashes", null, null, null, null,
null, null);
cur.moveToFirst();
while (!cur.isAfterLast()) {
Cursor curFrame = sql3.query("table_" + cur.getString(2), null,
null, null, null, null, null);
curFrame.moveToFirst();
((TextView) findViewById(R.id.tv1)).append("\nPath: "
+ cur.getString(1) + "\nHash: " + cur.getString(2)
+ "\nDate:" + cur.getString(3) + "\nSize: "
+ cur.getString(4) + " bytes\nResolution"
+ cur.getString(5) + "\nFormat: " + cur.getString(6)
+ "\nCodec: " + cur.getString(7) + "\nFPS: "
+ cur.getString(8) + "\n\nFirst Frame Info:\nType: "
+ curFrame.getString(1) + "\ncp_num: "
+ curFrame.getString(2) + "\ndp_num: "
+ curFrame.getString(3) + "\npts: "
+ curFrame.getString(4) + "\nqstride: "
+ curFrame.getString(5) + "\nsize: "
+ curFrame.getString(6) + "\nqp_stddev: "
+ curFrame.getString(7) + "\ncount: "
+ curFrame.getString(8) + "\nqp_avg: "
+ curFrame.getString(9) + "\n\n");
cur.moveToNext();
}
cur.close();
((Button) findViewById(R.id.btnSend)).setEnabled(true);
((Button) findViewById(R.id.btnStart)).setEnabled(true);
sql3.close();
sharedPreferences.edit().putString("lastVideoInfo", ((TextView) findViewById(R.id.tv1)).getText().toString()).commit();
sharedPreferences.edit().putBoolean("asyncTaskRunning", false).commit();
dateNow = new Date();
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
totProgress = 0;
currVid = 0;
curProgress = 0;
}
#Override
protected void onProgressUpdate(Integer... values) {
progress.setProgress(values[0]);
}
#Override
protected Void doInBackground(Void... arg0) {
// Calculate total size of all files
for (String path : myFiles) {
totProgress += getFileSize(path);
}
progress.setMax(totProgress);
String strDB3File = getFilesDir().getPath() + "/VideoHashes.db3";
sql3 = SQLiteDatabase.openDatabase(strDB3File, null,
SQLiteDatabase.CREATE_IF_NECESSARY);
try {
String mysql = "CREATE TABLE IF NOT EXISTS videohashes (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, path TEXT NOT NULL, hash TEXT NOT NULL, date TEXT NOT NULL, size INTEGER, resolution TEXT NOT NULL, codec TEXT NOT NULL, format TEXT NOT NULL, fps TEXT NOT NULL)";
sql3.execSQL(mysql);
} catch (SQLiteException e) {
// TODO: handle exception
}
for (String path : myFiles) {
try {
String hash = getMD5Checksum(path);
Cursor curFrame = sql3.query("videohashes",
new String[] { "hash" }, "hash=?",
new String[] { hash }, null, null, null);
if (!curFrame.moveToFirst()) {
ContentValues myInsertData = new ContentValues();
myInsertData.put("path", path);
myInsertData.put("hash", hash);
Date date = new Date();
myInsertData.put("date", dateFormat.format(date));
myInsertData.put("size", getFileSize(path));
naInit(path);
Log.i("VPMA", "After naInit");
int[] prVideoRes = naGetVideoResolution();
myInsertData.put("resolution", prVideoRes[0] + "x"
+ prVideoRes[1]);
String prVideoCodecName = naGetVideoCodecName();
myInsertData.put("codec", prVideoCodecName);
String prVideoFormatName = naGetVideoFormatName();
myInsertData.put("format", prVideoFormatName);
double prFps = naGetVideoFPS();
Log.i("VPMA", "fps: " + prFps);
myInsertData.put("fps", prFps);
Object[] prObjArray = naGetArray();
Log.i("VPMA", (String) prObjArray[0]);
String[] prStrArray = Arrays.copyOf(prObjArray,
prObjArray.length, String[].class);
Log.i("VPMA", "before frames");
try {
String mysql = "CREATE TABLE table_"
+ hash
+ " (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, type TEXT NOT NULL, cp_num TEXT NOT NULL, dp_num TEXT NOT NULL, pts TEXT NOT NULL, qstride TEXT NOT NULL, size TEXT NOT NULL, qp_stddev TEXT NOT NULL, count TEXT NOT NULL, qp_avg TEXT NOT NULL)";
sql3.execSQL(mysql);
} catch (SQLiteException e) {
// TODO: handle exception
}
for (String str : prStrArray) {
ContentValues myFrameInsertData = new ContentValues();
String[] strArr = str.split(",");
if (strArr.length == 9) {
String stddev = "", strCount = "", strQp_avg = "";
double sd, qp_avg, count = 0, sum = 0, sqrSum = 0;
try {
count = Integer.parseInt(strArr[6].trim());
sum = Integer.parseInt(strArr[7].trim());
sqrSum = Integer.parseInt(strArr[8].trim());
//sd = (sum * sum / count);
sd = (sqrSum - (sum*sum/count))/(count-1);//(sqrSum - sd) / (count - 1);
stddev = String.valueOf(sd);
qp_avg = sum / count;
strCount = String.valueOf(count);
strQp_avg = String.valueOf(qp_avg);
} catch (Exception e) {
Log.i("Error: ", "error converting values");
}
//Log.i("Java Code: ", "Sum: " + sum + " SqrSum: " + sqrSum + " Count: " + count);
//Log.i("Java Code: ", "StdDev: " + stddev + " qp_avg: " + strQp_avg);
myFrameInsertData.put("type", strArr[0]);
myFrameInsertData.put("cp_num", strArr[1]);
myFrameInsertData.put("dp_num", strArr[2]);
myFrameInsertData.put("pts", strArr[3]);
myFrameInsertData.put("qstride", strArr[4]);
myFrameInsertData.put("size", strArr[5]);
myFrameInsertData.put("qp_stddev", stddev);
myFrameInsertData.put("count", strCount);
myFrameInsertData.put("qp_avg", strQp_avg);
sql3.insert("table_" + hash, null,
myFrameInsertData);
}
}
sql3.insert("videohashes", null, myInsertData);
naClose();
}
curFrame.close();
currVid++;
curProgress += getFileSize(path);
publishProgress(curProgress);
Log.i("Progress", "CurrVid:" + currVid + " Max:"
+ progress.getMax());
} catch (Exception e) {
Log.i("File", "File not Found");
}
}
return null;
}
}
}
if (sharedPreferences.getBoolean("asyncTaskRunning", false) == false)
{
((Button) findViewById(R.id.btnStart)).setEnabled(false);
progress = (ProgressBar) findViewById(R.id.progressBar1);
text = (TextView) findViewById(R.id.tv1);
if (sharedPreferences.contains("lastVideoInfo"))
{
text.setText("Last Video Information Parsed " + "(" + dateFormat.format(dateNow) + "):\n\n" + sharedPreferences.getString("lastVideoInfo", ""));
((Button) findViewById(R.id.btnSend)).setEnabled(true);
}
else
{
text.setText("");
((Button) findViewById(R.id.btnSend)).setEnabled(false);
}
progress.setProgress(0);
myFiles = new ArrayList<String>();
new StartAsyncTask().execute();
}
}

When the Activity is destroyed, it loses its reference to the AsyncTask, as when the AsyncTask is created it is passed in a reference to the instance of the Activity that creates it. When the instance dies, the reference to the Activity becomes useless.
A better approach would be put the AsyncTask into a Service, set up the Service and set the AsyncTask running in the Service and bind your Activity to the Service.
Then when a new instance of the Activity is created (ie when the user re-enters the app), it can bind to the same Service instance that's already running and pass in a reference to its self to receive progress info.
Another advantage of this approach is that your Service can put a notification icon in the notification bar, which greatly reduces it chances of being killed by the system, the user could view progress at a glance, and even be notified of when the process is complete.
Allowing an AsyncTask to be cut loose from its owner (is the Activity) and trusting that it will complete what its doing is a pretty bad idea, and will probably have some unexpected results, lead to potential memory leaks etc.

Related

How can I repeat a ListView item a certain number of times using Android and SQLite data and changing one column each iteration?

I am trying to repeat certain data from a table in a ListView with one row changing each iteration of the item. I want to have a user input an int of the times to repeat and press a button to update the list.
I am working with dates as the updating item. So if a user inputs '3', I want 3 rows added to the list, and inserted into the table, adding 1 month to the date for the 3 months.
Main Activity
planAheadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (planAheadMonths.getText().toString().trim().isEmpty()) {
Toast.makeText(getBaseContext(), "Add months to plan by", Toast.LENGTH_SHORT).show();
} else {
int plan = Integer.parseInt(planAheadMonths.getText().toString());
mDBHelper.updateCheckingPlanner(plan, mChecking.getCheckingId());
planAheadMonths.setText("");
mDBHelper.getTransRepeater(plan, mChecking.getCheckingId());
}
}
});
DBHelper
public void getTransactionRepeater(int repeat, int id) {
SQLiteDatabase db = this.getWritableDatabase();
String whereclause = TRANSACTION_DATE + " BETWEEN " + getCurrentMonth() + " AND " + getFutureMonth() + " AND " +
TRANSACTION_CHECKING_ID + " = " + id + " AND " + TRANSACTION_REPEAT + " != 'NONE' ";
String[] whereargs = new String[1];
Cursor res = db.query(TABLE_TRANSACTIONS, null, whereclause, null, null, null, TRANSACTION_DATE + " ASC, " + TRANSACTION_ID);
db.beginTransaction();
while (res.moveToNext()) {
if (res.getString(res.getColumnIndex(TRANSACTION_REPEAT)).equals("MONTH")) {
for (int i = 0; i < repeat; i++) {
+ i);
ContentValues cv = new ContentValues();
cv.put(TRANSACTION_NAME, res.getString(res.getColumnIndex(TRANSACTION_NAME)));
cv.put(TRANSACTION_TYPE, res.getString(res.getColumnIndex(TRANSACTION_TYPE)));
cv.put(TRANSACTION_DATE, sdf.format(cFuture.getTime()) + res.getString(res.getColumnIndex(TRANSACTION_NAME)) + i);
cv.put(TRANSACTION_AMOUNT, res.getInt(res.getColumnIndex(TRANSACTION_AMOUNT)));
cv.put(TRANSACTION_NOTES, res.getString(res.getColumnIndex(TRANSACTION_NOTES)));
cv.put(TRANSACTION_REPEAT, res.getString(res.getColumnIndex(TRANSACTION_REPEAT)));
cv.put(TRANSACTION_CHECKING_ID, res.getInt(res.getColumnIndex(TRANSACTION_CHECKING_ID)));
cv.put(TRANSACTION_NEW_BALANCE, res.getInt(res.getColumnIndex(TRANSACTION_NEW_BALANCE)));
cv.put(TRANSACTION_CREDIT_ID, res.getString(res.getColumnIndex(TRANSACTION_CREDIT_ID)));
whereargs[0] = res.getString(res.getColumnIndex(TRANSACTION_DATE));
db.insert(TABLE_TRANSACTIONS, null, cv);
}
}
}
res.close();
db.setTransactionSuccessful();
db.endTransaction();
}

Why does my app crash if I don't set in the SQL table not null statement?

I have this code:
{
String SQL_CREATE_BOOKS_TABLE = "CREATE TABLE " + BooksContract.BooksEntry.TABLE_NAME + " ("
+ BooksContract.BooksEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT + " TEXT NOT NULL, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PRICE + " DECIMAL NOT NULL DEFAULT 0, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY + " INTEGER NOT NULL, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER + " TEXT, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PHONE + " INTEGER );";
db.execSQL(SQL_CREATE_BOOKS_TABLE);
}
Even though I didn't set the supplier to be not null, if I don't type it in my editText when I click the save button my app crashes. Also even though quantity is set to default 0, if I don't type any quantity it still crashes. why?
EditorActivity:
private void insertBooks() {
String productString = productName.getText().toString().trim();
String priceString = price.getText().toString().trim();
int price = Integer.parseInt(priceString);
String quantityString = quantity.getText().toString().trim();
int quantity = Integer.parseInt(quantityString);
String supplierString = supplier.getText().toString().trim();
String phoneString = supplierPhone.getText().toString().trim();
int phone = Integer.parseInt(phoneString);
BooksDbHelper dbHelper = new BooksDbHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT, productString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE, priceString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, quantityString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER, supplierString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PHONE, phoneString);
long newRowId = db.insert(BooksContract.BooksEntry.TABLE_NAME, null, values);
if (newRowId == -1) {
// If the row ID is -1, then there was an error with insertion.
Toast.makeText(this, "Error with saving book", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Book saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
}
}
CatalogActivity:
private void insertBooks() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT, "Walks with men");
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE, 10.00);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, 2);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER, "Amazon");
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PHONE, 727213658);
long newRowId = db.insert(BooksContract.BooksEntry.TABLE_NAME, null, values);
}
Use this code in your insertBooks() method to avoid NumberFormatException:
String productString = productName.getText().toString().trim();
String priceString = price.getText().toString().trim();
double price = 0.0;
try {
price = Double.parseDouble(priceString);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String quantityString = quantity.getText().toString().trim();
int quantity = 0;
try {
quantity = Integer.parseInt(quantityString);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String supplierString = supplier.getText().toString().trim();
String phoneString = supplierPhone.getText().toString().trim();
Why is phone INTEGER?
Note:
The create table code you posted is probably part of the onCreate() method in your class that extends SQLiteOpenHelper.
The onCreate() method was executed the 1st time you ran the app and created the table. Since then you may have altered the columns either by type or name, or even inserted or deleted columns, but all these changes were made only in your code and not in the table. The onCreate() method has never again been triggered.
So if any of the above applies to you:
Uninstall the app from the emulator/device so that the database is deleted and run again your app to recreate it as it should be.

Retrieve records from sqllite using Array list

I am retrieving all records from database and saving all records in array list.when i show array list records ,repeated data displayed,i don't know what's the prolblem??
Calling this function in view_record class:
public ArrayList<tuple> getdata() { // TODO Auto-generated method stub
tuple obj=new tuple();
Cursor c = ourDatabase.query(DATABASE_TABLE, PROJECTION_ALL, null, null, null, null, null);
if(c == null) {
return null;
}
ArrayList <tuple> data = new ArrayList<tuple>();
// String result = " ";
int i=0;
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
obj.ROWID= c.getString(c.getColumnIndexOrThrow(KEY_ROWID));
obj.CNAME= c.getString(c.getColumnIndexOrThrow(KEY_CNAME));
obj.SNAME= c.getString(c.getColumnIndexOrThrow(KEY_SNAME));
obj.FAMILY= c.getString(c.getColumnIndexOrThrow(KEY_FAMILY));
obj.LOCATION= c.getString(c.getColumnIndexOrThrow(KEY_LOCATION));
obj.IMAGE1= c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE1));
obj.IMAGE2= c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE2));
obj.IMAGE3= c.getBlob(c.getColumnIndexOrThrow(KEY_IMAGE3));
data.add(i, obj);
i++;
}
c.close();
return data; }
My view_record class
public class view_record extends Activity {
#Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.db_view);
TextView tv=(TextView) findViewById(R.id.tvSqlinfo);
ImageView img=(ImageView) findViewById(R.id.img_view);
db_handler info=new db_handler(this);
info.open();
// Log.d("abc", "adb");
ArrayList <tuple> data=info.getdata();
Log.d("abc", "adb");
String result="";
for(int i=0;i<data.size();i++){
result +=" " + data.get(i).ROWID + " " + data.get(i).CNAME + " " + data.get(i).SNAME + " " + data.get(i).FAMILY + " " + data.get(i).LOCATION + "\n";
tv.setText(result);
img.setImageBitmap(Utilities.getImage(data.get(0).IMAGE2));
}
info.close(); } }
I want to view as follows :
" 1 cname sname family location "
" 2 cname sname family location "
But i am getting
" 2 cname sname family location "
" 2 cname sname family location "
Means last record displayed two times.
and one more thing ,i want to get all records where cname=something,getting error while doing it so
you are not making a new project in your for loop. That is whay you are getting same results. just do...
on start of for loop add this line obj=new tuple();
Answer to your Second Question
for(int i=0;i<data.size();i++){
result +=" " + data.get(i).ROWID + " " + data.get(i).CNAME + " " + data.get(i).SNAME + " " + data.get(i).FAMILY + " " + data.get(i).LOCATION + "\n";
img.setImageBitmap(Utilities.getImage(data.get(0).IMAGE2));
}
tv.setText(Html.fromHtml(result));

android list refreshing only from one void and not from another

this is quite odd to me: in my project (which by the way is derived from Yaxim, so it's a chat app for android), I'm trying to implement writing notification through the messageeventlistener. I do receive the notification event. To make it appear in the listview (which is in another class) I'm adding a row in the database, as if it were a message (only the text is "..."). I've added a counter and the row is properly inserted in the db (subsequently the row is deleted from the db when the actual message comes). The notifychanges is also called properly. The strange thing is that despite all of this the row doesn't appear in the listview. If I move the procedure a few lines below, on the packetlistener for the regular messages, it gets inserted. However I cannot leave it like that because the packetlistener doesn't receive properly all composing notifications, so it doesn't work all the time for that purpose. Also, while the writing notification is properly inserted into the db, if I keep the procedure in the messageEventNotificationListener() the writing notification message does not get deleted when the actual message arrives (and the iCount keeps increasing); if I put the procedure in the packetListener it does (hence iCount equals 0 after the message is pushed into the db)
This is the messageeventlistener:
private void messageEventNotificationListener(){
// Create a MessageEventManager
if (mMessageEventManager == null)
mMessageEventManager = new MessageEventManager(mXMPPConnection);
// Add the listener that will react to the event notifications
mMessageEventManager.addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) {
Log.d(TAG, "The message has been delivered (" + from + ", " + packetID + ")");
}
public void displayedNotification(String from, String packetID) {
Log.d(TAG, "The message has been displayed (" + from + ", " + packetID + ")");
}
public void composingNotification(String from, String packetID) {
Log.d(TAG, "The message's receiver is composing a reply (" + from + ", " + packetID + ")");
//controlla se l'utente non sta giĆ  scrivendo
String selection = ChatConstants.JID + " = '" + from + "' AND " +
ChatConstants.DELIVERY_STATUS + " = " + ChatConstants.DS_WRITING;
Cursor cursor = mContentResolver.query(ChatProvider.CONTENT_URI,
new String[] { "count(" + ChatConstants._ID + ")" },
selection, null, null);
cursor.moveToFirst();
int iCount = cursor.getInt(0);
//if (iCount == 0)
//{
addChatMessageToDB(ChatConstants.INCOMING, from, "...", ChatConstants.DS_WRITING, System.currentTimeMillis(), packetID);
//}
}
public void offlineNotification(String from, String packetID) {
Log.d(TAG, "The message's receiver is offline (" + from + ", " + packetID + ")");
}
public void cancelledNotification(String from, String packetID) {
Log.d(TAG, "The message's receiver cancelled composing a reply (" + from + ", " + packetID + ")");
}
});
}
and here is the packetlistener:
private void registerMessageListener() {
// do not register multiple packet listeners
if (mPacketListener != null)
mXMPPConnection.removePacketListener(mPacketListener);
PacketTypeFilter filter = new PacketTypeFilter(Message.class);
mPacketListener = new PacketListener() {
public void processPacket(Packet packet) {
try {
if (packet instanceof Message) {
Message msg = (Message) packet;
String chatMessage = msg.getBody();
DeliveryReceipt dr = (DeliveryReceipt)msg.getExtension("received", DeliveryReceipt.NAMESPACE);
if (dr != null) {
Log.d(TAG, "got delivery receipt for " + dr.getId());
changeMessageDeliveryStatus(dr.getId(), ChatConstants.DS_DELIVERED);
}
if (chatMessage == null)
return;
if (msg.getType() == Message.Type.error) {
chatMessage = "<Error> " + chatMessage;
}
long ts;
DelayInfo timestamp = (DelayInfo)msg.getExtension("delay", "urn:xmpp:delay");
if (timestamp == null)
timestamp = (DelayInfo)msg.getExtension("x", "jabber:x:delay");
if (timestamp != null)
ts = timestamp.getStamp().getTime();
else
ts = System.currentTimeMillis();
String fromJID = getJabberID(msg.getFrom());
//elimina il messaggio "writing" se esiste
deleteWritingChatMessageFromDB(fromJID);
if (msg.getExtension("request", DeliveryReceipt.NAMESPACE) != null) {
// got XEP-0184 request, send receipt
sendReceipt(msg.getFrom(), msg.getPacketID());
}
addChatMessageToDB(ChatConstants.INCOMING, fromJID, chatMessage, ChatConstants.DS_NEW, ts, msg.getPacketID());
mServiceCallBack.newMessage(fromJID, chatMessage);
}
}
} catch (Exception e) {
// SMACK silently discards exceptions dropped from processPacket :(
Log.e(TAG, "failed to process packet:");
e.printStackTrace();
}
};
mXMPPConnection.addPacketListener(mPacketListener, filter);
}
and finally the insert and delete method
private void addChatMessageToDB(int direction, String JID,
String message, int delivery_status, long ts, String packetID) {
ContentValues values = new ContentValues();
values.put(ChatConstants.DIRECTION, direction);
values.put(ChatConstants.JID, JID);
values.put(ChatConstants.MESSAGE, message);
values.put(ChatConstants.DELIVERY_STATUS, delivery_status);
values.put(ChatConstants.DATE, ts);
values.put(ChatConstants.PACKET_ID, packetID);
Uri noteUri = mContentResolver.insert(ChatProvider.CONTENT_URI, values);
//mContentResolver.notifyChange(noteUri, null);
}
private void deleteWritingChatMessageFromDB(String JID) {
int count = mContentResolver.delete(ChatProvider.CONTENT_URI,
ChatConstants.JID + " = ? AND " + ChatConstants.MESSAGE + " = ? AND " + ChatConstants.DELIVERY_STATUS + " = ?", new String[] { JID, "...", "3" });
debugLog("deleteWritingChatMessageEntryFromDB: Deleted " + count + " entries");
}
both these methods get called into the ChatProvider class and end with
getContext().getContentResolver().notifyChange(url, null);
Turns out the problem was different: I posting this just in case someone else runs in the same issue. The message was infact inserted into the db, but while the packetlistener returns only the jid, the messageeventlistener returns the jid with the resource at the end as well, which needed to be taken away before the insert. Done that, the message appeared in the list. Also: the code I posted is not completely correct to handle the messageEvent. The listeners need to be called from inside the packetListener directly.

Android Application did not close cursor or database

Im running into an issue inserting items into my database. It really odd that the program will run through 4 rows and insert them fine, but on the 5th row it crashes and I got three reports of Application did not close cursor or database. In my code I am closing and reopening both the cursor and the database so I am really confused on how this is happening. Here is the code.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case REQUEST_CODE_NEW:
if (resultCode == -1) {
Integer chapterId = data.getIntExtra("chapterId", -1);
Toast toast = Toast.makeText(this, "Selected Chapter: " + chapterId.toString(), Toast.LENGTH_LONG);
if (chapterId != -1) {
DbHelper db = new DbHelper(this);
for (int i = 0; i < questions.getTitle().size(); i++) {
db.insertQuestion(chapterId.toString(), questions.getTitle().get(i), questions.getAnswer().get(i), questions.getAr().get(i));
}
}
toast.show();
}
break;
}
}
Thats calling the database to be created through a dbhelper class. Here is the database helper
public void insertQuestion(String cat_id, String title, String answer, String article) {
db.execSQL("INSERT INTO " + QUESTION_TABLE_NAME + " (title,answer,article,picture) VALUES ('" + title + "','" + answer + "','" + article + "','none')");
String id = getQuestionId(title);
db.execSQL("INSERT INTO " + REL_TABLE_NAME + " (question_id, cat_id) VALUES (" + id + "," + cat_id + ")");
}
public String getQuestionId(String title) {
String id;
cursor = this.db.rawQuery("SELECT question_id FROM " + QUESTION_TABLE_NAME + " WHERE title = '" + title + "' LIMIT 1", null);
cursor.moveToFirst();
id = cursor.getString(0);
cursor.close();
db.close();
return id;
}
I put the cursor at the class level so I can try to manage it better, but still to no avail. Its still crashing after 4 queries.
Try this "fix":
DbHelper db = new DbHelper(this);
try{
for (int i = 0; i < questions.getTitle().size(); i++) {
db.insertQuestion(chapterId.toString(), questions.getTitle().get(i), questions.getAnswer().get(i), questions.getAr().get(i));
}
}catch(Exception e1){
android.util.Log.v("DbHelper Error","Crash: "+e1.getMessage(), e1);
}finally{
db.close();
}

Categories

Resources