Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从我的内容提供程序返回列的所有行_Java_Android_Sqlite_Cursor_Provider - Fatal编程技术网

Java 从我的内容提供程序返回列的所有行

Java 从我的内容提供程序返回列的所有行,java,android,sqlite,cursor,provider,Java,Android,Sqlite,Cursor,Provider,我正在尝试访问一个我已经制作的内容提供者(我是通过非常密切地遵循一个教程制作的,我已经用教程教给我的代码对它进行了测试,效果很好),并读取我添加到SQLite数据库中的电子邮件地址。数据库中有ID、列名称和列电子邮件。我想获取电子邮件列的所有行,并将其作为返回到此活动的字符串的ArrayList 到目前为止,我最好的猜测是,不知何故,我将使用内容提供者的查询方法查询数据库,将光标返回到活动,然后从行中收集所有字符串,并设法发送一个仅针对该列的投影查询,或者过滤掉所有@lorem.com或游标的第

我正在尝试访问一个我已经制作的内容提供者(我是通过非常密切地遵循一个教程制作的,我已经用教程教给我的代码对它进行了测试,效果很好),并读取我添加到SQLite数据库中的电子邮件地址。数据库中有ID、列名称和列电子邮件。我想获取电子邮件列的所有行,并将其作为返回到此活动的字符串的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) { }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u email\u调度程序);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
最终文本视图emailText=(文本视图)findViewById(R.id.emailText);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Cursor=getContacts();
Log.i(“TAG”,cursor.getColumnName(2));
emailText.append(cursor.toString());
}
});
}
专用静态最终int联系人_LOADER=0;
公共Uri;
ArrayList地址=新的ArrayList();
光标;
私有游标getContacts(){
//运行查询
Uri=Contact.CONTENT\u Uri;
字符串[]投影=新字符串[]{Contact.\u ID,
Contact.COLUMN_NAME};
字符串选择=Contact.COLUMN_EMAIL+“=”“
+ ("1") + "'";
字符串[]selectionArgs=null;
字符串排序器=Contact.COLUMN\u NAME
+“整理本地化ASC”;
返回getContentResolver(),
分拣机);
}
//LoaderManager调用以创建加载程序
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
游标装载机游标装载机;
开关(id){
案件联系人(加载器):
游标装入器=新游标装入器(此,
contactUri,//要显示的联系人的Uri
null,//null投影返回所有列
null,//null选择返回所有行
null,//没有选择参数
null);//排序顺序
打破
违约:
游标加载程序=null;
打破
}
返回游标装入器;
}
@凌驾
public void onLoadFinished(加载器、光标数据){
Log.i(“标记”,“到达onloadfinish的开头”+数据);
if(data!=null&&data.moveToFirst()){
int nameIndex=data.getColumnIndex(Contact.COLUMN\u NAME);
int emailIndex=data.getColumnIndex(Contact.COLUMN\u EMAIL);
字符串地址=data.getString(emailIndex);
Log.i(“标签”,地址);
while(data.getString(emailIndex)!=null){
address.add(cursor.getString(cursor.getColumnIndex(
联系方式(电邮);;
Log.i(“TAG”,addresses.toString());}
}
}
@凌驾
public void onLoaderReset(加载程序){}
}

在onCreate方法中,当我运行它时,它会返回以下数据:android.content.ContentResolver$CursorWrapperInner@60c9bd2

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

private void getContacts(){
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();
    }
}
列表地址=新的ArrayList(); //运行查询 Uri=Contact.CONTENT\u Uri; 字符串[]投影=新字符串[]{Contact.COLUMN_EMAIL}; 字符串选择=null; 字符串[]selectionArgs=null; 字符串排序器=Contact.COLUMN\u电子邮件 +“整理本地化ASC”; Cursor Cursor=getContentResolver().query(uri、投影、选择、selectionArgs、, 分拣机); TextView emailText=(TextView)findViewById(R.id.emailText); 如果(光标!=null){ cursor.moveToFirst(); 字符串类别; 对于(int i=0;i