【问题标题】:Simple query cursor in a thread is not finding value线程中的简单查询游标未找到值
【发布时间】:2015-12-16 23:45:45
【问题描述】:

由于某些原因,在以下函数中,光标没有读取任何标记。我不知道我做错了什么,我一直在为我们调试这段代码。当它运行时,它说 tagid 和 catid 的值都是-1。没有乒乓球或乒乓球:(

public String getCategoryNameByLawId(final int lawID){

    final String[] categoryName = {"Success"+lawID};
    final int[] tagID = {-1};
    final int[] categoryID =  {-1};

    runnable = new Runnable() {
        @Override
        public void run() {
            try {
                openToRead();

                String Query = "SELECT * from " + Constants.TABLE_LAW_TAG;
                Cursor c1 = mSqLiteDatabase.rawQuery(Query, null);

                if (c1.moveToFirst()) {
                    categoryName[0] = "ping";
                    while (c1.isAfterLast() == false) {
                        categoryName[0] = "pong";
                        try {
                            if (c1.getInt(c1.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) {
                                int indexTagID = c1.getColumnIndex(Constants.KEY_TAG_ID);
                                tagID[0] = c1.getInt(indexTagID);
                                categoryName[0] = "pang";
                            }
                        } catch (Exception e) {
                            categoryName[0] = e.getMessage();
                        }
                        c1.moveToNext();
                    }
                }
                close();
                openToRead();
                Query = "SELECT * from " + Constants.TABLE_CATEGORY_TAG;
                Cursor c2 = mSqLiteDatabase.rawQuery(Query, null);
                if (c2.moveToFirst()) {
                    while (c2.isAfterLast() == false) {
                        if (c2.getInt(c2.getColumnIndex(Constants.KEY_TAG_ID)) == tagID[0]) {
                            int indexCategoryID = c2.getColumnIndex(Constants.KEY_CATEGORY_ID);
                            categoryID[0] = c2.getInt(indexCategoryID);
                        }
                        c2.moveToNext();
                    }
                }

                /*
                exceptionHandler.alert(new RuntimeException(), "catid-" + categoryID[0]);

                Query = "SELECT * from " + Constants.TABLE_CATEGORY;
                Cursor c3 = mSqLiteDatabase.rawQuery(Query, null);

                if (c3.moveToFirst()) {
                    while (c3.isAfterLast() == false) {
                        if (c3.getInt(c3.getColumnIndex(Constants.KEY_CATEGORY_ID)) == categoryID[0]) {
                            int indexCategoryName = c3.getColumnIndex(Constants.KEY_CATEGORY_NAME);
                            categoryName[0] = c3.getString(indexCategoryName);
                        }
                        c3.moveToNext();
                    }
                }
                exceptionHandler.alert(new RuntimeException(), "catnam-" + categoryName[0]);*/

                close();
            }
            catch(Exception e){
                categoryName[0] ="error";
            }
        }
    };
    new Thread(runnable).start();
    return categoryName[0].toLowerCase() + " tagid: "+ tagID[0]+ " catid: "+ categoryID[0];

}

【问题讨论】:

  • 您的数据库是否已创建。
  • 是的,内容是正确的
  • 我已经完成了这个项目
  • 据我所知,if (c1.getInt(c1.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) { int indexTagID = c1.getColumnIndex(Constants.KEY_TAG_ID); tagID[0] = c1.getInt(indexTagID); 中可能有错误,这就是为什么也没有设置 catid
  • 线程不是这样工作的。如果要使用它们,则需要了解线程的作用。 (简而言之:你启动了你的线程,但它并没有立即执行。否则如果使用线程就没有意义了)

标签: java android multithreading sqlite


【解决方案1】:

start() 只是启动线程;它不会等待它完成。 问题是当您执行return categoryName[0]... 时,尚未找到该值

删除Runnable间接,直接执行数据库代码。

如果你真的想使用一个线程,你可以等待线程完成(调用它的join()),但这没有意义,因为只要数据库代码正在运行,你的主线程就会被挂起。为了能够在主线程中执行其他操作,您必须重新组织程序,以便数据库代码在获得结果时将单独的消息发送回主线程。让它查看CursorLoader(不过需要内容提供者)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多