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

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

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

Cursor.getString介绍

[英]Returns the value of the requested column as a String.

The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.
[中]以字符串形式返回请求列的值。
结果以及当列值为null或列类型不是字符串类型时,此方法是否引发异常由实现定义。

代码示例

代码示例来源: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

public String getRealPathFromURI(Context context, Uri contentUri) {
 Cursor cursor = null;
 try { 
  String[] proj = { MediaStore.Images.Media.DATA };
  cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
 } finally {
  if (cursor != null) {
   cursor.close();
  }
 }
}

代码示例来源: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 c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

if (c.moveToFirst()) {
  while ( !c.isAfterLast() ) {
    Toast.makeText(activityName.this, "Table Name=> "+c.getString(0), Toast.LENGTH_LONG).show();
    c.moveToNext();
  }
}

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

String[] projection = { MediaStore.Images.Media.DATA}; 
     Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
     int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     String capturedImageFilePath = cursor.getString(column_index_data);

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

private String getNote(long contactId) { 
  String note = null; 
  String[] columns = new String[] { ContactsContract.CommonDataKinds.Note.NOTE }; 
  String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
  String[] whereParameters = new String[]{Long.toString(contactId), ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
  Cursor contacts = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null); 
  if (contacts.moveToFirst()) { 
   rv = contacts.getString(0); 
  } 
  contacts.close(); 
  return note; 
}

代码示例来源: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: square/sqlbrite

@Override public String apply(Query query) {
  Cursor cursor = query.run();
  try {
   if (!cursor.moveToNext()) {
    throw new AssertionError("No rows");
   }
   return cursor.getString(0);
  } finally {
   cursor.close();
  }
 }
});

代码示例来源:origin: greenrobot/greenDAO

public void testQuery() {
  SimpleEntity entity = new SimpleEntity();
  entity.setSimpleString("hello");
  daoSession.insert(entity);
  long id = entity.getId();
  SimpleEntity entity2 = new SimpleEntity();
  entity2.setSimpleString("content");
  daoSession.insert(entity2);
  long id2 = entity2.getId();
  Cursor cursor = getContext().getContentResolver().query(SimpleEntityContentProvider.CONTENT_URI, null,
      null, null, "_id");
  assertEquals(2, cursor.getCount());
  int idxId = cursor.getColumnIndexOrThrow(SimpleEntityDao.Properties.Id.columnName);
  int idxString = cursor.getColumnIndexOrThrow(SimpleEntityDao.Properties.SimpleString.columnName);
  assertTrue(cursor.moveToFirst());
  assertEquals("hello", cursor.getString(idxString));
  assertEquals(id, cursor.getLong(idxId));
  assertTrue(cursor.moveToNext());
  assertEquals("content", cursor.getString(idxString));
  assertEquals(id2, cursor.getLong(idxId));
}

代码示例来源: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: stackoverflow.com

public String getRealPathFromURI(Uri contentUri) {
  String res = null;
  String[] proj = { MediaStore.Images.Media.DATA };
  Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
  if(cursor.moveToFirst()){;
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    res = cursor.getString(column_index);
  }
  cursor.close();
  return res;
}

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

// public static final String INBOX = "content://sms/inbox";
// public static final String SENT = "content://sms/sent";
// public static final String DRAFT = "content://sms/draft";
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
  do {
    String msgData = "";
    for(int idx=0;idx<cursor.getColumnCount();idx++)
    {
      msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
    }
    // use msgData
  } while (cursor.moveToNext());
} else {
  // empty box, no SMS
}

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

public String getRealPathFromURI(Uri contentUri) {
   String[] proj = { MediaStore.Images.Media.DATA };
   Cursor cursor = managedQuery(contentUri, proj, null, null, null);
   int column_index = cursor
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
   return cursor.getString(column_index);
 }

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

@Nullable
private String getPath(@NonNull Uri uri) {
 final Cursor cursor = query.query(uri);
 try {
  if (cursor != null && cursor.moveToFirst()) {
   return cursor.getString(0);
  } else {
   return null;
  }
 } finally {
  if (cursor != null) {
   cursor.close();
  }
 }
}

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

Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(
               getContentResolver(), selectedImageUri,
               MediaStore.Images.Thumbnails.MINI_KIND,
               null );
if( cursor != null && cursor.getCount() > 0 ) {
   cursor.moveToFirst();//**EDIT**
   String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
}

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

@Test
public void testDataInMemoryDatabaseIsPersistentAfterClose() throws Exception {
  SQLiteDatabase db1 = openOrCreateDatabase("db1");
  db1.execSQL("CREATE TABLE foo(id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);");
  ContentValues d1 = new ContentValues();
  d1.put("data", "d1");
  db1.insert("foo", null, d1);
  db1.close();
  SQLiteDatabase db2 = openOrCreateDatabase("db1");
  Cursor c = db2.rawQuery("select * from foo", null);
  assertThat(c).isNotNull();
  assertThat(c.getCount()).isEqualTo(1);
  assertThat(c.moveToNext()).isTrue();
  assertThat(c.getString(c.getColumnIndex("data"))).isEqualTo("d1");
}

代码示例来源:origin: greenrobot/greenDAO

public static String queryString(Database db, String sql) {
  Cursor cursor = db.rawQuery(sql, null);
  try {
    assertTrue(cursor.moveToNext());
    return cursor.getString(0);
  } finally {
    cursor.close();
  }
}

代码示例来源: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);
    }
  }
}

相关文章