游标只检索一个项目,而不是所有项目
Cursor only retrieving one item, not all items
所以我在我的应用程序中使用 Cursor 从存储中检索所有曲目。问题是它只返回一首曲目而不是所有曲目。代码完美无误。即使我已经尝试访问我正在检索和存储所有曲目的 ArrayList。 ArrayList 的大小是 1 而不是很多。我正在添加以下代码:
private void Encrypt() {
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if(cursor!= null && cursor.moveToFirst()){
int TrackTitle = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int Artist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int Path = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do{
String Tracktitle = cursor.getString(TrackTitle);
String Trackartist = cursor.getString(Artist);
String Trackpath = cursor.getString(Path);
arrayList = new ArrayList<CustomListView>();
arrayList.add(new CustomListView(R.drawable.none, Tracktitle, Trackartist, Trackpath));
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, arrayList);
tracks.setAdapter(customListViewAdapter);
tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}while (cursor.moveToNext());
代码到底有什么问题??
您正在每个循环中创建一个新的 ArrayList
,将此行移出您的 do-while 循环:
arrayList = new ArrayList<CustomListView>();
do {
...
所以我在我的应用程序中使用 Cursor 从存储中检索所有曲目。问题是它只返回一首曲目而不是所有曲目。代码完美无误。即使我已经尝试访问我正在检索和存储所有曲目的 ArrayList。 ArrayList 的大小是 1 而不是很多。我正在添加以下代码:
private void Encrypt() {
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if(cursor!= null && cursor.moveToFirst()){
int TrackTitle = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int Artist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int Path = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do{
String Tracktitle = cursor.getString(TrackTitle);
String Trackartist = cursor.getString(Artist);
String Trackpath = cursor.getString(Path);
arrayList = new ArrayList<CustomListView>();
arrayList.add(new CustomListView(R.drawable.none, Tracktitle, Trackartist, Trackpath));
CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, arrayList);
tracks.setAdapter(customListViewAdapter);
tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}while (cursor.moveToNext());
代码到底有什么问题??
您正在每个循环中创建一个新的 ArrayList
,将此行移出您的 do-while 循环:
arrayList = new ArrayList<CustomListView>();
do {
...