【问题标题】:Returning all rows of a column from my content provider从我的内容提供者返回一列的所有行
【发布时间】:2022-01-05 00:23:52
【问题描述】:

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

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

基本上我只是卡住了。

好的,这是我的代码:

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

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

【问题讨论】:

    标签: java android sqlite android-cursor


    【解决方案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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多