@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { switch (sUriMatcher.match(uri)) { case SP: SharedPreferences sp = getSharedPreference(SP_NAME_STRING); //两列 数据 MatrixCursor cursor = new MatrixCursor(new String[] { "key", "value" }); Map<String, ?> map = sp.getAll(); if (map == null || map.size() == 0) { Log.i(getClass().getSimpleName(), "map 为空"); return null; } if (selection != null) { Log.i(getClass().getSimpleName(), "查询 单条"); //统一以String 类型返回 cursor.addRow(new String[] { selection, String.valueOf(map.get(selection)) }); } else { Log.i(getClass().getSimpleName(), "查询 全部"); Iterator<String> iterator = map.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); cursor.addRow(new String[] { key, String.valueOf(map.get(key)) }); } } return cursor; default: break; } return null; } private SharedPreferences getSharedPreference(String spName) { return getContext().getSharedPreferences(spName, Context.MODE_PRIVATE); }
Cursor cursor = getContentResolver().query( Uri.parse("content://com.secapp.auth/SEC"), null, selection, null, null); if(cursor==null) { Toast.makeText(this, "cursor==null", Toast.LENGTH_SHORT).show(); return; } while (cursor.moveToNext()) { System.out.println("key=" + cursor.getString(0) + " value=" + cursor.getString(1)); // if (cursor.getString(0).equals("keyInt")) { // int i = cursor.getInt(1); // if (i == 10) { // System.out.println("i=" + i); // } // } }
时间: 2024-11-09 06:22:13