从我的内容提供者返回一列的所有行
Returning all rows of a column from my content provider
我正在尝试访问我已经创建的 Content Provider(这是我非常严格地按照教程创建的,我已经使用教程引导我完成的代码对其进行了测试并且工作正常)并阅读电子邮件我添加到 SQLite 数据库中的地址。在数据库中是 ID,一个 COLUMN_NAME 和一个 COLUMN_EMAIL。我想获取电子邮件列的所有行,并将其作为字符串的 ArrayList returned 到此 activity.
到目前为止我最好的猜测是我会在某个地方使用内容提供者的查询方法查询数据库,return 光标到 activity,然后收集所有字符串从行中发送一个只包含该列投影的查询,或者过滤掉所有 @lorem.com 或游标的第二个索引或某种 post 数据检索过滤器。
基本上我只是被困住了。
好的,这是我的代码:
public class EmailScheduler extends AppCompatActivity implements LoaderManager
.LoaderCallbacks<Cursor> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email_scheduler);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView emailText = (TextView) findViewById(R.id.emailText);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor cursor = getContacts();
Log.i("TAG", cursor.getColumnName(2));
emailText.append(cursor.toString());
}
});
}
private static final int CONTACT_LOADER = 0;
public Uri contactUri;
ArrayList<String> addresses = new ArrayList<>();
Cursor cursor;
private Cursor getContacts() {
// Run query
Uri uri = Contact.CONTENT_URI;
String[] projection = new String[] { Contact._ID,
Contact.COLUMN_NAME };
String selection = Contact.COLUMN_EMAIL + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = Contact.COLUMN_NAME
+ " COLLATE LOCALIZED ASC";
return getContentResolver().query(uri, projection, selection, selectionArgs,
sortOrder);
}
// called by LoaderManager to create a Loader
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader;
switch (id) {
case CONTACT_LOADER:
cursorLoader = new CursorLoader(this,
contactUri, // Uri of contact to display
null, // null projection returns all columns
null, // null selection returns all rows
null, // no selection arguments
null); // sort order
break;
default:
cursorLoader = null;
break;
}
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.i("TAG", "got to the beginning of onloadfinish " + data);
if (data != null && data.moveToFirst()) {
int nameIndex = data.getColumnIndex(Contact.COLUMN_NAME);
int emailIndex = data.getColumnIndex(Contact.COLUMN_EMAIL);
String address = data.getString(emailIndex);
Log.i("TAG", address);
while (data.getString(emailIndex) != null){
addresses.add(cursor.getString(cursor.getColumnIndex(
Contact.COLUMN_EMAIL)));
Log.i("TAG", addresses.toString());}
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) { }
}
在 onCreate 方法中,当我 运行 时,它 return 发送此数据:android.content.ContentResolver$CursorWrapperInner@60c9bd2
如何从中获取我需要的信息?我尝试的一切都变成了死胡同。
private void getContacts() {
List<String> addresses = new ArrayList<String>();
// Run query
Uri uri = Contact.CONTENT_URI;
String[] projection = new String[] { Contact.COLUMN_EMAIL };
String selection = null;
String[] selectionArgs = null;
String sortOrder = Contact.COLUMN_EMAIL
+ " COLLATE LOCALIZED ASC";
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs,
sortOrder);
TextView emailText = (TextView) findViewById(R.id.emailText);
if (cursor != null) {
cursor.moveToFirst();
String category;
for (int i = 0; i < cursor.getCount(); i++){
category = cursor.getString(cursor
.getColumnIndexOrThrow(Contact.COLUMN_EMAIL));
addresses.add(category);
cursor.moveToNext();
}
// always close the cursor
cursor.close();
}
}
我正在尝试访问我已经创建的 Content Provider(这是我非常严格地按照教程创建的,我已经使用教程引导我完成的代码对其进行了测试并且工作正常)并阅读电子邮件我添加到 SQLite 数据库中的地址。在数据库中是 ID,一个 COLUMN_NAME 和一个 COLUMN_EMAIL。我想获取电子邮件列的所有行,并将其作为字符串的 ArrayList returned 到此 activity.
到目前为止我最好的猜测是我会在某个地方使用内容提供者的查询方法查询数据库,return 光标到 activity,然后收集所有字符串从行中发送一个只包含该列投影的查询,或者过滤掉所有 @lorem.com 或游标的第二个索引或某种 post 数据检索过滤器。
基本上我只是被困住了。
好的,这是我的代码:
public class EmailScheduler extends AppCompatActivity implements LoaderManager
.LoaderCallbacks<Cursor> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_email_scheduler);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView emailText = (TextView) findViewById(R.id.emailText);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor cursor = getContacts();
Log.i("TAG", cursor.getColumnName(2));
emailText.append(cursor.toString());
}
});
}
private static final int CONTACT_LOADER = 0;
public Uri contactUri;
ArrayList<String> addresses = new ArrayList<>();
Cursor cursor;
private Cursor getContacts() {
// Run query
Uri uri = Contact.CONTENT_URI;
String[] projection = new String[] { Contact._ID,
Contact.COLUMN_NAME };
String selection = Contact.COLUMN_EMAIL + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = Contact.COLUMN_NAME
+ " COLLATE LOCALIZED ASC";
return getContentResolver().query(uri, projection, selection, selectionArgs,
sortOrder);
}
// called by LoaderManager to create a Loader
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader;
switch (id) {
case CONTACT_LOADER:
cursorLoader = new CursorLoader(this,
contactUri, // Uri of contact to display
null, // null projection returns all columns
null, // null selection returns all rows
null, // no selection arguments
null); // sort order
break;
default:
cursorLoader = null;
break;
}
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.i("TAG", "got to the beginning of onloadfinish " + data);
if (data != null && data.moveToFirst()) {
int nameIndex = data.getColumnIndex(Contact.COLUMN_NAME);
int emailIndex = data.getColumnIndex(Contact.COLUMN_EMAIL);
String address = data.getString(emailIndex);
Log.i("TAG", address);
while (data.getString(emailIndex) != null){
addresses.add(cursor.getString(cursor.getColumnIndex(
Contact.COLUMN_EMAIL)));
Log.i("TAG", addresses.toString());}
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) { }
}
在 onCreate 方法中,当我 运行 时,它 return 发送此数据:android.content.ContentResolver$CursorWrapperInner@60c9bd2
如何从中获取我需要的信息?我尝试的一切都变成了死胡同。
private void getContacts() {
List<String> addresses = new ArrayList<String>();
// Run query
Uri uri = Contact.CONTENT_URI;
String[] projection = new String[] { Contact.COLUMN_EMAIL };
String selection = null;
String[] selectionArgs = null;
String sortOrder = Contact.COLUMN_EMAIL
+ " COLLATE LOCALIZED ASC";
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs,
sortOrder);
TextView emailText = (TextView) findViewById(R.id.emailText);
if (cursor != null) {
cursor.moveToFirst();
String category;
for (int i = 0; i < cursor.getCount(); i++){
category = cursor.getString(cursor
.getColumnIndexOrThrow(Contact.COLUMN_EMAIL));
addresses.add(category);
cursor.moveToNext();
}
// always close the cursor
cursor.close();
}
}