android.database.Cursor.getColumnIndex()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(1261)

本文整理了Java中android.database.Cursor.getColumnIndex()方法的一些代码示例,展示了Cursor.getColumnIndex()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cursor.getColumnIndex()方法的具体详情如下:
包路径:android.database.Cursor
类名称:Cursor
方法名:getColumnIndex

Cursor.getColumnIndex介绍

[英]Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use #getColumnIndexOrThrow(String) instead, which will make the error more clear.
[中]返回给定列名的从零开始的索引,如果该列不存在,则返回-1。如果希望该列存在,请改用#getColumnIndexOrThrow(字符串),这将使错误更加清楚。

代码示例

代码示例来源:origin: stackoverflow.com

String selection = "_id = "+id;
Uri uri = Uri.parse("content://sms");
Cursor cursor = contentResolver.query(uri, null, selection, null, null);
String phone = cursor.getString(cursor.getColumnIndex("address"));
int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc.
String date = cursor.getString(cursor.getColumnIndex("date"));
String body = cursor.getString(cursor.getColumnIndex("body"));

代码示例来源:origin: stackoverflow.com

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while(people.moveToNext()) {
  int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
  String contact = people.getString(nameFieldColumnIndex);
  int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
  String number = people.getString(numberFieldColumnIndex);
}

people.close();

代码示例来源:origin: stackoverflow.com

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
 super.onActivityResult(reqCode, resultCode, data);

 switch (reqCode) {
  case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) {
    Uri contactData = data.getData();
    Cursor c =  getContentResolver().query(contactData, null, null, null, null);
    if (c.moveToFirst()) {
     String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
     // TODO Whatever you want to do with the selected contact name.
    }
   }
   break;
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testExecSQL() throws Exception {
  database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');");
  Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM table_name", null);
  assertThat(cursor).isNotNull();
  assertThat(cursor.moveToNext()).isTrue();
  assertThat(cursor.getInt(0)).isEqualTo(1);
  cursor = database.rawQuery("SELECT * FROM table_name", null);
  assertThat(cursor).isNotNull();
  assertThat(cursor.moveToNext()).isTrue();
  assertThat(cursor.getInt(cursor.getColumnIndex("id"))).isEqualTo(1234);
  assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Chuck");
}

代码示例来源:origin: aa112901/remusic

public static Uri getAlbumUri(Context context, long musicId) {
  ContentResolver cr = context.getContentResolver();
  Cursor cursor = cr.query(Media.EXTERNAL_CONTENT_URI, proj_music, "_id =" + String.valueOf(musicId), null, null);
  long id = -3;
  if (cursor == null) {
    return null;
  }
  if (cursor.moveToFirst()) {
    id = cursor.getInt(cursor.getColumnIndex(Media.ALBUM_ID));
  }
  cursor.close();
  return getAlbumArtUri(id);
}

代码示例来源:origin: xinghongfei/LookLook

public boolean isRead(String table, String key, int value) {
  boolean isRead = false;
  Cursor cursor = mSQLiteDatabase.query(table, null, "key=?", new String[]{key}, null, null, null);
  if (cursor.moveToNext() && (cursor.getInt(cursor.getColumnIndex("is_read")) == value)) {
    isRead = true;
  }
  cursor.close();
  return isRead;
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testIsNullWhenNull() throws Exception {
 assertThat(cursor.moveToFirst()).isTrue();
 assertThat(cursor.moveToNext()).isTrue();
 assertThat(cursor.isNull(cursor.getColumnIndex("id"))).isFalse();
 assertThat(cursor.isNull(cursor.getColumnIndex("name"))).isFalse();
 assertThat(cursor.isNull(cursor.getColumnIndex("long_value"))).isTrue();
 assertThat(cursor.isNull(cursor.getColumnIndex("float_value"))).isTrue();
 assertThat(cursor.isNull(cursor.getColumnIndex("double_value"))).isTrue();
}

代码示例来源:origin: naman14/Timber

public static final SortedCursor makeRecentTracksCursor(final Context context) {
  Cursor songs = RecentStore.getInstance(context).queryRecentIds(null);
  try {
    return makeSortedCursor(context, songs,
        songs.getColumnIndex(SongPlayCount.SongPlayCountColumns.ID));
  } finally {
    if (songs != null) {
      songs.close();
      songs = null;
    }
  }
}

代码示例来源:origin: stackoverflow.com

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
 String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
 String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}
phones.close();

代码示例来源:origin: ACRA/acra

