Android日历-查询实例表不总是返回最新数据
创始人
2024-10-09 18:32:01
0

在Android日历中,查询实例表不总是返回最新数据的问题可能是由于缓存数据或异步查询导致的。以下是解决该问题的代码示例:

方法1:使用AsyncQueryHandler进行异步查询

public class MainActivity extends AppCompatActivity {

    private static final int QUERY_INSTANCE_TOKEN = 1;

    private AsyncQueryHandler queryHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        queryHandler = new MyAsyncQueryHandler(getContentResolver());

        // 查询实例表
        Uri uri = CalendarContract.Instances.CONTENT_URI;
        String[] projection = {CalendarContract.Instances.TITLE, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END};
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = null;

        queryHandler.startQuery(QUERY_INSTANCE_TOKEN, null, uri, projection, selection, selectionArgs, sortOrder);
    }

    private class MyAsyncQueryHandler extends AsyncQueryHandler {

        public MyAsyncQueryHandler(ContentResolver cr) {
            super(cr);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            super.onQueryComplete(token, cookie, cursor);

            if (token == QUERY_INSTANCE_TOKEN) {
                // 处理查询结果
                if (cursor != null && cursor.moveToFirst()) {
                    do {
                        // 获取数据
                        String title = cursor.getString(cursor.getColumnIndex(CalendarContract.Instances.TITLE));
                        long beginTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Instances.BEGIN));
                        long endTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Instances.END));

                        // 处理数据...
                    } while (cursor.moveToNext());
                }
                // 关闭游标
                if (cursor != null) {
                    cursor.close();
                }
            }
        }
    }
}

方法2:使用ContentObserver监听数据变化

public class MainActivity extends AppCompatActivity {

    private static final int QUERY_INSTANCE_TOKEN = 1;

    private Cursor cursor;
    private ContentObserver contentObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注册ContentObserver
        contentObserver = new MyContentObserver();
        getContentResolver().registerContentObserver(CalendarContract.Instances.CONTENT_URI, true, contentObserver);

        // 查询实例表
        Uri uri = CalendarContract.Instances.CONTENT_URI;
        String[] projection = {CalendarContract.Instances.TITLE, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END};
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = null;

        cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // 获取数据
                String title = cursor.getString(cursor.getColumnIndex(CalendarContract.Instances.TITLE));
                long beginTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Instances.BEGIN));
                long endTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Instances.END));

                // 处理数据...
            } while (cursor.moveToNext());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 取消注册ContentObserver
        getContentResolver().unregisterContentObserver(contentObserver);

        // 关闭游标
        if (cursor != null) {
            cursor.close();
        }
    }

    private class MyContentObserver extends ContentObserver {

        public MyContentObserver() {
            super(new Handler());
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            // 查询实例表
            Uri uri = CalendarContract.Instances.CONTENT_URI;
            String[] projection = {CalendarContract.Instances.TITLE, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END};
            String selection = null;
            String[] selectionArgs = null;
            String sortOrder = null;

            Cursor newCursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
            if (newCursor != null && newCursor.moveToFirst()) {
                do {
                    // 获取数据
                    String title = newCursor.getString(newCursor.getColumnIndex(CalendarContract.Instances.TITLE));
                    long beginTime = newCursor.getLong(newCursor.getColumnIndex(CalendarContract.Instances.BEGIN));
                    long endTime = newCursor.getLong(newCursor.getColumnIndex(CalendarContract.Instances.END));

                    // 处理数据...
                } while (newCursor.moveToNext());
            }

            // 关闭新游标
            if (newCursor != null) {
                newCursor.close();
            }
        }
    }
}

这些代码示例演示了两种解决方法:使用AsyncQueryHandler进行异步查询和使用

相关内容

热门资讯

透视教学!aapoker万能辅... 透视教学!aapoker万能辅助器,pokemmo脚本辅助器下载(透视)本来是真的有挂(揭秘攻略)透...
透视软件!拱趴大菠萝有什么挂,... 透视软件!拱趴大菠萝有什么挂,pokemmo手机版修改器,好像是有挂(解密教程)1、拱趴大菠萝有什么...
透视辅助!aapoker ai... 透视辅助!aapoker ai插件,wepoker手机助手(透视)原生真的是有挂(必胜教程);1)w...
透视辅助!pokemmo手机脚... 透视辅助!pokemmo手机脚本辅助器,werplan有挂吗,都是存在有挂(技巧教程)1、pokem...
透视玄学!来玩app破解,we... 透视玄学!来玩app破解,wepoker有透视底牌吗(透视)固有真的是有挂(安装教程);1、wepo...
透视透视!pokemmo修改器... 透视透视!pokemmo修改器手机版,pokermaster辅助器,本来是有挂(技巧教程);1、很好...
辅助透视!wepoker免费脚... 辅助透视!wepoker免费脚本,poker world辅助器(透视)一贯是真的有挂(玩家教你)1)...
透视有挂!xpoker辅助控制... 透视有挂!xpoker辅助控制,红龙poker作弊指令,竟然存在有挂(系统教程);1、下载好红龙po...
透视科技!epoker透视底牌... 透视科技!epoker透视底牌,哈糖大菠萝开挂(透视)一向是有挂(总结教程)1、下载好哈糖大菠萝开挂...
透视实锤!pokerworld... 透视实锤!pokerworld辅助器,约局吧游戏挂,都是真的是有挂(AI教程);1、完成pokerw...