从我的内容提供商

时间:2016-03-31 22:29:09

标签: java android sqlite cursor provider

我试图访问我已经制作的内容提供商(我通过非常密切关注教程,我已经使用教程中的代码对其进行了测试,并且工作正常)并阅读我已添加到SQLite数据库中的电子邮件地址。在数据库中是ID,COLUMN_NAME和COLUMN_EMAIL。我想获取电子邮件列的所有行,并将其作为返回此活动的字符串的ArrayList。

到目前为止,我最好的猜测是某处某处我会使用内容提供程序中的查询方法查询数据库,将游标返回到活动,然后从行中收集所有字符串并管理发送只对该列进行投影的查询,或过滤掉所有@ lorem.com或光标的第二个索引或某种后期数据检索过滤器。

基本上我只是被卡住了。

好的,这就是我的代码:

公共类EmailScheduler扩展AppCompatActivity实现LoaderManager         .LoaderCallbacks {

@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方法中,它在运行时返回此数据:android.content.ContentResolver$CursorWrapperInner@60c9bd2

如何从中获取所需信息?我尝试的一切都变成了死胡同。

1 个答案:

答案 0 :(得分:1)

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();
    }
}