首页 文章

从我的内容提供商返回列的所有行

提问于
浏览
1

我正在尝试访问我已经制作的内容提供商(我通过非常仔细地遵循教程,我已经使用代码进行了测试,教程引导我完成并且它工作正常)并阅读我已经编写的电子邮件地址添加到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 回答

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

相关问题