我想通过查询获得的一张专辑的歌曲列表MediaStore与CursorLoader
我怎样才能做到这一点 ?我可以使用以下代码获取设备的所有歌曲:
static final String[] TRACK_SUMMARY_PROJECTION = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE};
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
String select = null;
return new CursorLoader(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
TRACK_SUMMARY_PROJECTION, select, null,
sortOrder);
}
Run Code Online (Sandbox Code Playgroud)
我应该添加什么代码或修改来过滤特定专辑的歌曲?
我想在chrome和其他浏览器中获取最后访问过的URL.我能够在Android Native浏览器中获取Last URL.我使用以下代码 -
Cursor cur = getContentResolver().query(Browser.BOOKMARKS_URI,
new String[] { Browser.BookmarkColumns.URL }, null, null,
BookmarkColumns.DATE + " DESC");
if (cur != null && cur.getCount() > 0) {
cur.moveToFirst();
String url = cur.getString(cur
.getColumnIndex(Browser.BookmarkColumns.URL));
cur.close();
return url;
} else {
if (cur != null) {
cur.close();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码不适用于其他浏览器,例如chrome.如何在Chrome和其他浏览器中获取上次访问过的网址.
提前致谢.
我正在尝试用AlphabetIndexer实现一个快速滚动条,但是当光标改变时,它没有刷新索引chache.在我的CursorAdapter构造函数中,我调用setCursor(cursor)但没有任何更改,并根据文档:
如果光标发生变化,您的适配器负责通过调用setCursor(Cursor)来更新光标.getPositionForSection(int)方法执行二进制搜索给定部分(字母表)的起始索引.
但什么都没发生.我将其用于搜索过滤器,因此当我搜索联系人时,它会使用联系人更新列表,因此AlphabetIndexer应该用于更新列表中新项目的索引.
示例:我的整个列表以以"A"开头的联系人开头,以以"E"开头的联系人结束.所以AlphabetIndexer会在其缓存中包含这些索引.
但是,让我们尝试使用'C'搜索联系人,并假设我有250个以'C'开头的联系人.所以,我必须快速滚动浏览这些联系人,并且'C'索引必须显示,但不是只有'C'它显示所有索引,当我有整个列表时显示.
这是我的CursorAdapter构造函数,我为每个输入的字母调用setCursor(cursor):
public MyCursorAdapter(Context context, Cursor cursor, ArrayList<Integer> ids)
{
super(context, cursor);
try
{
mAlphaIndexer = new AlphabetIndexer(cursor, cursor.getColumnIndexOrThrow("Name")," ABCDEFGHIJKLMNOPQRSTUVWXYZ");
notifyDataSetChanged();
mAlphaIndexer.setCursor(cursor);
this.mSelectedContacts = ids;
Log.e("MyCursorAdapter", "construtor: ");
}
catch(Exception ex)
{
Log.e("MyCursorAdapter", "Error: " + ex);
}
finally
{
mAlphaIndexer.setCursor(cursor);
}
}
Run Code Online (Sandbox Code Playgroud) 我问这个问题是因为 StackOverflow 上的一些答案不是我要找的答案。我的问题是,在 Android 的早期版本(如 2.3)中,查询非常慢,并且我收到此消息。
我的错误信息
06:37:25.521: ERROR/CursorWindow(322): need to grow: mSize = 1048576, size = 45, freeSpace() = 43, numRows = 8928
07-12 06:37:25.521: ERROR/CursorWindow(322): not growing since there are already 8928 row(s), max size 1048576
我有 8928 行的原因是因为我正在做搜索建议,有人可以输入“t”并获得 8928 行或更大的行,这显然会冻结 Android 2.3。我无法限制我的搜索建议阈值,因为某些仅包含两个字符的结果可能有 20 个结果或 20000 个以上结果,具体取决于结果是什么。
此链接可能是获得答案的最佳位置。给定链接中的最后一个答案似乎很有希望,但是除非您这样做,否则无法获取行数,cursor.getCount()这需要您从大查询中获取行总数(例如 20000+ 结果),除非我'我想错了。有一些解决方法吗?您将如何在代码中执行此操作来解决 CursorWindow 问题?
我很困惑是使用 MergeCursor 还是 CursorJoiner。
我有一个包含大量数据的光标 (A)。假设 Cursor (A) 中有 100 行和 3 列。我想要做的是向 Cursor 插入(追加)一个新列,因此生成的 Cursor (B) 有 100 行但有 4 列。
此时此刻,我希望第 4 列包含 100 行的默认值。
我该怎么做?
看起来android.database.Cursor.getColumnIndex(String)在行循环上应该是不变的。
但我看到大量代码在执行以下操作:
try (Cursor cursor = database.query(...)) {
while (cursor.moveToNext()) {
String aString = cursor.getString(cursor.getColumnIndex(aColumnName));
// ... other loop code and more repeated use of getColumnIndex()
}
}
Run Code Online (Sandbox Code Playgroud)
这看起来很愚蠢而且浪费,除非 的结果getColumnIndex()确实会随着对cursor.moveToNext()-- 的调用而变化,并且这个调用应该被提升到循环之外,如下所示:
try (Cursor cursor = database.query(...)) {
int aStringIdx = cursor.getColumnIndex(aColumnName);
while (cursor.moveToNext()) {
String aString = cursor.getString(aStringIdx);
// ... other loop code
}
}
Run Code Online (Sandbox Code Playgroud)
或者在最坏的情况下(假设getColumnIndex()要求您位于有效的数据行上):
try (Cursor cursor = database.query(...)) {
if (cursor.moveToFirst()) {
int aStringIdx = cursor.getColumnIndex(aColumnName);
do {
String …Run Code Online (Sandbox Code Playgroud) 我正在尝试将返回具有多列的单行的查询转换为具有一列或多列的多行。例如,我的查询如下所示:
SELECT _id, value1, value2, value3, value4 FROM table WHERE _id IS 1;
Run Code Online (Sandbox Code Playgroud)
结果:
_id value1 value2 value3 value4
----------------------------------------
1 10 11 12 13
Run Code Online (Sandbox Code Playgroud)
我希望我的结果如下所示:
name value
--------------
value1 10
value2 11
value3 12
value4 13
Run Code Online (Sandbox Code Playgroud)
但我也可以处理这样的事情:
value
-----
10
11
12
13
Run Code Online (Sandbox Code Playgroud)
目前我正在使用 Android 中的 MatrixCursor 手动转换它,但理想的情况是光标已经处于可用状态。有没有办法修改我的查询以检索所需架构中的结果,或者使用临时表之类的东西来实现所需的最终状态?
另外,是否有一个通用术语来描述数据库中的此功能?
我已经将我的手机联系人排序并列入了一个arraylist但是,我在列表中找到了许多相同联系人姓名的副本.这种情况怎么样?怎么避免这个?
这是我试过的,
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,
"(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
while (cursor.moveToNext()) {
try {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact_names_list.add(name);
phone_num_list.add(phonenumber);
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
谁能帮忙?
我们最近在使用 Android 的 Room 数据库时遇到了一些问题。
崩溃只发生在某些用户身上,但我无法在我的测试设备或模拟器上重现它。(这有时也会发生在 Firebase 测试设备上)
有一些例外似乎都是相关的。
所有这些异常都发生在我们由 Room 生成的 Dao 的不同行中。
(无论是查询、插入、更新还是删除)。
CursorWindowAllocationException: Cursor window allocation of 2048 kb failed.
IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: ‘/whatever/database’
SQLiteDiskIOException: disk I/O error (code 266)
SQLiteDiskIOException: disk I/O error (code 778)
SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26)
SQLiteException: Failed to change locale for db ‘/whatever/database' to 'fr_FR'.
Run Code Online (Sandbox Code Playgroud)
我试过的:
这些方法都没有帮助,错误仍然存在。
该LiveData的查询是唯一一个这是不卸载默认的数据库执行者,因为我无法找到一个方法来做到这一点。我怀疑这可能是问题所在,但我不完全确定。
这是 LiveData …
我在 Android Studio 中收到与光标有关的错误。
\n我的代码中有以下行
\nval dataText = cursor.getString(cursor.getColumnIndex(MyDbNameClass.COLUMN_NAME_TITLE))\nRun Code Online (Sandbox Code Playgroud)\n我尝试使用cursor.getColumnIndexOrThrow(),但它没有帮助\n我是android新手,希望你能帮助我。这是我的代码。
\n活动主文件
\n<?xml version="1.0" encoding="utf-8"?>\n<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n tools:context=".MainActivity">\n\n <TextView\n android:id="@+id/tvTest"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:text="Hello World!"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintLeft_toLeftOf="parent"\n app:layout_constraintRight_toRightOf="parent"\n app:layout_constraintTop_toTopOf="parent" />\n\n <EditText\n android:id="@+id/edTitle"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:ems="10"\n android:inputType="textPersonName"\n android:text="Name"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintHorizontal_bias="0.437"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent"\n app:layout_constraintVertical_bias="0.071" />\n\n <EditText\n android:id="@+id/edDesc"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:ems="10"\n android:inputType="textPersonName"\n android:text="Name"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintHorizontal_bias="0.437"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent"\n app:layout_constraintVertical_bias="0.166" />\n\n <Button\n android:id="@+id/button"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:onClick="onClickSave"\n android:text="Button"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintEnd_toEndOf="parent"\n app:layout_constraintHorizontal_bias="0.498"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toTopOf="parent"\n app:layout_constraintVertical_bias="0.796" />\n\n</androidx.constraintlayout.widget.ConstraintLayout>\nRun Code Online (Sandbox Code Playgroud)\nMainActivity.kt
\n …android ×10
android-cursor ×10
sqlite ×4
android-room ×1
kotlin ×1
mergecursor ×1
sorting ×1
unpivot ×1