Why is this cursor empty?
Asked Answered
S

0

9

I have for example button and when user click on it I want to find out the download history by DownloadManager, but problem is that my cursor is empty, it never goes to condition if(c.moveToFirst()), it always skip this condition.

DownloadManager downloadMgr = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
                Cursor c = downloadMgr.query(query);
                if(c==null) {
                    //
                }
                else {
                    if(c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        int status = c.getInt(columnIndex);
                        if(status == DownloadManager.STATUS_RUNNING){
                        //do something
                        }
                    }
                }

I also tried this code (by imran khan) but still same problem:

DownloadManager.Query query = null;
    Cursor c = null;
    DownloadManager downloadManager = null;
    downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
    query = new DownloadManager.Query();
    query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
    c = downloadManager.query(query);
    if(c.moveToFirst()) { 
    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); 
    switch(status) { 
    case DownloadManager.STATUS_PAUSED: 
    break; 
    case DownloadManager.STATUS_PENDING: 
    break; 
    case DownloadManager.STATUS_RUNNING: 
    break; 
    case DownloadManager.STATUS_SUCCESSFUL: 
    break; 
    case DownloadManager.STATUS_FAILED: 
    break; 
    }
}
Scrubland answered 22/4, 2012 at 10:38 Comment(5)
What do you mean by "skip this condition"? Do you mean that the condition isn't evaluated, or that it's evaluated but returns false. And the two pieces of code are completely different, so just saying you get the "same" problem isn't very helpful. For your second example, on which line is the error, and what specifically is the error message?Opuscule
"Skip condition" means that this condition is not evaluated (fulfilled). I know that these codes are different, but the problem is same, condition is not fulfilled. I haven't got an error, my problem is that it always "skips" that condition if(c.moToFirst()), because it seems that cursor is empty.Scrubland
what do you mean by a cursor being "empty"? Do you mean that it is null? Or it isn't null, but the result set that it is a cursor for is an empty result set?Opuscule
no cursor is not null, cursor is empty, because it doesn not return TRUE value from moveToFirst.Scrubland
@Scrubland Please post the solution if you had found one.Gentle

© 2022 - 2024 — McMap. All rights reserved.