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; 
    }
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Adam
  • 2,152
  • 4
  • 35
  • 45
  • 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? – Mark Byers Apr 22 '12 at 10:45
  • "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. – Adam Apr 22 '12 at 11:03
  • 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? – Mark Byers Apr 22 '12 at 11:11
  • no cursor is not null, cursor is empty, because it doesn not return TRUE value from moveToFirst. – Adam Apr 22 '12 at 11:39
  • 5
    @Adam Please post the solution if you had found one. – Talespin_Kit Oct 20 '13 at 00:08

0 Answers0