jeudi 31 mars 2016

Returning all rows of a column from my content provider

I'm trying to access a Content Provider I already made (which I made by very closely following a tutorial, I've tested it with the code the tutorial walked me through and it works fine) and read the email addresses that I've added into a SQLite database. In the database is the ID, a COLUMN_NAME, and a COLUMN_EMAIL. I want to get all the rows for the email column and have that be an ArrayList of strings that's returned into this activity.

So far my best guess is that somewhere somehow I'll query the database using the query method from the content provider, return the cursor to the activity, and then collect all the Strings from the rows and either manage to send a query with a projection for just that column or filter out all the @lorem.com's or second index of the cursor or some kind of post data retrieval filter.

Basically I'm just pretty stuck.

Okay so here's my code:

public class EmailScheduler extends AppCompatActivity implements 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) { }

}

In the onCreate method it returns this data when I run it: android.content.ContentResolver$CursorWrapperInner@60c9bd2

How do I get the information I need from that? Everything I try turns into a dead end.

Aucun commentaire:

Enregistrer un commentaire