gpt4 book ai didi

java - 从我的内容提供者返回一列的所有行

转载 作者:行者123 更新时间:2023-11-29 20:00:28 25 4
gpt4 key购买 nike

我正在尝试访问我已经创建的 Content Provider(这是我非常严格地遵循教程创建的,我已经使用教程引导我完成的代码对其进行了测试并且它工作正常)并阅读电子邮件地址我已经添加到 SQLite 数据库中。在数据库中是 ID、COLUMN_NAME 和 COLUMN_EMAIL。我想获取电子邮件列的所有行,并将其作为返回到该 Activity 中的字符串的 ArrayList。

到目前为止,我最好的猜测是,我会在某个地方使用内容提供程序的查询方法查询数据库,将光标返回到 Activity ,然后从行中收集所有字符串,或者设法发送查询仅对该列进行投影或过滤掉所有@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

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

最佳答案

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

关于java - 从我的内容提供者返回一列的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36344930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com