@NonNull
public static String getFileNameFromUri(@NonNull Context context, @NonNull Uri uri) throws FileNotFoundException {
  try (Cursor cursor = context.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
    }
  }
  throw new FileNotFoundException("Could not resolve filename of " + uri);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void query_shouldReturnColumnValues() throws Exception {
 ShadowDownloadManager manager = new ShadowDownloadManager();
 long id = manager.enqueue(request.setDestinationUri(destination));
 Cursor cursor = manager.query(new DownloadManager.Query().setFilterById(id));
 cursor.moveToNext();
 assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))).isEqualTo(uri.toString());
 assertThat(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).isEqualTo(destination.toString());
}

代码示例来源:origin: bumptech/glide

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == REQUEST_CONTACT && resultCode == RESULT_OK) {
  Uri uri = Preconditions.checkNotNull(data.getData());
  final Cursor cursor = getContentResolver().query(uri, null, null, null, null);
  try {
   if (cursor != null && cursor.moveToFirst()) {
    final long contactId = cursor.getLong(cursor.getColumnIndex(Contacts._ID));
    showContact(contactId);
   }
  } finally {
   if (cursor != null) {
    cursor.close();
   }
  }
  return;
 }
 super.onActivityResult(requestCode, resultCode, data);
}

代码示例来源:origin: zhihu/Matisse

public static Item valueOf(Cursor cursor) {
  return new Item(cursor.getLong(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)),
      cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.MIME_TYPE)),
      cursor.getLong(cursor.getColumnIndex(MediaStore.MediaColumns.SIZE)),
      cursor.getLong(cursor.getColumnIndex("duration")));
}

代码示例来源:origin: stackoverflow.com

protected void onActivityResult(int requestCode, int resultCode, 
    Intent imageReturnedIntent) {
  super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

  switch(requestCode) { 
  case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
      Uri selectedImage = imageReturnedIntent.getData();
      String[] filePathColumn = {MediaStore.Images.Media.DATA};

      Cursor cursor = getContentResolver().query(
                selectedImage, filePathColumn, null, null, null);
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String filePath = cursor.getString(columnIndex);
      cursor.close();

      Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    }
  }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testExecSQLInsertNull() throws Exception {
  String name = "nullone";
  database.execSQL("insert into exectable (first_column, name) values (?,?);", new String[]{null, name});
  Cursor cursor = database.rawQuery("select * from exectable WHERE `name` = ?", new String[]{name});
  cursor.moveToFirst();
  int firstIndex = cursor.getColumnIndex("first_column");
  int nameIndex = cursor.getColumnIndex("name");
  assertThat(cursor.getString(nameIndex)).isEqualTo(name);
  assertThat(cursor.getString(firstIndex)).isEqualTo(null);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testReplace() throws Exception {
  long id = addChuck();
  assertThat(id).isNotEqualTo(-1L);
  ContentValues values = new ContentValues();
  values.put("id", id);
  values.put("name", "Norris");
  long replaceId = database.replace("table_name", null, values);
  assertThat(replaceId).isEqualTo(id);
  String query = "SELECT name FROM table_name where id = " + id;
  Cursor cursor = executeQuery(query);
  assertThat(cursor.moveToNext()).isTrue();
  assertThat(cursor.getString(cursor.getColumnIndex("name"))).isEqualTo("Norris");
}

代码示例来源:origin: stackoverflow.com

DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = mgr.enqueue(request);

DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(id);
Cursor cursor = mgr.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
cursor.close();

代码示例来源:origin: jeasonlzy/okhttp-OkGo

public static SerializableCookie parseCursorToBean(Cursor cursor) {
  String host = cursor.getString(cursor.getColumnIndex(HOST));
  byte[] cookieBytes = cursor.getBlob(cursor.getColumnIndex(COOKIE));
  Cookie cookie = bytesToCookie(cookieBytes);
  return new SerializableCookie(host, cookie);
}

代码示例来源:origin: stackoverflow.com

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER};

Cursor people = getContentResolver().query(uri, projection, null, null, null);

int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

if(people.moveToFirst()) {
  do {
    String name   = people.getString(indexName);
    String number = people.getString(indexNumber);
    // Do work...
  } while (people.moveToNext());
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

public static <T> CacheEntity<T> parseCursorToBean(Cursor cursor) {
  CacheEntity<T> cacheEntity = new CacheEntity<>();
  cacheEntity.setKey(cursor.getString(cursor.getColumnIndex(KEY)));
  cacheEntity.setLocalExpire(cursor.getLong(cursor.getColumnIndex(LOCAL_EXPIRE)));
  cacheEntity.setResponseHeaders((HttpHeaders) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(HEAD))));
  //noinspection unchecked
  cacheEntity.setData((T) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(DATA))));
  return cacheEntity;
}

相关文章